An optimized heap sorting

Source: Internet
Author: User
How to generate m random numbers? After reading the article about programming, I learned a little about it. later I found someone else designed it on csdn. If there is no clue, a random number is usually generated, and then the existence of the set is compared, which does not exist in it; otherwise, the generation will continue. Each time an element is extracted in descending order, an ordered sequence from right to left is established. For n-1 siftup and siftdown operations, each operation can be O (logn) at most, so the time is O (

How to generate m random numbers? After reading the article about programming, I learned a little about it, and later I found someone else designed it on csdn. let me talk about it. If there is no clue, a random number is usually generated, and then the existence of the set is compared, which does not exist in it; otherwise, the generation will continue. According to Zhu Ji, psuedo:

Select = m; remaining = n; for I = [0.. n] if (bigrand () % remaining)    Code:Void getknuth (int m, int n) {for (int I = 0; I        Another method is to generate n numbers first from 0 to n in ascending order of I, and then randomly disrupt the positions of m numbers and select them.    One cycle:For (int I = 0; I        Similarly, each data is initialized to 0, and a random position is selected. if the position is zero, no value is assigned, and the value is I:Int a [100] = {0}; int I, m; for (I = 1; I <= 99; ++ I) {while (a [m = rand () 0]); a [m] = I ;}This method is inefficient (because it is likely to generate duplicate locations), and the principles behind this chapter are very good:Correctly understand your problemsExtract abstract questionsConsider as many solutions as possibleImplement a solution    These suggestions are really good. The time and depth of thinking are proportional to the intensity of solving the problem.        Well, let's talk about the problem and answer another sorting method: heap sorting.    Heap sorting is also one of the good sorting methods. for example, the average efficiency of fast sorting is very high, but in the worst case, it still cannot escape the O (n ^ 2) level (when the data is already sorted ), however, the heap sorting remains strong in the worst case, maintaining the O (n * log (n) height. Let's talk about it.        Heap sorting requires the establishment of a heap. the heap is a special data structure: x [I/2] <= x [I] has the heap attribute.The heap operation has two key operations: siftup and siftdown (filtering up and filtering down respectively correspond to inserting a data and modifying the heap top data). note: The Heap here is represented by an array.    Void siftup (int n) pre n> 0 & heap (1, n-1) post heap (1, n) {tmp = x [I]; I = n; while (I> 1) {if (x [I]> = x [I/2]) break; x [I] = x [I/2]; // swap (x [I], x [I/2); I = I/2;} x [I] = temp;} void siftdown (int n) pre heap (2, n) & amp; n> 0; post heap (1, n) {I = 1; c = 2 * I; while (c <= n) {if (c + 1 <= n) {if (x [c]> x [c + 1]) c ++ ;} if (x [c]> = x [I]) break; swap (x [c], x [I]); I = c; c = 2 * I ;}}    Heap sorting combines arrays with two abstract structures: heap on the left and sorted element sequence on the right. 1 ................... I ..................... nFirst, create a heap:For I = 2. n // The first one is already heap siftup (I)        Then create an ordered sequence:For I = n; I> = 2; I -- swap (1, I); siftdown (i-1 );Based on the above, you can write down the following short sorting method:For (int I = 2; I <= n; I ++) siftup (I); for (int I = n; I> = 2; I --) {swap (1, I); siftdown (i-1 );}        Each time an element is extracted in descending order, an ordered sequence from right to left is established. For n-1 siftup and siftdown operations, each operation has a maximum of O (logn), so the time is O (nlogn), which is very powerful.This article is available at http://www.nowamagic.net/librarys/veda/detail/1169.

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.