# Algorithm (i): sort

Source: Internet
Author: User
Tags sorts

Categories with large sorting can be divided into two types: inner and outer sort. In the sort process, all records are stored in memory, which is called an inner sort, and is called an out-of-order if you need to use external memory during sorting. The sort that follows is an inner sort.

Within a sort can be divided into the following categories:

(1), insert Sort: Insert sort , dichotomy insert sort, hill sort.

(2), select Sort: Select sort , heap sort.

(3), Exchange sort: bubble sort , quick sort .

(4), merge sort

(5), base sort

First, insert sort

• Thought: Each step inserts a backlog of records into the appropriate position of the previously sorted sequence of characters by their sequential code size until all insertions are sorted out. • Key issue: Find the right insertion position in the sequence that is already sorted in front of you. • Method: – Insert Sort – binary insertion sort – hill sort

① Insert sort (insert after finding suitable position from rear)

1, the basic idea: each step of a record to be sorted, by its sequential code size into the previous sorted word sequence of the appropriate position (from the back to find a suitable position) until the full insertion sort.

2. Example

3. Java implementation

` 1 package com.sort; 2 3 public class Direct insert sort {4 5 public static void main (string[] args) {6 int[] a={49,38,65,97,76,13,27,49,78 , 34,12,64,1}; 7 System.out.println ("Before sorting:"); 8 for (int i = 0; i < a.length; i++) {9 System.out.print (a[i]+ ""); 10}11//Direct Insert Sort n for (int i = 1; i < a.length; i++) {13//element to be inserted int temp = a[i];15 int                 J;16/*for (j = i-1; j>=0 && a[j]>temp; j--) {17//will move back one 18 greater than temp                 A[J+1] = a[j];19}*/20 for (j = i-1; j>=0; j--) {21//will move back more than temp one 22                  if (a[j]>temp) {a[j+1] = a[j];24}else{25 break;26 }27}28 a[j+1] = temp;29}30 System.out.println (); Syst Em.out.println ("After sorting:"); (int i = 0; i < a.length; i++) {System.out.print (a[i]+ "); 34}35}36 37} `

4. Analysis

The direct insert sort is a stable sort. For stability analysis of various algorithms, refer to Http://www.cnblogs.com/Braveliu/archive/2013/01/15/2861201.html

When a file is not in the same state, the time it takes to insert the order directly differs greatly. If the initial state of the file is a positive sequence, then each record to be inserted only need to compare once to find the appropriate location to insert, the algorithm time complexity of O (n), then the best case. If the initial state is reversed, then the first I want to insert records need to compare i+1 times to find a suitable location to insert, so the time complexity of O (N2), then the worst case.

The average time complexity of the direct insert sort is O (N2).

② binary Insertion sort (by dichotomy find suitable position insertion)

1, the basic idea: the dichotomy of the idea of inserting a sort of direct insertion, just find the right place to insert a different way, here is the dichotomy to find the right position, you can reduce the number of comparisons.

2. Example

3. Java implementation

` 1 package com.sort; 2 3 public Class Two-step insert sort {4 public static void main (string[] args) {5 int[] a={49,38,65,97,176,213,227,49,78 , 34,12,164,11,18,1}; 6 System.out.println ("Before sorting:"); 7 for (int i = 0; i < a.length; i++) {8 System.out.print (a[i]+ "");          9}10//Two points insert sort one (a); System.out.println (); System.out.println ("After sorting:"); 14 for (int i = 0; i < a.length; i++) {System.out.print (a[i]+ "");}17}18 PR             Ivate static void sort (int[] a) {(int i = 0; i < a.length; i++) {int temp = a[i];22                 int left = 0;23 int right = i-1;24 int mid = 0;25 while (left<=right) {26                 Mid = (left+right)/2;27 if (Temp<a[mid]) {right = Mid-1;29          }else{30 left = mid+1;31}32}33   for (int j = i-1, J >= Left, j--) {a[j+1] = a[j];35}36 if (left! = i) {37 A[left] = temp;38}39}40}41}`

4. Analysis

Of course, the dichotomy insertion sort is also stable.

