Sorting algorithm is a basic algorithm, often asked, some companies interview may also be in the examination, here deliberately all the sorting algorithm to do a summary
1. Bubble sort
//bubble sort, two loops start bubbling from 0 Public int[] Maopao (int[] Array) { intLen =Array.Length; for(intI=0; i<len-1; i++){ for(intj=0; j<len-i-1; j + +){ if(array[j]>array[j+1]){ inttemp =Array[j]; ARRAY[J]= array[j+1]; Array[j+1] =temp; } } } returnArray; }
The most basic sort algorithm always bubbles the largest to the right, always comparing two adjacent elements.
Average Time complexity O (n^2), best time O (n), Worst time O (n^2), Space complexity O (1), no additional space required
Stable algorithm, because the bubble sort is always compared with two adjacent elements, so it is stable.
2. Direct Insert Sort
//Direct Insert Sort Public int[] Charu (int[] Array) { intLen =Array.Length; for(inti=1;i<len;i++){ inttemp =Array[i]; intJ=i-1; while(J>=0 && temp<Array[j]) {//All elements are moved back array[j+1]=Array[j]; J--; } array[j+1] =temp; Enter and leave the element}returnArray; }
The first element considers the ordered, followed by each element to be inserted in the sorted array, and all the other elements move backwards one
Average Time complexity O (n^2), best time O (n), Worst time O (n^2), Space complexity O (1), no additional space required
Stable algorithm, because the entry and exit sorts are also adjacent to each other and then move back, so it is also stable
3. Select sort
//Choose the sort, and each time you find the smallest row . Public int[] Selectsort (int[] Array) { intLen =Array.Length; for(inti=0;i<len;i++){ intindex =i; for(intj=len-1;j>i;j--){ if(array[j]<Array[index]) {Index=J; } } inttemp =Array[i]; Array[i]=Array[index]; Array[index]=temp; } returnArray; }
Each cycle, find the smallest element and place it in each loop.
The average time complexity O (n^2), the best time O (n^2), regardless of the need to cycle to determine the smallest element, the worst time O (n^2), Space complexity O (1), do not need additional space
An unstable sorting algorithm, because each time you need to choose to do, and is selected location inserted.
4. Quick Sort
//quick sorting ideas, each time the first one for base, then the small left, big put on the right, recursion on both sides Public int[] QuickSort (int[] Array,intLeftintRight ) { if(left<Right ) { intBase =Array[left]; inttemp; intI=left, j=Right ; Do{ while(Array[i]<base && i<Right ) {i++; } while(Array[j]>base && j>Left ) {J--; } if(i<=j) {Temp=Array[i]; Array[i]=Array[j]; ARRAY[J]=temp; I++; J--; } } while(i<=j); if(left<j) {QuickSort (ARRAY,LEFT,J); } if(right>i) {QuickSort (array,i,right); } } returnArray; }
Each time, the first left element is used as the base base, and then it starts on both side and right, and the small one is placed in the lower half, and the large one is placed in the lower part. Then recursively sort the left and right until Left==right stops
The average time complexity O (Nlog (n)), the best time O (Nlog (n)), the worst Time O (n^2), the space complexity is generally o (log (n)), but also can reach O (1), such as the above code, there is no additional space to open.
Fast sorting is also an unstable sorting algorithm
5. Merge sort
//Merge Sort Public int[] MergeSort (int[] Array,intLeftintRight ) { intMid = (left+right)/2; if(left<Right ) {mergesort (Array,left,mid); MergeSort (Array,mid+1, right); Merge (Array,left,mid,right); } returnArray; } Public voidMergeint[] Array,intLeftintMidintRight ) { int[] temp =New int[Right-left+1]; intK = 0; intI=left;//left intj=mid+1;//Right//Merging while(I<=mid && j<=Right ) { if(array[i]<Array[j]) {Temp[k]=Array[i]; K++; I++; }Else{Temp[k]=Array[j]; I++; J++; } } //left remaining while(i<=mid) {Temp[k]=Array[i]; K++; I++; } //left side while(j<=Right ) {Temp[k]=Array[j]; K++; I++; } //copied to the original array for(intii=0;ii<temp.length;ii++) {Array[left+ii]=Temp[ii]; } }
Using the idea of divide and conquer, first an array is constantly split, always split to each has only one element, and then call the merge algorithm, and constantly merge two already sorted array, know the final row order
The average time complexity O (Nlog (n)), the best time O (Nlog (n)), the worst Time O (Nlog (n)), the space complexity is generally o (log (n)), the merge sort is required for additional application space, each time a new array needs to be generated.
The merge sort is stable because it is always first divided into the smallest array and then merged.
6. Hill Sort
Essence is the combination of divide and conquer thought and direct insertion sorting algorithm
Average Time complexity O (n^ (1.3)), best time O (n), Worst Time O (n^ (2)), spatial complexity is generally o (1)
An unstable sorting algorithm
7. Heap Sequencing
The essence is to use the smallest heap or the largest heap to sort (binary tree), the first need to build the heap, and then each time a minimum element or the largest element, you need to re-adjust the heap
Average Time complexity O (Nlog (n)), best time O (Nlog (n)), Worst Time O (Nlog (n)), spatial complexity is generally o (l)
An unstable sorting algorithm
8. Base sorting, algorithm based on bucket ordering
(1) The bucket is sorted first by the single digit, and then the elements in the bucket are obtained according to 0-9
(2) Sort the buckets according to 10 digits and then follow 0-9 to get the elements in the bucket
(3) .....
(4) always reach the highest position, the result will be the final sorting result
9. Sorting Buckets
The practice of exchanging space for time, given a large range of buckets, each element in a bucket, can be used only once in a cycle
Time complexity O (n)
Sorting algorithm Summary