Heap sorting (c + +): Three big sorts of processing massive data

Source: Internet
Author: User
Tags sorts

When it comes to sorting large data volumes (100W or more), the following three sorting methods are used: quick sort, merge sort, heap sort. At this level, other bubbles, selection, insertion sort, etc. have not been seen at all, the efficiency is very low, with the previous three kinds of sorting poor appearances, so do not compare.

The average time complexity of the three sorts is O (NLOGN), the fast sort, and the merge sort is reduced in the face of the order of the basic ordered sequence. And the merge sort requires a temporary storage space of O (n). Heap sorting has no obvious drawbacks, especially in the face of sorting requirements that often insert new elements, and heap sorting works best.

The following is a time comparison of three sorting of 100W unordered arrays, which can be seen on average, time efficiency: fast > Merge > Heap sorting

Basic Concepts                                                             

What is a heap?

Heap : A data structure, all called: two fork heap data structure, is an array object.

When all nodes are greater than the respective left and right child nodes, it is called the Big Top heap;

When all nodes are smaller than the respective left and right child nodes, it is called the small top heap.

In heap ordering, a large top heap structure is used.

Sorting principle                                                                  

If the maximum value of the top of the output heap is made, the sequence of the remaining n-1 elements is re-built into a heap, then the second largest value of n elements is obtained. With this repeated execution, an ordered sequence can be obtained, and this process is called a heap sort.

Therefore, the idea of the implementation of heap sequencing can be subdivided into two parts:

1. How to arrange an unordered array into a large top heap (build heap process)

2. How to find the second largest value from the remaining heap after taking the maximum value, re-establish the large top heap (screening process)

Complexity of time                                                               

Heap sorting can be divided into two parts: the process of building a heap and the sequencing process.

The time complexity of building a heap is O (n), and it takes only linear time to build a pile of unordered arrays.

When sorting requires n data to be filtered, O (logn) time is required for each filter, so the time for the entire sort process is O (NLOGN)

So the total run time for the heap sort is: O (nlogn) = O (n) + O (nlogn)

Algorithm implementation                                                                   

#include"stdafx.h"#include<iostream>#include<ctime>using namespacestd;inta[1000000];#defineBegin_record \{clock_t ____temp_begin_time___; ____temp_begin_time___=clock ();#defineEnd_record (dtime) \Dtime=float(Clock ()-____temp_begin_time___)/clocks_per_sec;}/*Target: The filter area is a subtree with index i as the root tree, find out the maximum value of the subtree, and put it into the index I process: from the index of the node I to start down, with a large sub-node Exchange values, search down until the sub-tree bottom A-to sort the array I-Filter the starting node index len -Number of sorted elements*/voidSiftintA[],intIintLen) {    inttemp =A[i]; intj =2*i;  while(J <=Len) {        if(J < Len && A[j] < a[j+1])//If the right node is larger than the left node, then the right node is compared with the parent node .J + +; if(A[i] < a[j])//If the child node is larger than the parent node, the values are exchanged, the child nodes become the new parent node, and continue to filter down{A[i]=A[j]; A[J]=temp; I=J; J=2*i; }        Else                            //If the parent node is larger than the child node, the maximum value of the subtree is found, ending the filter        {             Break; }} A[i]=temp;}/*Heap sort (Big Top heap) A-array to be sorted len-array length*/voidHeapsort (intA[],intLen) {    inttemp; inti;  for(i = len-1; i >0; i--)//The heap sort can only be sorted from subscript 1, so all data in the array is shifted back. The data of subscript 0 is not processed{A[i]= A[i-1]; }         for(i = len/2; I >=1; i--)//Build the heap process (so that the parent node of the whole tree is larger than the child node){Sift (A, I, Len); }     for(i = len-1; I >=2; i--)//sorting procedure: Each time a value is taken from the root (the value must be the maximum value), the last node of the tree is placed N, and the node is removed from the tree. Repeat the sorting process until all nodes are removed from the tree and the sort ends .{Temp= a[1]; a[1] =A[i]; A[i]=temp; Sift (A,1I1);//Remove the maximum from the root, take the tail tree node to the root, when the root is no longer the maximum, you need to filter the root of the tree to ensure that the root remains the maximum value    }}voidPrintArray (intA[],intlength) {cout<<"Array Contents:";  for(inti =0; i < length; i++)    {        if(i = =0) cout<<A[i]; Elsecout<<","<<A[i]; } cout<<Endl;}int_tmain (intARGC, _tchar*argv[]) {    floatTim; Begin_record//int a[1000000];     for(inti =0; I <1000000; i++) {A[i]=int(rand ()%100000); }    //PrintArray (A, sizeof (a)/sizeof (int.));Heapsort (A,sizeof(a)/sizeof(int)); //PrintArray (A, sizeof (a)/sizeof (int.));End_record (Tim) cout<<"Run Time:"<< Tim <<"s"<<Endl; System ("Pause"); return 0;}

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.