Seven sorts of algorithms

Source: Internet
Author: User
Tags sorts

Insert Sort
public static void Insertsort (int["T) {for (int i=1; i<t.length; i++) {//To extract number I of int temp = T[i];int j;for (j=i-1; j>= 0 && temp<t[j]; j--) {//assuming that the extracted number is less than t[j], the number in the data is shifted backwards. T[J+1] = t[j];} The number extracted is greater than or equal to t[j]. INSERT into t[j] (i.e. t[j+1]) t[j+1] = temp;}}

Time complexity: The best case is O (n); the worst case is: O (n^2), which is stable.


Hill sort
public static void Shellsort (int[] t) {//shrinks the position between the data until it becomes an insert sort for (int delta=t.length/2; delta>0; delta/=2) {// The principle and the insertion sort,//is simply to change 1 to Delta just for (int i=delta; i<t.length; i++) {int temp=t[i];int j;for (j=i-delta; j>=0 && temp<t[j]; J-=delta) {T[j+delta] = t[j];} T[j+delta] = temp;}}}

The basic idea is to group the direct insertion sort. The time complexity is: O (n (㏒2n) 2). The 2 refers to the Subscript 2 (hereinafter), which is unstable.

Bubble sort
public static void Bubblesort (int[] t) {//whether to swap tokens for Boolean exchange = true;//once per loop, you get a maximum value for (int i=1; i<t.length && Exchange; i++) {exchange = false;for (int j=0; j<t.length-i; J + +) {if (t[j]>t[j+1]) {int temp = t[j+1];t[j+1] = t[j];t[j] = Temp;ex Change = True;}}}

The basic idea is that the keyword value of the adjacent two elements, assuming the inverse order. is exchanged.

The assumption is that in ascending order, each trip will swap the largest element in the scanned data series to the last position. It's like bubbles popping out of the water.

Time Complexity: Best case: O (n). Worst case: O (N2), is stable

High-speed sequencing
public static void QuickSort (int[] t) {quickSort (t,0,t.length-1);}  private static void QuickSort (int[] t, int begin, int end) {//infer whether the sequence is valid if (begin<end) {int I=begin, j=end;//extract datum value int vot = T[i];while (I!=J) {//from behind looking for smaller value while (i<j && vot <= t[j]) {j--;} if (i<j) {//Small values move forward t[i++] = t[j];} Look from the front for larger values while (i<j && t[i]<=vot) {i++;} if (i<j) {//Large value moves backward t[j--] = T[i];}} Determine the position of the base value t[i]=vot;//the pre-terminal sequence in the sort, recursive call to Quicksort (t,begin,j-1);//post-terminal sequence in sort, recursive call quicksort (T,i+1,end);}}

The basic idea is to select a value in the data series as a comparative reference value. Each trip starts alternating from each end of the data sequence, exchanging elements that are less than the base value to the sequence front end, and exchanging elements that are larger than the base value to the sequence backend. A position in between is the last position of the base value.

At the same time, a sequence is divided into two sub-sequences. The same method is used to sort the two sub-sequences separately. The sort is completed until the length of the subsequence is 1.

Time Complexity: Best case: O (n*㏒2n), worst case: O (N2), unstable.


Direct Select sort
public static void Selectsort (int["T) {for (int i=0; i<t.length-1; i++) {//mark the minimum value of the bits int min=i;//traverse to find the minimum for (int j=i+1; j& Lt;t.length; J + +) {if (T[j]<t[min]) {min=j;}} Swap the minimum value to the previous if (min!=i) {int temp = T[i];t[i] = t[min];t[min] = temp;}}}

Basic idea: The first trip selects the smallest (or largest) element of the keyword from the data sequence of n elements and puts it in the first (or last) position, and the next pass selects the smallest (large) element from the n-1 element and places it in the pre (post) position.    And so on, after the n-1 trip finished sorting. Time Complexity: Best case: O (N2), worst case: O (N2), unstable.

Heap Sort
The subtree with the begin root is adjusted to the minimum heap private static void Sift (int[] t,int begin, int end) {//j is the left child int i=begin of the I node, and the value int of the j=2*i+1;//sub-tree root node Temp=t[i];while (j<=end) {//compare children. Position J to the smallest if (j<end && t[j]>t[j+1]) {j + +;} if (Temp>t[j]) {///Joz node value is large,//small child node up t[i]=t[j];//make i,j down a layer i=j;j=2*i+1;} Else{break;}} The position of the current subtree after the original root value is adjusted t[i]=temp;} public static void Heapsort (int[] t) {int n=t.length;//create minimum heap for (int j=n/2-1; j>=0; j--) {sift (t,j,n-1);} Each trip will exchange the minimum value to the back. Re-adjust piles for (int j=n-1; j>0; j--) {int temp = t[0];t[0] = t[j];t[j] = Temp;sift (t,0,j-1);}}

Basic idea: First and direct selection of the sort comparison. In the direct selection sort. Each trip from the data sequence to choose a minimum value exchange to the front, the remaining n-1 elements remain in place, the next trip needs to compare these elements again, so the direct selection of the sorting algorithm is much more repetitive. Assuming that each trip can take advantage of the comparison of the previous trip, it will reduce some of the repetition, thus improving the efficiency of the algorithm.

Heap ordering is the use of the full binary tree, each trip only need to traverse a path in the tree. Rather than all elements.

Time complexity: The best worst case scenario: O (N*㏒2N). is not stable.

Merge sort
The operation of the subsequence element,//m and R respectively, are the starting subscript for the adjacent two sorted subsequence,//n for the length of the subsequence private static void merge (int[] X, int[] Y, int m, int r, int n) {int i=m, J =r, k=m;//merges two contiguous sub-sequences in X into y while (i<r && j<r+n && j<x.length) {if (X[i] < X[j]) {//copy smaller values into Y y[k + +] = x[i++];} else{y[k++] = x[j++];}} Copies the remaining elements of the previous subsequence to Y while (I<r) {y[k++] = x[i++];} Copy the remaining elements of the latter subsequence into Y while (J<r+n && j<x.length) {y[k++] = x[j++];}}  Operation of a child sequence private static void Mergepass (int[] X, int[] Y, int n) {int i = 0;while (i < x.length-n*2+1) {merge (X, Y, I, I+n, n); i + = 2*n;} Operation if (I+n < x.length) {///for the remainder of the subsequence that is not enough 2n) {//Although the latter subsequence is not of the length n. But I can still do it again. Mergemerge (X, Y, I, I+n, n);} else{for (int j=i; j<x.length; J + +) {y[j]=x[j];}}} Operation of an array public static void MergeSort (int[] X) {int[] Y = new Int[x.length];int n = 1;//A while loop completes two times and is returned,//data series from X to Y, then y to x,// Causes the sorted data sequence to still be in array X while (n<x.length) {mergepass (x,y,n); N*=2;mergepass (y,x,n); n*=2;}}

The basic idea: the data sequence of n elements can be considered to consist of a sequence of n-length 1, repeating that the adjacent two sub-sequences are merged into a sorted subsequence until they are combined into a sequence, the sort is complete.    Time complexity: The best worst case scenario is: O (n*㏒2n). is stable.


Test method
public static void Main (string[] args) {//Create an array int[] y = new int[10];//Assign a value to the array for (int i=0; i<y.length; i++) {int a = new Random (). Nextint (+); y[i] = A;} Run the Sort method Selectsort (y);//print sorted array for (int i=0; i<y.length; i++) {System.out.println (y[i]);}}


If the reader has any better suggestions or questions, please feel free to share your progress with the following comments.







Seven sorts of algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.