The number of comparisons for a binary insertion sort is independent of the initial state of the record to be sorted, and depends only on the number of records. When n is large, the maximum number of comparisons is much less than the direct insertion sort. But the minimum number of comparisons is greater than the direct insertion sort. The number of moves of the algorithm is the same as the direct insertion sorting algorithm, the worst case is N2/2, the best case is N, and the average number of moves is O (N2).

③ Hill Sort

1, the basic idea: First take an integer less than n D1 as the first increment, the entire record of the file into D1 groups. All records with a multiple of D1 are placed in the same group. The direct insert sort is performed in each group first, then the second increment d2<d1 repeats the above groupings and sorts until the increment dt=1 (DT<DT-L<...<D2<D1) is taken, that is, all the records are placed in the same group for direct insert sorting. This method is essentially a grouping insertion method.

2. Example

3. Java implementation

` 1 package com.sort; 2 3//Unstable 4 public class Hill sort {5 6 7 public static void Main (string[] args) {8 int[] a={49,38,65,97,7 6,13,27,49,78,34,12,64,1}; 9 System.out.println ("before Sorting:"), ten for (int i = 0; i < a.length; i++) {System.out.print (a[             i]+ ""); 12}13//Hill sort int d = a.length;15 while (true) {d = d/2;17 for (int x=0;x<d;x++) {(int i=x+d;i<a.length;i=i+d) {+ int temp = A[i]                         ; int j;21 for (j=i-d;j>=0&&a[j]>temp;j=j-d) {22             A[j+d] = a[j];23}24 a[j+d] = temp;25}26}27 if (d = = 1) {break;29}30}31 System.out.println (); System . OUT.PRINTLN ("After sorting:"); (int i = 0; i < a.length; i++) {System.out.Print (a[i]+ ""); 35}36}37 38} `

4. Analysis

We know that once the insertion sort is stable, but in different insertion sorts, the same elements may move in their own insert sort, and finally their stability will be disturbed, so the hill sort is unstable.

The time performance of the Hill sort is better than the direct insert sort for the following reasons:

(1) The number of comparisons and movements required to insert a sort directly when the initial state of the file is basically ordered is less.　　(2) When the n value is small, the difference between N and N2 is also small, that is, the best time to insert the order of the complexity O (n) and the worst time complexity 0 (n2) difference is not small. (3) In the beginning of the hill, the increment is larger, more groups, the number of records per group is small, so the direct insertion within the group is faster, the increment di gradually reduced, the number of groups gradually decreased, and the number of records of the group gradually increased, but because already according to Di-1 as a distance arrangement, so that the file is closer to the　　So the new trip sort process is also faster.　　Therefore, the efficiency of hill sort is better than direct interpolation. The average time complexity for hill sorting is O (Nlogn). Second, choose the sort• Thought: Each trip selects the lowest-keyword record from the sequence of records to be sorted and places it in the top position of the sorted table until it is all finished. • Key issue: Find the minimum key code record in the remaining sequence of records to be sorted. • Method: – Select Sort – heap Sort ① Select Sort 1, the basic idea: in the group of numbers to be sorted, select the smallest number and the first position of the number of exchanges, and then in the remaining number to find the smallest and second position of the number of exchanges, so loop to the penultimate number and the last number comparison. 2. Example 3, Java implementation
`1 package com.sort; 2  3//Unstable 4 public class simple selection sort {5 6 public     static void Main (string[] args) {7         int[] a={49,38,65,97,76, 13,27,49,78,34,12,64,1,8}; 8         System.out.println ("before Sorting:"), 9 for         (int i = 0; i < a.length; i++) {             System.out.print (a[i]+ "");         }12         //Simple selection sort for         (int i = 0; i < a.length; i++) {             decimal int min = a[i];15             int n=i;//Minimum index for             (int j=i+1;j<a.length;j++) {                 a[j]<min) {  //Find the smallest number of                     min = a[j];19                     n = j;20                 }21             }22             a[n] = a[i];23             A[i] = min;24         }26         System.out.println (),         System.out.println ("After sorting:");         int i = 0; i < a.length; i++) {             System.out.print (a[i]+ ");         }31     }32 33}`

4. Analysis

Simple selection sorting is an unstable sort.

Time complexity: T (n) =o (n2).

② Heap Sorting

1. Basic ideas:

