Classification:
1) Insert sort (direct insert sort, hill Sort)
2) Swap sort (bubble sort, quick sort)
3) Select sort (Direct select sort, heap sort)
4) Merge sort
5) Allocation sort (cardinal sort)
Maximum required auxiliary space: merge sort
Minimum required secondary space: heap Ordering
Fastest Speed: Fast sorting
Unstable: Quick sort, hill sort, heap sort.
1, Bubble algorithm (bubble)--is the exchange sort----o (n^2)
22 The adjacent elements are compared, and the interchange is not met. Until finally there are no elements to interact with. A trip to sort down, the largest element at the tail, the smaller the element will slowly "float" through the exchange to the top of the series.
The bubbling sorting algorithm works as follows:
- Compares the adjacent elements. If the first one is bigger than the second one, swap them both.
- Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. When this is done, the final element will be the maximum number.
- Repeat the above steps for all elements, except for the last one.
- Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.
Coding
Public Static void bubblesort (int[] arr) { int i, J, temp, len = arr.length; for (i = 0; i < len-1; i++) { for (j = 0; J < len-1-I, J + +) { if
(Arr[j] > arr[j + 1]) { = arr[j]; = Arr[j + 1]; + 1] = temp; }}}}
Optimized bubbling:
Since it may have been sequenced in the first few times, it still needs to be traversed in the last bubble sort. Worst case for O (n^2)
Optimization: Sets a flag that is true if the interchange occurred, otherwise false. Obviously if there was a trip without an exchange, the description sort has been completed. The worst case can be optimized to O (n)
Public voidBubbleSort1 (int[] a) {inttemp = 0; intLen =a.length; BooleanFlag =true; while(flag) {flag=false; for(intj = 1; J < Len-1; J + +) if(A[j-1] >A[j]) { //Note whether a[j-1] or a[j] is prone to boundary problems//sort from small to largetemp = A[j-1]; A[j-1] =A[j]; A[J]=temp; //set Flag bitFlag =true; } } }View Code
2. Direct insertion
Basic idea: 将需要排序的关键数与前面已经排好序的数据从后往前进行比较,使其插入到合适的位置 .
The insertion sort is efficient when the data operation is almost already ordered, that is, the efficiency of linear sequencing can be achieved;
Coding
Public Static voidInsertsort (int[] Array) { for(inti = 1; i < Array.Length; i++) { inttemp =Array[i]; intj = I-1; for(; J >= 0 && array[j] > temp; j--) { //move the value greater than temp to one unit after the wholeArray[j + 1] =Array[j]; } array[j+ 1] =temp; } System.out.println (arrays.tostring (array)+ "Insertsort"); }View Code
The bubbling sort is the same run time as the insert sort, but the two algorithms differ greatly in the number of exchanges required. In the worst case, the bubble sort requires a secondary exchange, while the insertion sort is exchanged as much as possible . The implementation of the bubbling sort (similar to the following) usually runs poorly on the sequenced sequence (), whereas the insertion order in this example requires only {\displaystyle O (n)} operations. As a result, many modern algorithmic textbooks avoid using bubble sorting and replace them with insertion sorting.
3.
8 Sorting Algorithms-I know (bubbling, direct insertion)