Iv. Merging and sorting
1 , basic ideas : Merge ( Merge The sorting method is to combine two (or more than two) ordered tables into a new ordered table, which divides the ordered sequence into several sub-sequences, each of which is ordered. Then the ordered subsequence is combined into a whole ordered sequence.
2 , Instance
3 , Java Implement
1 Packagecom.sort;
2
3//Stable
4 public classMerge 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}
One//Merge Sort
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]+ "");
17}
18}
19
private static Voidmergesort (int[] A, int left, int. right) {
if (left<right) {
middle int = (left+right)/2;
//recursive to the left
MergeSort (A, left,middle);
//recursive to the right
MergeSort (A, middle+1,right);
//Merging
Merge (A,left,middle,right);
29}
30}
31
+ private static void merge (int[] A,int left, int middle, int. right) {
int[] Tmparr = newint[a.length];
int mid = middle+1; //start position on right
int tmp = left;
third int = left;
PNS while (left<=middle&& mid<=right) {
//Select a smaller number from two arrays to put in an intermediate array
if (A[left]<=a[mid]) {
tmparr[third++] =a[left++];
}else{
tmparr[third++] =a[mid++];
43}
44}
//put the rest of the parts into the middle array
(Left<=middle) {
tmparr[third++] =a[left++];
48}
(mid<=right) {
[tmparr[third++] =a[mid++];
51}
//Copy the intermediate array back to the original array
(tmp<=right) {
A[TMP] =tmparr[tmp++];
55}
56}
*
4 , Analysis
Merge sort is a stable sorting method.
the time complexity of 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 sort
1 , the basic idea: to unify all the values to be compared (positive integers) to the same digit length, the number of the shorter number is preceded by 0. 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 , Instance
3 , Java Implement
1 Packagecom.sort;
2
3 Import java.util.ArrayList;
4 Import java.util.List;
5//Stable
6 public classBase 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++) {
One System.out.print (a[i]+ "");
12}
//Base Sort
+ sort (a);
System.out.println ();
System.out.println ("after sorting:");
(int i = 0; I <a.length; i++) {
System.out.print (a[i]+ "");
19}
20}
21st
private static void sort (Int[]array) {
//find the maximum number to determine how many trips to sort
int max = 0;
(int i = 0; I <array.length; i++) {
if (Max<array[i]) {
Max =array[i];
28}
29}
//determine the number of digits
int times = 0;
(max>0) {
max = MAX/10;
times++;
35}
//Build 10 Queues
PNS list<arraylist> Queue =new arraylist<arraylist> ();
(int i = 0; i < 10;i++) {
ArrayList queue1 = Newarraylist ();
Queue.add (queue1);
41}
//make Timessub-allocation and collection
(int i = 0; i < times;i++) {
//Distribution
(int j = 0; J <array.length; J + +) {
=array[j]% int x (int) Math.pow (ten, i+1)/(int) Math.pow (ten, I);
ArrayList queue2 =queue.get (x);
Queue2.add (Array[j]);
Queue.set (x,queue2);
50}
Wuyi//Collect
int count = 0;
(int j = 0; J < 10;j++) {
Queue.get (j). Size () >0) {
arraylist<integer> queue3 = Queue.get (j);
Array[count] =queue3.get (0);
Queue3.remove (0);
count++;
59}
60}
61}
62}
63}
4 , Analysis
The Cardinal sort is a stable sorting algorithm.
the time complexity of base sorting is O (d (n+r)), D is the number of digits, R as the cardinality.
Summarize:
First, stability :
stabilize: bubble sort, insert sort, merge sort, and Cardinal sort
Instability: Select sort, quick sort, hill sort, heap sort
Second, the average time complexity
O (n^2): Direct Insert Sort, simple select sort, bubble sort.
when the data size is small ( 9W inside), directly into the sort, simple selection sort of 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 algorithm
1. 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 bubbling
2. data size is not very large
( 1 completely can use the memory space, the sequence is disorderly, has no requirement to the stability, Quick Sort , this time to pay Log ( N ) of additional space.
( 2 The sequence itself may be ordered, the stability is required, space permitting, the appropriate Merge Sort
3. 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 heap
4. Sequence initial basic order (positive order), preferably with direct insertion, bubbling
Reference: http://www.cnblogs.com/liuling/p/2013-7-24-01.html
Several sorting algorithms and their Code implementation (IV.)