Heap sorting is a sort of tree selection, which is an effective improvement on direct selection sorting.

Heap definition: A sequence with n elements (h1,h2,..., HN), when and only if satisfied (hi>=h2i,hi>=2i+1) or (hi<=h2i,hi<=2i+1) (i=1,2,..., N/2) is called a heap. Only the heap that satisfies the former condition is discussed here. As can be seen from the definition of a heap, the top element of the heap (that is, the first element) must be the largest (large top heap). A fully binary tree can represent the structure of a heap visually. Heap top is the root, the other is Zuozi, right subtree.

Thought: Initially, the sequence of numbers to be sorted is considered to be a two-fork tree of sequential storage, adjusting their storage order to make it a heap, when the heap has the largest number of root nodes. The root node is then exchanged with the last node of the heap. The number of fronts (n-1) is then re-adjusted to make it a heap. And so on, until there are only two nodes of the heap, and exchange them, and finally get an ordered sequence of n nodes. In terms of algorithm description, heap sequencing requires two processes, one is to build the heap, and the other is the last element of the heap to exchange the position. So the heap sort has two functions. One is to build the seepage function of the heap, and the second is to call the function of the infiltration function to realize the sorting.

2. Example

Initial sequence: 46,79,56,38,40,84

Build heap:

Swap, kicking the maximum number out of the heap

And so on: The last two remaining nodes in the heap are exchanged, kicked out, and sorted.

3. Java implementation

` 1 package com.sort; 2//unstable 3 import java.util.Arrays; 4 5 public class Heapsort {6 public static void main (string[] args) {7 int[] a={49,38,65,97,76,13,27,49,78, 34,12,64};   8 int arraylength=a.length; 9//Cyclic build heap for (int i=0;i<arraylength-1;i++) {11//Jian Yu buildmaxheap (A,arra  YLENGTH-1-I);  13//Swap heap top and last element in swap (a,0,arraylength-1-i);  System.out.println (Arrays.tostring (a));           16} 17}18//data array from 0 to LastIndex build large top heap public static void Buildmaxheap (int[] data, int lastIndex) {20             Starting from the parent node of the LastIndex node (the last node) for (int i= (lastIndex-1)/2;i>=0;i--) {//k Saves the node being judged 23 int k=i;24//If the child node of the current K-node exists in the (k*2+1<=lastindex) {//k node of the left child node                 The index of INT biggerindex=2*k+1;28//If Biggerindex is less than lastindex, that is, the right child node of the K node represented by Biggerindex+1 exists 29 if (bIggerindex<lastindex) {30//If the value of the right child node is greater than if (Data[biggerindex]<data[bigger  Index+1]) {//biggerindex always records the index of a larger child node biggerindex++; 34} 35} 36//If the value of the K-node is less than the value of its larger child nodes if (data[k]<  Data[biggerindex]) {38//exchange their Data,k,biggerindex swap;  40//Biggerindex to K, start the next loop of the while loop, re-guarantee that the value of the K-node is greater than the value of its left and right child nodes k=biggerindex;  }else{; 44} 45}46}47}48//interchange-private static void swap (int[] data, int i, in  T j) {Tmp=data[i int];  Wuyi Data[i]=data[j];  data[j]=tmp; 53} 54}`

4. Analysis

Heap ordering is also an unstable sorting algorithm.

Heap sorting is better than simple selection of the reason for sorting:

In order to select the smallest record of a keyword from R[1..N], a n-1 comparison must be made, and then the smallest record of the keyword is selected in R[2..N], and n-2 is required. In fact, there are a number of comparisons that are likely to have been made in the previous n-1 comparison, but because the previous n-2 did not retain these comparisons, the comparison was performed repeatedly when the latter was sorted.

Heap sorting saves some of the comparison results by using a tree structure, which reduces the number of comparisons.

The worst time complexity for heap sorting is O (NLOGN). The average performance of the heap is closer to the worst performance. Heap sorting is not appropriate for files with fewer records due to the number of comparisons required to build the initial heap.

Third, exchange sort

① Bubble Sort

1, the basic idea: in order to sort a group of numbers, the current is not yet ranked in the range of all the number, top-down to the adjacent two numbers in turn to compare and adjust, so that the larger number to sink, smaller to the top. That is, each time a comparison of two adjacent numbers finds that they are in the opposite order of order, they are interchanged.

