The characteristics and differences between sorting and bubbling sorting
++++++++++++++++++++++++++++++++++++++++++++++
| Select sort |
This kind of simple sorting method, its basic idea is: R[n] The first time from r[0]~r[n-1] to select the minimum value, and r[0] exchange; The second time from r[1]~r[n-1] to select the minimum value, and r[1] exchange; ...., (i) Select the minimum value from r[i-1]~r[n-1] and swap with r[i-1]; ....., (n-1) Select the minimum value from r[n-2]~r[n-1] and swap with r[n-2]; A total of n-1 times, to get a sort code from small to large ordered sequence. |
| Characteristics |
|
1 for(inti = 0;i<arr.length;i++){2 3 for(intJ= 1; j<arr.length;j++){4 if(arr[j-1]>Arr[j]) {5 inttemp =Arr[j];6ARR[J] = arr[j-1];7ARR[J-1] =temp;8 9 }Ten } One}
+++++++++++++++++++++++++++++++++++++++++++++++
| Bubble sort |
Compare adjacent two numbers in turn, place decimals ahead, and large numbers behind . That's the first trip: First, the 1th and 2nd numbers are compared, the decimals are placed before the large number is placed; Then compare the 2nd and 3rd numbers, put the decimal before the large number, so continue until the last two numbers are compared, the decimal place before the large number of places; At the end of the first trip, the largest number was put to the last. On the second trip: The comparison is still started from the first logarithm (because the 1th number is no longer less than the 2nd number because of the 2nd and 3rd number of interchanges), the decimal place is placed before the large number; Always compare to the second-to-last number (which is already the largest in the penultimate position); At the end of the second trip, we get a new maximum number in the penultimate position (in fact the second largest number in the whole sequence).
|
| Characteristics |
The average time complexity of bubble sorting is the same as the insertion sort, and is also a square-level, but also very easy to implement algorithm. |
1 for(intI =0;i<arr.length-1;i++) {//Traverse from the first start2 3 for(intj=0;j<arr.length-1-i;j++) {//4 5 if(Arr[j]>arr[j+1]) {//the previous number is larger than the number behind.6 inttemp = Arr[j];//and then swap, and the big back row7ARR[J] = arr[j+1];8ARR[J+1] =temp;9 }Ten } One}
Mo mo says: Select sort (include shaker sort/heap sort) and bubble sort are all part of exchange sort.
Some sort algorithms for computers: Insert Sort, bubble sort, select sort, quick sort, heap sort, merge sort, base sort, Hill sort
"Each sorting algorithm has an average time complexity"
Java Select sort and bubble sort