C # Data Structure -- Sort [bottom],
Sorting Ideology:
Take an integer d1 less than n as the first increment, and group all records of the file. All records whose distance is multiples of d1 are placed in the same group. Insert the sort directly in each group first; then, take the second incremental d2 <d1 repeat the group and sort the above until the incremental dt = 1 (dt <dt-1... <D2 <d1), that is, all records are placed in the same group for direct insertion sorting.
Complexity:O (n3/2 ).
Stability:Unstable.
Code example:
Int [] list = {50, 10, 90, 30, 70, 40, 20}; int len = list. length; int temp, j; while (len> 1) {len = len/2; for (int I = len; I <list. length; I ++) {if (list [I] <list [I-len]) {temp = list [I]; for (j = I-len; j> = 0 & temp <list [j]; j = j-len) {list [j + len] = list [j];} list [j + len] = temp ;}} Console. writeLine ("Hill sorting result: {0}", string. join (",", list ));
Sorting Ideology: A sorting algorithm designed by using the data structure of the stacked tree (HEAP). It is a type of sorting algorithm.
A heap is a complete binary tree with the following properties: the value of each node is greater than or equal to the value of its left and right child nodes; or the value of each node is smaller than or equal to the value of its left and right child nodes.
Complexity:O (nlogn ).
Stability:Unstable.
Heap sorting only uses the following two points:
1: Build a large top stack from an unordered sequence.
2: swap the positions of the top node and the back node (n) in the big top heap, re-adjust the remaining elements, and build a new big top heap. Loop in sequence ....
Code example:
Static void Main (string [] args) {int [] list = {50, 10, 90, 30, 70, 40, 20}; for (int I = list. length/2-1; I> = 0; I --)/* build a large top heap [list. length/2-1: Number of knots in the unordered array] */{HeapAdjust (list, I, list. length);} int temp; for (int I = list. length-1; I> 0; I --)/* replace the location of the Big Top heap, and re-build the Big Top heap. */{Temp = list [0]; /* replace the location list [I] */list [0] = list [I]; list [I] = temp before the maximum value list [0] and the minimum value in the big top heap; heapAdjust (list, 0, I);/* re-build the Big Top heap */} Console. writeLine ("heap sorting result: {0}", string. join (",", list ));} /// <summary> /// create a large top heap /// </summary> /// <param name = "list"> sort set </param> /// <param name = "NodeIndex"> parent node </param> // <param name = "len"> large top heap length </param> static void HeapAdjust (int [] list, int NodeIndex, int len) {int temp = list [NodeIndex];/* Binary Tree node value */for (int I = NodeIndex * 2 + 1; I <len; I = NodeIndex * 2 + 1) /* The left and right children under the cyclic Binary Tree node [NodeIndex * 2 + 1 find the left and right children under the node] */{if (I + 1 <len & list [I] <list [I + 1]) /* I + 1: whether two children exist. list [I] <list [I + 1]: by default, the left child is greater than the right child */{I ++; /* The left child is less than the right child and the list [I] is the right child value */} if (temp> = list [I]) /* If the node is greater than or equal to (left/right), the Child exits directly without replacing the node value */{break;} list [NodeIndex] = list [I]; /* replace the value between the node and the (left/right) child, and keep the node greater than the left and right children */NodeIndex = I;/* reset the node value, cyclically query */} list [NodeIndex] = temp;/* replace (left/right) the value between the child and the node */}
Sorting IdeologyThe Chinese meaning of the word "merge" is the meaning of merging and merging. The definition in the data structure is to combine two or more ordered tables into a new ordered table.
Merging Sort is a Sort method that uses the idea of Merging. The principle is that assume that the initial sequence contains n records, it can be regarded as n ordered subsequences, the length of each subsequence is 1, and then merged into two, obtain the ordinal n/2 substring (the smallest integer not less than x) of an ordered subsequence with a length of 2 or 1 ,......, This is repeated until an ordered sequence with a length of n is obtained. This sorting method is called 2-way merge sorting.
Complexity:O (NLogN).
Stability:Stable.
Code example:
Static void Main (string [] args) {int [] list = {50, 10, 90, 30, 70, 40, 20}; MeSort (list, 0, list. length-1); Console. writeLine ("result of merging and sorting: {0}", string. join (",", list);} static void MeSort (int [] list, int start, int end) {if (start <end) {int middle = (start + end)/2;/* grouping arrays */MeSort (list, start, middle);/* grouping left sequence */MeSort (list, middle + 1, end);/* group right sequence */MergeS (list, start, middle, end);/* Merge (merge) the Left and Right sequences) */} static void MergeS (int [] list, int first, int middle, int end) {int IndexA = first; /* start position of the Left sequence */int IndexB = middle + 1;/* start position of the right sequence */int [] tempList = new int [end-first + 1]; /* Temporary array after the Left and Right sequences are merged */int tempIndex = 0; while (IndexA <= middle & IndexB <= end) /* Data in the round-robin sequence */{if (list [IndexA]> = list [IndexB]) /* compare the data size in the left and right sequences */{tempList [tempIndex ++] = list [IndexB ++];/* The right element is greater than the left element, store the right element in the temporary array tempList, and the temporary array tempIndex ++, then, the next element in the right sequence */} else {tempList [tempIndex ++] = list [IndexA ++];/* The left element is greater than the right element, store the left element in the temporary array tempList, and the temporary array tempIndex ++, and then take the next element in the sequence */} while (IndexA <= middle) /* after one sub-table is traversed, it jumps out of the loop, add the remaining number of sub-tables on the other side to the saved array at a time */{tempList [tempIndex ++] = list [IndexA ++];} while (IndexB <= end) {tempList [tempIndex ++] = list [IndexB ++];} tempIndex = 0;/* set the temporary array to replace from 1st bits */for (int I = first; I <= end; I ++)/* Temporary array to replace the data in the List array */{list [I] = tempList [tempIndex ++];}
Sorting Ideology: Data to be sorted is divided into two independent parts by one sort. All the data in one part is smaller than all the data in the other part, then, sort the two data parts by using this method. The entire sorting process can be recursive to convert the entire data into an ordered sequence.
Complexity:O (NLogN).
Stability:Unstable.
Code example:
Static void Main (string [] args) {int [] list = {50, 10, 90, 30, 70, 40, 20}; QuickSort (list, 0, list. length-1); Console. writeLine ("quick sorting result: {0}", string. join (",", list);} private static void QuickSort (int [] list, int start, int end) {int begin; if (start <end) {Partition = Partition (list, start, end);/* returns the median of the split sequence */QuickSort (list, start, interval-1 ); /* sort low-end sequences */QuickSort (list, sort + 1, end ); /* sort high-end sequences */} private static int Partition (int [] list, int first, int end) {int second tkey = list [first]; /* the default 0th bits in the sequence are pivot */while (first <end) {while (first <end & list [end]> = begin tkey) /* Records smaller than pivot are switched to the low end */{end --;} swap (list, first, end ); while (first <end & list [first] <= pivotkey)/* Records larger than pivot are switched to high-end */{first ++;} swap (list, first, end);} return first; /* return the pivot value */} // <summary> // Replace the position of array A between array B /// </summary> private static void swap (int [] list, int A, int B) {int temp = list [A]; list [A] = list [B]; list [B] = temp ;}
Common sorting methods are basically the same. Shared .....
The sorting performance and speed are faster than other common sorting methods.