java 常用算法和一些题目
选择排序,复杂度O(n²)
package com.example.demo; import org.junit.Test; /** * 选择排序 * @author zhzh.yin * */ public class HTest { @Test public void testMethod(){ Integer [] numList = {1,2,2,2,1,4,5,2,5,3,9,6}; for(int i=0;i<numList.length-1;i++){ int max = numList[i]; for(int j = i+1;j<numList.length;j++){ if(numList[i]<numList[j]){ max = numList[j]; numList[j]=numList[i]; numList[i]=max; } } } for(int num:numList){ System.out.print(numList[num]+" "); } } }
冒泡排序 复杂度 O(n2)
package com.example.demo; import org.junit.Test; /** * 冒泡排序 * @author zhzh.yin * */ public class FTest { @Test public void testMethod(){ Integer[]numList = {1,2,1,1,1,8,3,5,1}; if(numList.length==1){ System.out.println("-1"); }else if(numList.length==2){ String string =""; System.out.println( numList[1]>numList[2]? numList[1]+""+numList[0]:numList.toString());; } int temp = 0; for(int i =0;i<numList.length-1;i++){ for(int j=i+1;j<numList.length;j++){ if(numList[i]<numList[j]){ temp=numList[i]; numList[i]=numList[j]; numList[j]=temp; } } } for(int num :numList){ System.out.print(num+" "); } } }
快速排序O(nlogn)
@Test public void speedSortTest(){ Integer []numList={2,3,7,9,5,10,1,4}; speedSort(numList,0,numList.length-1); for(int time:numList){ System.out.print(time+" "); } } public void speedSort(Integer []numList,int lower,int higher){ if(lower>=higher){ return; } int temp=0; int icon = numList[lower]; int indexofLeft = lower; int indexofRight = higher; while(indexofLeft<indexofRight){ while(icon<=numList[indexofRight]&&indexofLeft<indexofRight){ indexofRight--; } while(icon>=numList[indexofLeft]&&indexofLeft<indexofRight){ indexofLeft++; } if(indexofLeft<indexofRight){ temp = numList[indexofRight]; numList[indexofRight]=numList[indexofLeft]; numList[indexofLeft]=temp; } for(int num:numList){ System.out.print(num+" "); } System.out.println(); } temp=numList[indexofLeft]; numList[indexofLeft]=numList[lower]; numList[lower]=temp; speedSort(numList,lower,indexofLeft-1); speedSort(numList,indexofRight+1,higher); }
二分法查询-非迭代O(logn)
http://hedengcheng.com/?p=595
import org.junit.Test; /** * 找到数字N,适用于有多个重复数字,使用部分math原生方法向上取整/向下取整 * middle=min+(max-min)/2 算法更加准确
* 返回-1为没有找到 * @author zhzh.yin * */ public class CTest { @Test public void testMethod(){ Integer[] num ={1,2,3,3,3,4,5,5,5,5,5,6,6,6,7,10}; System.out.println("the num is at "+selectPosition(num,8)); } public int selectPosition(Integer [] numList,int num){ int min=0; int max=numList.length-1; int middle = -1; while(min<=max){ middle = min+(max-min)/2; System.out.println("min--"+min+",max--"+max+",middle--"+middle); if((min==middle||max==middle)&&numList[middle]!=num){ middle = -1; break; } if(numList[middle]<num){ min=(int) Math.floor(middle); }else if(numList[middle]>num){ max=(int) Math.ceil(middle); }else{ break; } } return middle; } }
迭代+复杂化二分法
package com.example.demo; import org.junit.Test; /** * 找出数组中最后一个6(或者8)的位置,复杂度O(nlogn),用迭代实现 * * @author zhzh.yin flag==true 获取最右的坐标 * */ public class CTest { @Test public void testMethod() { Integer[] num = { 1, 2, 3, 3, 3, 4, 5, 5, 5, 5, 5, 6, 6, 6, 7, 10 }; System.out.println("hello,position is " + selectPosition(num, 10, 0, num.length - 1, false)); } public int selectPosition(Integer[] numList, int num, int min, int max, Boolean flag) { int middle = min + (max - min) / 2; System.out.println("min-" + min + ",max-" + max + ",middle-" + middle); if (min < max - 1) { if (numList[middle] < num) { return selectPosition(numList, num, (int) Math.floor(middle), max, flag); } else if (numList[middle] > num) { return selectPosition(numList, num, min, (int) Math.ceil(middle), flag); } else if (numList[middle] == num && (min != max && min != max - 1)) { if (flag) { return selectPosition(numList, num, (int) Math.ceil(middle), max, flag); } else { return selectPosition(numList, num, min, (int) Math.floor(middle), flag); } } } else{ if (numList[min] != num && numList[max] != num) { middle = -1; return middle; } else if (numList[min] == numList[max]) { return flag.equals(true) ? max : min; } else if (numList[min] == num) { return min; } else { return max; } } return middle; } }
顺时针打印矩阵
递归
import org.junit.Test; /** * 顺时针打印矩阵 * @author user * */ public class ATest { //顺时针打印一个矩阵 @Test public void test(){ int[][] num = new int[100][100]; int n = 6; int count =1; for(int i=0;i<n;i++){ for(int j =0;j<n;j++){ num[i][j]=count++; } } output(num,0,n-1); } /** * * @param num * @param start * @param end */ public void output(int[][] num,int start,int end){ if(start>end || end<=0)return; for(int i=start;i<=end;i++){ System.out.println(num[start][i]); } for(int i=start+1;i<=end;i++){ System.out.println(num[i][end]); } for(int i=end-1;i>=start;i--){ System.out.println(num[end][i]); } for(int i=end-1;i>start;i--){ System.out.println(num[i][start]); } output(num,start+1,end-1); } }
找出出现次数最多的
先recordNum 再从hashMap里面找出value最大的
package com.example.demo; import java.util.HashMap; import java.util.Map; import org.junit.Test; /** * 找出出现次数最多的数值 * @author zhzh.yin * */ public class BTest { @Test public void TestMethod(){ Integer []numList = {1,1,1,-1,2,3,44,4,5,6,9,9,9,9,9,8,7,6,5}; // chooseMax(recordNum(numList)); System.out.println(chooseMax(recordNum(numList),numList[0])); } public HashMap <Integer,Integer> recordNum (Integer [] numList){ HashMap<Integer, Integer> map=new HashMap<>(); for(int num:numList){ if(map==null){ map.put(num, 1); continue; } if(map.containsKey(num)){ map.put(num, map.get(num)+1); }else{ map.put(num, 1); } } return map; } public String chooseMax(HashMap<Integer, Integer>map,int num){ String string = ""; int key = num; int time = map.get(key); for(int keyActually :map.keySet()){ if(map.get(keyActually)>time){ time=map.get(keyActually); key = keyActually; } } string= "出现次数最多的数是"+key+" "+time+" "+map.toString(); return string; } }
把AB文件字符合并重排,输出到C
package com.example.demo; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.junit.Test; /** * 把AB文件中的字符重排,输入到C * @author zhzh.yin * */ public class ETest { @Test public void TestMethod() throws IOException{ FileReader filea = new FileReader("C:\\Users\\user\\Downloads\\a.txt"); BufferedReader ina = new BufferedReader(filea) ; FileReader fileb = new FileReader("C:\\Users\\user\\Downloads\\b.txt"); BufferedReader inb = new BufferedReader(fileb) ; String line ; char[]arr; List<Character> list = new ArrayList<>(); BufferedWriter writerc= new BufferedWriter(new FileWriter("C:\\Users\\user\\Downloads\\c.txt")); while((line=ina.readLine())!=null){ arr =line.toCharArray(); for(int i=0;i<arr.length;i++){ list.add(arr[i]); } } ina.close(); while((line=inb.readLine())!=null){ arr =line.toCharArray(); for(int i=0;i<arr.length;i++){ list.add(arr[i]); } } inb.close(); Object[]array = list.toArray(); Arrays.sort(array); for(Object object:array){ writerc.write((char)object); } writerc.close(); FileReader filec = new FileReader("C:\\Users\\user\\Downloads\\c.txt"); BufferedReader read = new BufferedReader(filec); while((line = read.readLine())!=null){ System.out.println(line); } read.close(); } }
找出出现2次的值
package com.example.demo; import java.util.HashMap; import org.junit.Test; /** * 找出出现2次的数值 * @author zhzh.yin * */ public class GTest { @Test public void testMethod(){ Integer [] numList = {1,2,2,2,3,4,5,5,6,7,7}; HashMap<Integer,Integer> numMap = new HashMap(); for(int num :numList){ if (!numMap.containsKey(num)){ numMap.put(num, 1); }else { numMap.put(num, numMap.get(num)+1); } } System.out.println(numMap); for(HashMap.Entry<Integer, Integer>entry :numMap.entrySet()){ if(entry.getValue()==2){ System.out.print(entry.getKey()+" ");} } } }