Why not take the Wu Hook and charge the 50 states of Guanshan. please Ling on the temporary , if a scholar office?
--13 South gardenThree kinds of sorting method: 1, bubbling Method 2, simple selection Method 3, Direct insertion code: 1, Bubble sort
1 Public classBubblesort {2 3 //The simplest sort, starting from 0 to the top4 Public Static voidBubblesort (int[] arr) {5 for(inti =0; i < arr.length-1; i++) {6 for(intj = i+1; J < Arr.length; J + +) {7 if(arr[i]>Arr[j]) {8Arr[i] = arr[i]^Arr[j];9ARR[J] = arr[i]^Arr[j];TenArr[i] = arr[i]^Arr[j]; One } A } - } - } the //authentic bubbling, positive - Public Static voidBubblesorta (int[] arr) { - for(inti =0; I < arr.length-1; i++) { - for(intj =0; J < arr.length-i-1; J + +) { + if(arr[j]>arr[j+1]) { -ARR[J] = arr[j]^arr[j+1]; +arr[j+1] = arr[j]^arr[j+1]; AARR[J] = arr[j]^arr[j+1]; at } - } - } - } - //Reverse - Public Static voidBUBBLESORTB (int[] arr) { in for(inti =0; I < arr.length-1; i++) { - for(intj = arr.length-1; J > i; j--) { to if(arr[j-1]>Arr[j]) { +ARR[J] = arr[j]^arr[j-1]; -arr[j-1] = arr[j]^arr[j-1]; theARR[J] = arr[j]^arr[j-1]; * } $ }Panax Notoginseng } - } the //Optimize bubbling + Public Static voidBubbleSortA0 (int[] arr) { ABoolean flag =true; the for(inti =0; I < arr.length-1&& Flag; i++) { +Flag =false; - for(intj = arr.length-1; J > i; j--) { $ if(arr[j-1]>Arr[j]) { $ARR[J] = arr[j]^arr[j-1]; -arr[j-1] = arr[j]^arr[j-1]; -ARR[J] = arr[j]^arr[j-1]; theFlag =true;//If no exchange occurs, the previous digit of each pair is no larger than the latter. No need to sort again - }Wuyi } the } - } Wu}
2, simple selection of the sort, with the bubble the first idea of the same. The difference is not to exchange each time, but to record the most value of the subscript position, the last exchange of each trip
1 Public classSimpleselectsort {2 3 //Simple bubbling upgrade, inner and outer loop unchanged, assignment n times, Exchange once4 Public Static voidSimpleselectsort (int[] arr) {5 intmin;6 for(inti =0; i < arr.length-1; i++) {7Min =i;8 for(intj = i+1; J < Arr.length; J + +) {9 if(Arr[min] >Arr[j]) {TenMin =J; One } A } - if(min! =i) { -Arr[min] = Arr[min] ^Arr[i]; theArr[i] = Arr[min] ^Arr[i]; -Arr[min] = Arr[min] ^Arr[i]; - } - } + } -}
3, the direct insertion method, this idea is very good, similar to the smooth card.
1 Public classStraightinsertionsort {2 3 //the method is taken from the second position, if it is smaller than the front, the front shift, imagine the card 5,3,4,6,2, flush4 Public Static voidStraightinsertionsort (int[] arr) {5 inttemp;6 for(inti =1, j =0; i < arr.length; i++) {7 if(arr[i]<arr[i-1]) {8temp = Arr[i];//Remove Card9j = I1;//to assign an initial value to the inner layer loopTen while(j>=0&& Arr[j] >temp) { Onearr[j+1] = Arr[j];//a big shift from the card Aj--; - } -arr[j+1] = temp;//Insert Card the } - } - } -}
Java Learning notes-sorting algorithm O (n²) sorting