Sorting is based on the use of external memory into internal and external sorting, the internal sort uses only memory for data storage, the external ordering because of the large amount of data need to rely on external memory.
Stability of the sort: the stability of the sort means that the same data elements are sorted after the same relative positions are stable, otherwise the order is unstable.
Insert Sort Direct Insert Sort
Thought: Inserting a record into an ordered table that has been sorted, thus obtaining a new, 1 ordered table. This process records movement while locating the location, without displaying the interchange elements
Complexity of Time: O (n^2)
Space complexity: O (1)
Stability: Stable sequencing
public void Insertsort (int[] a) {int j;for (int i= 1;i<a.length;i++) {int tmp = a[i];for (j=i;j>0 && Compare (a[ J-1],TMP); j--) A[j] = a[j-1];a[j] = tmp;}} Private Boolean compare (int a,int b) {return a>b; }
Binary Insert Sort
Thought: The process of directly inserting a sort is to find and compare, if the number of comparisons is relatively low, its efficiency will also improve, because it is necessary to find the insertion position in the sequenced sequence, so use binary to find a lower number of lookups
Complexity of Time: O (n^2)
Space complexity: O (1)
Stability: Stable sequencing
public void Binsertsort (int[] a) {int j;for (int. i=1;i<a.length;i++) {int tmp = A[i];int Low = 0;int High = i-1;while (low < high) {int mid = (low + high)/2;if (TMP < A[MID]), high = Mid-1;elselow = mid + 1;} for (j=i;j>low;j--) a[j] = a[j-1];a[j] = tmp;}}
Hill sort
Thought: On the basis of the insertion sort, because of the insertion sort if the sequence is basically orderly, his efficiency will be very high, if the direct order of its time complexity is O (n); so the hill sort divides the waiting sequence into several sub-sequences, inserts the sort separately, and then makes the whole sequence in order. The time complexity of the hill sort depends on its increment sequence.
Time complexity: Up to O (N3/2)
Space complexity: O (1)
Stability: unstable (jumping insert)
public void Shellsort (int[] a) {int j;int length = a.length;for (int. skp=length/2;skp>0;skp/=2) {for (int i=skp;i< length;i++) {int tmp =a[i];for (j = i;j>=skp && compare (a[j-skp],tmp); j-=skp) a[j] = a[j-skp];a[j] = tmp;}}} Private Boolean compare (int a,int b) {return a>b;}
I am using the hill increment sequence, he is not the best increment sequence, for example, when its number is a power of 2, each time its SKP is even digit, if the even number is the square maximum, the cardinality is the minimum value, then the hill sort does not have any effect, the increment sequence should guarantee its adjacent increment coprime, the minimum value is 1
Swap sort Bubble sort
Thought: Compare the first record with the second record if it is in reverse order, swap its position, and then compare the second to the third record until the n-1 item is compared to the nth item. This process is a bubble sort, select the maximum value, and then continue to compare the first n-1 values, and so on. The entire sort process requires a n-1 bubble sort.
Complexity of Time: O (n^2)
Space complexity: O (1)
Stability: Stable sequencing
public void Bubblesort (int[] a) {for (Int. i=0;i<a.length-1;i++) for (int j=0;j<a.length-i-1;j++) if (A[j] > a[j+1] ) {int tmp = A[j];a[j] = a[j+1];a[j+1] = tmp;}}
Quick Sort
Thought: The improvement of bubble sort, a sort of sorting the sequence to be sorted into two parts, some of which are larger than the other key words, and then the two parts of the sequence continues to be sorted to achieve the entire sequence order. Its implementation needs to find a pivot element that compares the sequence to the pivot element, less than the pivot element on one side, and greater than the pivot element on the other side. The selection of the pivot element is very important, generally is the first element of the selection sequence, but there is a problem, when the sequence is a positive or reverse order, all elements will tend to one side, so throughout the recursive process, so that its efficiency underground. You can select the first element of the sequence to be ordered, the middle element, the last element to compare, and then select the middle value as the pivot element.
Complexity of Time: O (NLOGN)
Space complexity: O (1)
Stability: Unstable sorting
public void QuickSort (int[] A,int left, int. right) {int dp;if (left < right) {DP = partition (A, left, right); QuickSort (A, Left, dp-1); QuickSort (A, dp+1, right);}} private int partition (int[] A,int left, int. right) { int pivot = a[left]; while (Left < right) { while (left < right && A[right] >= pivot) right--; if (left < right) & nbsp; a[left++] = a[right]; while (Left < right && A[left] < pivot) left++; if (left < right) a[right--] = a[left]; } a[left] = pivot; return left; }
Select Sort Simple Select sort
Thought: Each time the smallest element is selected from the N-i record as the first record in an ordered sequence
Complexity of Time: O (n^2)
Space complexity: O (1)
Stability: Unstable sorting
public void SelectSort1 (int[] a) {for (int i=0;i<a.length;i++) {int k = i;for (int j=i+1;j<a.length-1;j++) {if (A[j] < a[k]) k = j;} if (k! = i) {int tmp = A[i];a[i] = a[k];a[k] = tmp;}}}
Heap Sort
Thought: The idea of heap sequencing is to treat the sequence as a complete binary tree, the heap is divided into large piles and small heaps, Dagen means that the root node of each subtrees tree is larger than the root node of its left and right subtree, the opposite of small Gan. The process by which the Liegian is to be sorted is the process of filtering the maximum value, finding the maximum value and storing the value in the last position of the array to be sorted length-1, and the next maximum is placed in the length-2 position. Since it is a fully binary tree, the left and last non-terminal node is N-1/2 (since the array subscript starts with 0), starting with this value and selecting the maximum value from the left and right subtrees to push upward. Since the sequence of Dagen is from small to large, if it is necessary to have a positive order, then construct a large root heap, and construct a small Gan if you need to reverse it.
Complexity of Time: O (NLOGN)
Space complexity: O (1)
Stability: Unstable sorting
public void Heapsort (int[] a) {for (int i=0;i<a.length;i++) {createmaxheap (a,a.length-i-1); swap (a,i,a.length-i-1);}} private void Createmaxheap (int[] A,int lastIndex) {for (int i= (lastIndex-1)/2;i>=0;i--) {int k = i;//records the current node while (2*k+1 <= lastIndex) {//To determine if the child node exists int bigindex = 2*k+1;//record the maximum node, the default assignment is left child node if (Bigindex < LastIndex) {//To determine if there is a right child if (a[ Bigindex +1] > A[bigindex])//Find the maximum value of the child node bigindex++;} if (A[k] < A[bigindex]) {//To determine the maximum value of the child node compared to the root node value swap (a,k,bigindex); k = Bigindex;} Elsebreak;}}} private void Swap (int[] A,int i,int j) {int tmp = A[i];a[i] = a[j];a[j] = tmp;}
Merge sort
Thought: Merging two or more two ordered tables into a new ordered table. If there are N records to be ordered sequence, first divides it into N subsequence, then 22 merges to obtain the N/2 subsequence, then 22 merges, repeats executes. Until a sequential sequence of length n is obtained, this sort method is called 2-way merge sort;
Complexity of Time: O (NLOGN)
Space complexity: O (N)
Stability: Stable sequencing
public static void MergeSort (int[] a) {sort (a,0,a.length-1), for (int i:a) {System.out.println (i);}} private static void sort (int[] A, int left,int right) {if (left < right) {int center = (left + right)/2;sort (a,left,center ); sort (a,center+1,right); merge (a,left,center,right);}} private static void merge (int[] A,int left,int center,int right) {int[] Tmparr = new Int[a.length];int mid = Center+1;int C Ount = left;int tmp = Left;while (left <=center && mid <= right) {if (A[left] <= A[mid]) tmparr[count++] = a[ left++];elsetmparr[count++] = a[mid++];} while (left<=center) tmparr[count++] = A[left++];while (mid<=right) tmparr[count++] = a[mid++];while (tmp <= right) a[tmp] = tmparr[tmp++];}
Bucket sort
Thought: For the use of bucket sorting, create an array, the array size is the minimum and maximum values of the elements to be sorted, use the array subscript to store the sequence to be ordered, and then use the formula A[i] = A[i] + a[i-1], and calculate the position of each record in the sequence. Bucket sequencing needs to ensure that the range of the backlog is approximately the minimum value of the maximum value if the minimum difference is much greater than the number of elements, then the ancillary space costs a lot.
Time complexity: O (N+M) m for range
Space complexity: O (N)
Stability: Stable sequencing
public void Bucketsort (int[] a,int max,int min) {//cache data int[] tmp = new int[a.length];//bucket array, his size should be the maximum value in the given array minus the minimum int [] Buck ETS = new int[max-min];//each record into the bucket for (int i=0;i<a.length;i++) buckets[a[i]-min]++;//The position of the elements in the bucket in the ordered sequence for (int i=0;i <max-min;i++) Buckets[i] = Buckets[i] + buckets[i-1];tmp = arrays.copyof (A, a.length); for (int i=a.length-1;i>=0; i--) a[--buckets[tmp[i]-min]] = tmp[i];}
Base sort
Thought: The Cardinal sort is a sort of thought with the help of multi-keyword sorting. He breaks down the pending records into multiple keywords and sorts them according to the sub-keywords. There are two sorting methods for multi-keyword ordering, the highest bit priority method (MSD) and the lowest bit precedence method (LSD), which is divided into three keyword hundred, 10 bits, and bits according to the number of digits of the keyword, such as 123. The computer is more suitable for the LSD least bit precedence method, and the sorting between the keywords requires another stable sort, and if the pending sequence is an integer, then each bit is between [0-9], so you can use bucket sorting.
Time complexity: dependent on sub-keyword sorting algorithm
Spatial complexity: Relying on the key sorting algorithm
Stability: Stable sequencing
Radix=10,d is the number of bits public void Radixsort (int[] a,int radix, int d) {//Create cache data int[] tmp = new int[a.length];// Buckets record sorting element information int[] Buckets = new int[radix];//total number of sorted digits for (int i=0,rate=1;i<d;i++) {//Reset count array Arrays.fill (A, 0 );//copy buffered Data TMP = ARRAYS.COPYOF (A, a.length);//Put each bit in the bucket for (int j=0;j<a.length;j++) {int subkey = (a[j]/rate)%radix; buckets[subkey]++;} Calculates the position of an element in a bucket in a sequence for (int j=1;j<radix;j++) buckets[j] = Buckets[j] + buckets[j-1];//Sorts the specified data by sub-key for (int j=a.length-1 ; j>=0;j--) {int subkey = (a[j]/rate)%radix;a[--buckets[subkey]] = tmp[j];} Rate *=radix;}}
Summarize:
o (N3/2)
| algorithm |
time complexity |
Spatial complexity |
stability |
| Direct Insert sort |
o (n^2) |
O (1) |
stable |
| binary Insert sort |
o (n^2) |
O (1) |
stable |
| Hill sort |
O (1) |
unstable |
| bubble sort |
O (n^2) |
O (1) |
stable |
| Quick sort |
O (nlogn) |
O (1) |
unstable |
| Simple Selection Sort |
O (n^2) |
O (1) |
unstable |
| heap sort |
o (nlogn) |
O (1 |
not stable |
| merge sort |
O (nlogn) |
o (n) |
stable |
| Bucket Sort |
O (k+n) |
O (k+n) |
Stable |
The cardinality sort relies on the sub-keyword sorting algorithm to rely on the sub-keyword sorting algorithm stable
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Data structure-Sort summary