One day a series of the first, to see if they can persist, refueling!
Bubble sort and quick sort
Bubble sort, the first interview in life, NICE's examiner asked if I can write down
Quick sort, feel tall, divide and conquer thought, must learn
1 PackageOne ;2 3 Public classTest_sort {4 5 Public Static voidMain (string[] args) {6 int[] A = {2, 9, 5, 4, 8, 1 };7 //sort (bubble_sort);8Quick_sort (A, 0, a.length-1);9 for(inti = 0; i < a.length; i++) {TenSystem.out.print (A[i] + ""); One } A } - - //Bubble Sort the Public Static int[] Bubble_sort (int[] a) { - //set up a decision to optimize - //If the interchange does not occur during a traversal, it is not necessary to proceed because the sort is complete - BooleanAC =true; + for(intK = 1; K < A.length & ac; k++) { -AC =false; + for(inti = 0; i < a.length-1; i++) { A if(A[i] > a[i + 1]) { at inttemp =A[i]; -A[i] = a[i + 1]; -A[i + 1] =temp; -AC =true; - } - } in } - returnA; to } + - //Quick Sort the Public Static int[] Quick_sort (intS[],intLintr) { * if(L <r) { $ //Swap (S[l], s[(l + R)/2]);//swap this number and first number in the middle to see note 1Panax Notoginseng inti = l, j = r, x =S[l]; - while(I <j) { the //find the first number less than x from right to left + while(I < J && S[j] >=x) Aj--; the if(I <j) +s[i++] =S[j]; - $ //find the first number greater than or equal to x from left to right $ while(I < J && S[i] <x) -i++; - if(I <j) thes[j--] =S[i]; - }WuyiS[i] =x; theQuick_sort (S, l, i-1);//recursive calls from right to left -Quick_sort (S, i + 1, R);//from left to right Wu } - returns; About } $}
One day a Java foundation--Sort