2. Example

3. Java implementation

`1 package com.sort; 2  3//Stable 4 public class bubble sort {5 public     static void Main (string[] args) {6         int[] a={49,38,65,97,76,13,27,49,7 8,34,12,64,1,8}; 7         System.out.println ("before Sorting:"), 8 for         (int i = 0; i < a.length; i++) {9             System.out.print (a[i]+ "");         One//bubble sort for         (int i = 0; i < a.length; i++) {All for             (int j = 0; j<a.length-i-1; j + +) {+//here-I                 mainly Each traversal of the maximum number of I sank to the bottom, there is no need to replace the                 a[j]>a[j+1] {+                     int temp = a[j];17                     a[j] = a[j+1];18                     a[j+1 ] = temp;19                 }20             }21         }22         System.out.println (),         System.out.println ("After sorting:");         (int i = 0; i < a.length; i++)             { System.out.print (a[i]+ "");         }27     }28}`

4. Analysis

Bubble sort is a stable sort method.

• If the first form of the file is a positive sequence, then a trip to the bubble can be completed sorting, sorting code compared to n-1, and no record movement, time complexity is O (n) • If the file is in reverse order, you need to n-1, each trip to N-i order comparison, and each comparison moved three times, Maximum number of comparisons and moves: O (n2) • Bubble sort Average time complexity is O (n2) ② Quick Sort 1, the basic idea: Select a datum element, usually select the first element or the last element, through a scan, the waiting sequence is divided into two parts, some smaller than the Datum element, part is greater than the datum element, when the datum element in its correct position after the order, Then the same method is used recursively to sort the two parts of the division. 2. Example 3, Java implementation
`Package com.sort;//unstable public class quick sort {public static void main (string[] args) {int[] a={49,38,65,97,76,13,27        , 49,78,34,12,64,1,8};        System.out.println ("Before sorting:");        for (int i = 0; i < a.length; i++) {System.out.print (a[i]+ "");        }//Fast sort quick (a);        System.out.println ();        System.out.println ("After sorting:");        for (int i = 0; i < a.length; i++) {System.out.print (a[i]+ "");        }} private static void Quick (int[] a) {if (a.length>0) {quickSort (a,0,a.length-1);            }} private static void QuickSort (int[] A, int low, int. high) {if (Low4. AnalysisQuick Sort is an unstable sort.The time complexity for quick sorting is O (Nlogn).When n is larger, it is better to use fast parallelism, but it is not good to use fast platoon when the sequence is basically ordered.Iv. Merging and sorting1, the basic idea: Merge (merge) sorting method is to combine two (or more than two) ordered tables into a new ordered table, that is, to sort the sequence into a number of sub-sequences, each sub-sequence is ordered. Then the ordered subsequence is combined into a whole ordered sequence.2. Example3. Java implementation 1 package com.sort; 2 3//Stable 4 public class merge sort {5 public static void main (string[] args) {6 int[] a={49,38,65,97,76,13,27,49, 78,34,12,64,1,8}; 7 System.out.println ("Before sorting:");          8 for (int i = 0; i < a.length; i++) {9 System.out.print (a[i]+ ""); 10}11//merge sort 12 MergeSort (a,0,a.length-1); System.out.println (); System.out.println ("After sorting:"); int i = 0; i < a.length; i++) {System.out.print (a[i]+ ");}18}19 private static void MergeSort (int[] A, int l             EFT, int right) {left<right) {middle = (left+right)/2;23//recursion to the left 24 MergeSort (A, left, middle), 25//recursive to the right of the MergeSort (A, middle+1, yes); 27//Merge 2 8 merge (a,left,middle,right);}30}31 + private static void merge (int[] A, int left, int mi Ddle, int right) {int[] Tmparr = new Int[a.length];34 int mid = middle+1; The start position on the right is the int tmp = left;36 int third = left;37 while (left<=middle && mid<=right) {              38//Select a smaller number from two arrays into the intermediate array (A[left]<=a[mid]) {tmparr[third++] = a[left++];41         }else{42 tmparr[third++] = a[mid++];43}44}45//Put the remaining parts in the middle array 46             while (Left<=middle) {tmparr[third++] = a[left++];48}49 while (mid<=right) {50 tmparr[third++] = a[mid++];51}52//Copy intermediate array back to original array (tmp<=right) {a[ TMP] = tmparr[tmp++];55}56}57}4. AnalysisMerge sort is a stable sorting method.The time complexity of the merge sort is O (Nlogn).The speed is second only to the fast order, the stable sort algorithm, is generally used for the total disorder, but each sub-item is relatively orderly sequence.Five, the base sort1, the basic idea: all the values to be compared (positive integers) are unified into the same number of digits, the number of the short number of 0 before the first. Then, start with the lowest bit and order one at a time. Thus, from the lowest bit to the highest order, the sequence becomes an ordered series.2. Example3. Java implementation 1 package com.sort; 2 3 Import java.util.ArrayList; 4 Import java.util.List; 5//Stable 6 public class radix sort {7 public static void Main (string[] args) {8 int[] a={49,38,65,97,176,213,227,49, 78,34,12,164,11,18,1}; 9 System.out.println ("before Sorting:"), ten for (int i = 0; i < a.length; i++) {System.out.print (a[ i]+ ""); 12}13//Cardinal sort (a); System.out.println (); System.out.println ("Sort of      After: "); (int i = 0; i < a.length; i++) {System.out.print (a[i]+" "); 19}20}21 22 private static void sort (int[] array) {23//finds the maximum number, determines how many trips are to be sorted by int max = 0;25 for (int i = 0; i < Array.Length;          i++) {Max<array[i] {31 max = array[i];28}29}30//number of digits         int times = 0;32 while (max>0) {max = max/10;34 times++;35}36 Set up 10 queues of PNs List<ArrayList> queue = new arraylist<arraylist> (); (int i = 0; i < i++) {Arra Ylist queue1 = new ArrayList (), Queue.add (queue1), 41}42//times allocated and collected for (int i = 0; I < times; i++) {44//allocation for (int j = 0; J < Array.Length; J + +) {\$ int x = array[j]% (in T) Math.pow (I+1)/(int) Math.pow (i), ArrayList queue2 = Queue.get (x), Queue2.add (             ARRAY[J]); Queue.set (x,queue2); 50}51//collecting the int count = 0;53 for (int j = 0; J <; J + +) {Queue.get (j). Size () >0) {ArrayList <Integer> queue3 = Queue.get (j); Array[count] = queue3.get (0); queue3.re Move (0); count++;59}60}61}62}63}4. AnalysisThe Cardinal sort is a stable sorting algorithm.The time complexity of the radix sort is O (d (n+r)), D is the number of bits, and R is the cardinality.Summarize:First, Stability:Stabilize: Bubble sort, insert sort, merge sort, and Cardinal sortInstability: Select sort, quick sort, hill sort, heap sortSecond, the average time complexityO (n^2): Direct insert Sort, simple select sort, bubble sort.In the case of small data size (9W), the direct insertion of the sort, simple selection of sort almost. The bubbling sorting algorithm has the highest time cost when the data is large. The Performance O (n^2) algorithm is basically a comparison of adjacent elements, which are basically stable.O (NLOGN): Quick Sort, merge sort, hill sort, heap sort.Among them, the fastest row is the best, followed by the merger and Hill, heap sorting in the data is very large when the effect is obvious.Three, the choice of sorting algorithm1. Small Data size(1) In the case of the basic sequence of the sequence to be sorted, you can choose the direct insertion sort ;(2) The stability is not required to use simple selection of sorting, stability requirements should be inserted or bubbling2. Data size is not very large(1) Completely can use memory space, the sequence is disorderly, there is no requirement for stability, fast sorting , at this time to pay the log (N) extra space.(2) The sequence itself may be orderly, to the stability of the requirements, space permitting, it is appropriate to use the merge sort3. Data size is large(1) For the stability of the request, you can consider the merger sort.(2) Not required for stability, should be sorted by heap4. Sequence initial basic order (positive order), preferably with direct insertion, bubblingResources:http://blog.csdn.net/without0815/article/details/7697916 http://gengning938.blog.163.com/blog/static/ 128225381201141121326346/Algorithm (i): sort`
Related Keywords:

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.