Java bubble sort

    public static void bubbleSort(int[] array){
        //3 1 5 8 2 9 4 6 7    n*(n-1)/2    n
        for(int i=array.length-1;i>0;i--) {
            boolean flag=true;
            for (int j = 0; j < i; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    flag=false;
                }
            }
            if(flag){
                break;
            }
        }
    }  
Copy the code

Java selection sort

public static void selectSort(int[] array){ for(int i=0; i<array.length-1; i++) { int index = i; for (int j = i+1; j < array.length; j++) { if (array[j] < array[index]) { index = j; / /,2,5,8,3,9,4,6,7} {1}}; if(index! Int temp = array[index]; int temp = array[index]; array[index] = array[i]; array[i] = temp; }}}Copy the code

Java for other sorts

public class ExampleUnitTest { @Test public void addition_isCorrect() throws Exception { int[] array=new ,7,4,9,3,2,6,5,8 int [] {1}; int key=35; quickSort(array,0,array.length-1); // System.out.println(binarySearch(array,0,array.length,key)); for (int i : array) { System.out.print(i+" "); }} public static int binarySearch(int[] array,int fromIndex,int toIndex,int key){int low=fromIndex; int high=toIndex-1; while(low<=high){ int mid=(low+high)/2; Int midVal=array[mid]; If (key>midVal){// if(key>midVal){ }else if(key<midVal){ }else{ return mid; } } return -(low+1); Public static void quickSort(int[] array,int begin,int begin) public static void quickSort(int[] array,int begin,int end){ if(end-begin<=0) return; int x=array[begin]; int low=begin; //0 int high=end; Boolean direction=true; Boolean direction=true; L1: while(low<high){if(direction){// from right to left for(int I =high; i>low; i--){ if(array[i]<=x){ array[low++]=array[i]; high=i; direction=! direction; continue L1; } } high=low; }else{for(int I =low; i<high; i++){ if(array[i]>=x){ array[high--]=array[i]; low=i; direction=! direction; continue L1; } } low=high; Array [low]=x; QuickSort (array,begin,low-1); quickSort(array,low+1,end); } @ Test public void Test () {int [] array = new int [],1,6,4,3,9,8,10,7,5 {2}; / / merge (array, 0,4,7); mergeSort(array,0,array.length-1); for (int i : array) { System.out.print(i+" "); } } public static void mergeSort(int array[],int left,int right){ if(left==right){ return; }else{ int mid=(left+right)/2; mergeSort(array,left,mid); mergeSort(array,mid+1,right); merge(array,left,mid+1,right); } } // 0 4 7 // 1 2 5 9 === 3 4 10 11 public static void merge(int[] array,int left,int mid,int right){ int leftSize=mid-left; int rightSize=right-mid+1; Int [] leftArray=new int[leftSize]; int[] rightArray=new int[rightSize]; For (int I =left; i<mid; i++){ leftArray[i-left]=array[i]; } for(int i=mid; i<=right; i++){ rightArray[i-mid]=array[i]; } // merge int I =0; int j=0; int k=left; while(i<leftSize && j<rightSize){ if(leftArray[i]<rightArray[j]){ array[k]=leftArray[i]; k++; i++; }else{ array[k]=rightArray[j]; k++; j++; } } while(i<leftSize){ array[k]=leftArray[i]; k++; i++; } while(j<rightSize){ array[k]=rightArray[j]; k++; j++; }}}Copy the code