The sorting method in data structure

Source: Internet
Author: User

    1. Bubble sort

Bubble sort is one of the simplest sorting methods in the sorting algorithm, by comparing two adjacent data, moving the larger of the adjacent data (back). The first trip in A[0]-a[n-1] in the past, the previous two adjacent elements of the comparison, if the latter small exchange, compare n-1 times; The first pass compares the end, the largest element determines the position, and the next trip continues with the remaining n-1 elements. For example: Initial sequence (48,36, 68,72,12,48,02), first comparison (48,68, 72,12,48,02), second comparison (36,48,68,72, 12,48,02), third comparison (36,48,68,72,12, 48,02), fourth time comparison (36,48,68,12,72,48, 02), fifth time comparison (36,48,68,12,48 , 72,02), 6th time comparison (36,48,68,12,48,02,) 72. The above is only a comparison of the outer loop, each time the inner loop is 22 compared. in the best case, the time complexity is O (n) and the worst-case time complexity is

O (n2). The execution time of the algorithm is related to the initial arrangement of elements. The bubble sort is sequenced to determine the final position of an element . Bubble sort is a stable sorting method. The code uses Java implementations.

public class Bubblesort {public static void Bubblesort (int[] arr) {if (arr = = NULL | | Arr.length = = 0) return;for (int i = 0;i<arr.length-1;i++) {for (int j = arr.length-1;j>i;j--) {if (Arr[j-1]>arr[j] {swap (ARR,J-1,J);}}} public static void Swap (int[] Arr,int i,int j) {int temp = Arr[j];arr[j] = arr[i];arr[i] = temp;} public static void Main (string[] args) {//TODO auto-generated Method stub       int[] a = {33,15,54,64,63};       Bubblesort bubble= new Bubblesort ();       Bubblesort (a);       for (int i = 0;i<a.length;i++) {           System.out.println ("Sort:" +i+ "a" +a[i]);}}       

2. Select sort

The basic idea of simple sorting is to use the initial sequence (a[0]~a[n-1]) as the sequence to be sorted, the first to find the smallest element in the sequence to be sorted, to exchange it with the first element of the column A[0], so that the sequence a[0] is ordered, and the next to sort the subsequence (a[1]~a[n-1] ). I-trip sort in the subsequence to be sorted (a[i]~a[n-1], the minimum element is found, and the first element in the subsequence is a[i-1] exchanged. The order of the initial sequence is ordered after n-1. Simple selection The execution time of a sort is independent of the initial arrangement of elements. Regardless of the initial arrangement, the algorithm must perform a n-1 trip, performing a comparison of n-i-1 sub-keywords per trip, so that the number of comparisons is n (n-1)/2 times. Therefore, the best, worst, and average time complexity of a simple selection is O (N2), after which the algorithm can determine the final position of an element. Simple selection sorting is an unstable sort method.

 Public classSelectsort { Public Static voidSelectint[] arr) {        if(arr = =NULL|| Arr.length = = 0){            return; }        intMinindex = 0;  for(inti = 0;i<arr.length-1;i++) {Minindex=i;  for(intj = arr.length-1;j>i;j--){                if(arr[j]<Arr[minindex]) {Minindex=J; }            }            if(I! =Minindex)            {swap (Arr,i,minindex); }        }      }     Public Static voidSwapint[] arr,intIintj) {        inttemp =Arr[i]; Arr[i]=Arr[j]; ARR[J]=temp; }     Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        int[] A = {5,8,3,4,6};        Select (a);  for(inti = 0;i<a.length;i++) {System.out.println ("Sort" +i+ "a" +A[i]); }    }


3. Direct Insert Sort

The idea of direct insertion is very simple, the first element in the sequence is used as an ordered sequence, and then the remaining n-1 elements are inserted into the ordered sequence by keyword size, and each element remains ordered after each insertion, and is ordered after the n-1 sequence.

When you insert an element into an ordered table, it is first stored in the temporary variable target, and then the insertion position is looked up from behind. When target is greater than or equal to the element in the table or to the end of the first element of an ordered table, the target is placed in the position of the element that was just moved back. The direct insertion ordering method must be n-1, preferably O (n), and the worst case time complexity O (n2). Direct insertion Sorting cannot determine the final position of any one element until the end of the sort. The algorithm is a stable sorting algorithm. If you change the arr[j-1] > target condition to "arr[j-1" > =target "the algorithm becomes unstable (the unstable algorithm ignores the order of the elements of the same value in the ordering).

 Public classInsertsort { Public Static voidInsertint[] arr) {        if(arr = =NULL|| Arr.length = = 0){            return; }         for(inti = 1;i<arr.length;i++){            intj =i; inttarget = Arr[i];//The value of the back bit has been saved into the target             while(J > 0 && arr[j-1] >target) {Arr[j]= Arr[j-1];//Rear shiftj--; } Arr[j]= target;//assigns the previous value of the save to the target        }    }     Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        int[] A = {5,3,8,6,4};        Insert (a);  for(inti = 0;i<a.length;i++) {System.out.println ("Sort" +i+ "a" +A[i]); }    }}

4. Quick Sort

The idea of quick sorting is that for an element r in any given sequence, after a stroke, the original sequence is divided into two sub-sequences (RP (0),..., RP (S-1)) and (RP (s+1),..., RP (n-1)). The key value for all elements in the previous subsequence is less than or equal to the key value KP (s), and the key values for all elements in the latter subsequence are greater than the keyword value KP (s), and then the low-end sequence and high-side sequence are sorted quickly until the subsequence is empty or has only one element.

When the initial sequence is ordered, the efficiency of fast sorting is the lowest and the time complexity is O (N2). In the average and best case, the time complexity of the quick Sort is O (nlog2n). The fast sorting algorithm is an unstable algorithm.

 Public classQuickSort {//One Division     Public Static intPartition (int[] arr,intLeftintRight ) {        intProkey =Arr[left]; intProindex =Left ;  while(left<Right ) {             while(Left<right && arr[right]>=Prokey) right--;  while(Left<right && arr[left]<=Prokey) left++; Swap (arr,left,right);//turn the big swap to the right and the small swap to the left} swap (arr,proindex,left);//Finally, swap the prokey in the middle .        returnLeft ; }         Public Static voidQuickSort (int[] arr,intLeftintRight ) {        if(left>=Right )return; intProindex =Partition (arr,left,right); QuickSort (Arr,left,proindex-1); QuickSort (Arr,proindex+1, right); }     Public Static voidSwapint[] arr,intLeftintRight ) {        inttemp =Arr[left]; Arr[left]=Arr[right]; Arr[right]=temp; }          Public Static voidMain (string[] args) {//TODO auto-generated Method Stub                 int[] A = {33,15,54,64,63}; QuickSort (A,0,a.length-1);  for(inti = 0;i<a.length;i++) {System.out.println ("Sort:" +i+ "a" +A[i]); }

To be continued!!!!!!!!!!!!!!!!!!!

The sorting method in data structure

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.