Selection sort, the complexity 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]+" "); } } }
Bubble sort complexity O (nlogn)
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+" "); } } }
Dichotomy inquiry - non-iterative
org.junit.Test Import; / ** * locate the number N, a plurality of repeating applicable to digital, take up used portions of the native method integer math / rounding down * middle = min + (max- min) / 2 algorithm is more accurate
is not found * -1 * @author zhzh.yin * * / public class CTest { @Test public void testMethod () { Integer [] = {1,2,3,3,3,4,5,5 NUM, } 5,5,5,6,6,6,7,10; System.out.println ( "IS AT NUM The" + selectPosition (NUM,. 8)); } public int selectPosition (Integer [] numlist, NUM int ) { int min = 0; int = numList.length-max. 1; int Middle = -1; the while (min <= max) { Middle = min + (max-min) / 2; System.out.println ( "min to - "+ min +", max - "+ max +", middle - "+ middle); IF ((== Middle min || Middle == max) && 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; } }