Three advanced sorting

Source: Internet
Author: User

Three advanced sorting
1. Heap sorting
Heap sorting is suitable for scenarios with a large amount of data (Millions of data ).
Heap sorting does not require a large number of recursive or multi-dimensional temporary arrays.
This is suitable for a sequence with a very large amount of data.
For example, if there are more than millions of records, because of the fast sorting, Merge Sorting uses recursion to design algorithms,
A stack overflow error may occur when the data volume is large.
Heap sorting will build all the data into a heap, and the maximum data is on the heap top,
Then, the data at the top of the heap is exchanged with the last data in the sequence.
Next, rebuild the heap and exchange data. Then, sort all the data.

/// <Summary> /// heap sorting /// </summary> public class HeapSort: ISort {public int [] Sort (int [] array) {if (array! = Null) {HeapHanle (array, array. length); // The Heap Structure for (int I = array. length-1; I> = 0; I --) {Swap (ref array [0], ref array [I]); if (I> 1) {HeapToDown (array, 0, I) ;}} return array ;} /// <summary> /// organize the Heap Structure /// </summary> /// <param name = "array"> </param> /// <param name = "length"> </param> private static void HeapHanle (int [] array, int length) {for (int I = length/2-1; I> = 0; I --) {HeapToDown (array, I, length );}} /// <summary> /// process from bottom up /// </summary> /// <param name = "array"> array </param> /// <param name = "I"> the given node array index </param> private static void HeapToUp (int [] array, int I) {int cur = array [I]; int preIndex = (I-1)/2; while (I> 0 & preIndex> = 0 & cur <array [preIndex]) {array [preIndex] = cur; I = preIndex; preIndex = (I-1) /2;} array [I] = cur ;} /// <summary> /// process from top to bottom /// </summary> /// <param name = "array"> array </param> /// <param name = "I"> the given node array index </param> private static void HeapToDown (int [] array, int I, int length) {int cur = array [I]; int nextIndex = 2 * I + 1; // array index of the left child while (nextIndex <length) {if (nextIndex + 1 <length) {if (array [nextIndex]> array [nextIndex + 1]) {nextIndex = nextIndex + 1; // right child }}if (cur <= array [nextIndex]) {break;} else // process {array [I] = array [nextIndex]; I = nextIndex; nextIndex = 2 * I + 1 ;}} array [I] = cur;} public static void Swap (ref int int1, ref int int2) {int temp = int1; int1 = int2; int2 = temp ;}}Heap sorting

2. Merge and sort
Merge Sorting first breaks down the sequence to be sorted, divides it from 1 into 2, 2 into 4, and splits it in turn,
When there is only one group, these groups can be sorted,
Then merge the data into the original sequence, so that all data can be sorted.
Merging and sorting is a little faster than heap sorting, but the memory space needs to be doubled than the heap sorting,
Because it requires an additional array.

/// <Summary> /// merge Sort /// </summary> public class MergeSort: ISort {public int [] Sort (int [] array) {if (array! = Null) {int [] temp = new int [array. length]; SortLeftAndRight (array, 0, array. length-1, temp);} return array ;} /// <summary> /// combine array [first]-array [middle] and array [middle + 1]-array [last]. // </summary> /// <param name = "array"> </param> /// <param name = "first"> </param> /// <param name = "middle "> </param> /// <param name =" last "> </param> // <param name =" temp "> </param> private void Merge (int [] array, int first, int middle, int last, int [] temp) {int I = first; int n = middle; int j = middle + 1; int m = last; int k = 0; while (I <= n & j <= m) {if (array [I] <= array [j]) {temp [k ++] = array [I ++];} else {temp [k ++] = array [j ++];} while (I <= n) {temp [k ++] = array [I ++];} while (j <= m) {temp [k ++] = array [j ++] ;}for (I = 0; I <k; I ++) {array [first + I] = temp [I];} /// <summary> /// Recursive Sub-governance /// </summary> /// <param name = "array"> </param> /// <param name = "first"> </param> /// <param name = "last"> </param> // <param name = "temp"> </param> private void sortLeftAndRight (int [] array, int first, int last, int [] temp) {if (first <last) {int middle = (first + last)/2; SortLeftAndRight (array, first, middle, temp); SortLeftAndRight (array, middle + 1, last, temp); Merge (array, first, middle, last, temp );}}}Merge Sorting

3. Fast sorting
Quick sorting is a large-scale recursive algorithm that sorts data in the local area separately.
Essentially, it is the local version of Merge Sorting.
The quick sorting can be composed of the following four steps.
(1) If no more than one data entry is returned.
(2) generally, the leftmost value of the sequence is used as the pivot data.
(3) divide the sequence into two parts. One part is greater than the pivot data, and the other part is smaller than the pivot data.
(4) use recursive sorting sequence on both sides.
Quick sorting is faster than most sorting algorithms. Although we can write algorithms faster than quick sorting in some special situations,
But in general, there is no faster than it.
Quick sorting is recursive. It is not a good choice for machines with very limited memory.

/// <Summary> /// quick sorting /// </summary> public class QuickSort: ISort {public int [] Sort (int [] array) {if (array! = Null) {int [] temp = new int [array. length]; QuickSorting (array, 0, array. length-1);} return array;} private static void QuickSorting (int [] array, int l, int r) {if (l <r) {int I = l; int j = r; int temp = array [I]; while (I <j) {while (I <j & array [j]> = temp) {j --;} if (I <j) {array [I ++] = array [j];} while (I <j & array [I] <temp) {I ++ ;} if (I <j) {array [j --] = array [I] ;}} array [I] = temp; QuickSorting (array, l, I-1 ); quickSorting (array, I + 1, r );}}}Quick sorting

Test code:

Class Program {public static Random re = new Random (); static void Main (string [] args) {Stopwatch stw4 = new Stopwatch (); Stopwatch stw5 = new Stopwatch (); stopwatch stw6 = new Stopwatch (); int [] intArray4 = GetArray (int. maxValue/100000); int [] intArray5 = GetArray (int. maxValue/100000); int [] intArray6 = GetArray (int. maxValue/100000); ISort sort4 = new HeapSort (); // stw4.Start (); int [] result4 = sort4.Sort (intArray4); stw4.Stop (); Console. writeLine ("output sorting result (heap sorting)"); Console. writeLine ("Total program running time:" + stw4.Elapsed. toString (); ISort sort5 = new MergeSort (); // merge and sort stw5.Start (); int [] result5 = sort5.Sort (intArray5); stw5.Stop (); Console. writeLine ("output sorting result (Merge Sorting)"); Console. writeLine ("Total program running time:" + stw5.Elapsed. toString (); ISort sort6 = new QuickSort (); // stw6.Start (); int [] result6 = sort6.Sort (intArray6); stw6.Stop (); Console. writeLine ("output sorting result (quick sorting)"); Console. writeLine ("Total program running time:" + stw6.Elapsed. toString (); Console. readKey ();} private static int [] GetArray (long _ arrayLength) {long arrayLength = _ arrayLength; // initialize the array int [] intArray = new int [arrayLength]; for (int I = 0; I <arrayLength; I ++) {intArray [I] = re. next () ;}return intArray ;}}View Code

Test results:

Related Article

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.