Algorithm Note 005: Heap sequencing "variable Cure method"

Source: Internet
Author: User

Directory

1 problem description

2 Solutions

Brief introduction of 2.1 heap sorting principle

Introduction to the principle of 2.2 transformation method

2.3 Specific Code

2.4 Running Results

1 problem description (1) experimental topics

sorting of any given set of data using a heap sorting algorithm based on the variable rule method

(2) Purpose of the experiment

1) deeply understand and master the design idea of the method of transformation;

2) Grasp the concept of the heap and how to use the method of transformation to change any given set of data piles;

3) Improve the skill of applying the design algorithm of the variable-cure method.

(3) experimental requirements

1) design and implement heap sorting algorithm;

2) The data to be sorted can be entered manually ( usually a small size,aboutten data), to detect the correctness of the program, or the computer can be randomly generated (usually large size, the - the data) to verify ( with counting method ) the time efficiency of the heap sorting algorithm.

2 Solutions Brief introduction of 2.1 heap sorting principle

A heap can be defined as a binary tree, and the nodes of the tree contain keys (each node is a key) and meet the following two conditions:

(1) tree shape requirements -this binary tree is basically complete (or simply to complete a two-fork tree), which means that each layer of the tree is full, except the last layer of the rightmost element may be absent.

(2) parental advantage requirements, also known as heap characteristics --Each node's key is greater than or equal to its children's key (for any child node also to automatically meet parental advantage requirements).

Introduction to the principle of 2.2 transformation method

transformation: First, in the "change" stage, for this or that reason, the problem of the case becomes easier to solve (PS: Similar to this article to solve the problem, in order to sort out the array of the number of arrays processed); Then, in the second stage or " To solve the case.

according to our transformation of the problem instance, there are 3 main types of the thought of transformation:

(1) a simpler or more convenient instance of transforming to the same problem --we call it instantiation simplification;

(2) the different manifestations of the same example --What we call change performance;

(3) transforming to another instance of the problem, the algorithm of this problem is known --we call it the simplification of the problem.

2.3 Specific code
 PackageCom.liuzhen.heapsort; Public classHeapsort {/*Compare the maximum values in Array[a] and array[b], array[c], or the largest value in Array[a] and array[b], array[c, if it is smaller, or return the array directly*/     Public Static int[] Getmaxa (int[] Array,intAintBintc) {        inttemp = 0; if(Array[b] >=Array[c]) {            if(Array[a] <Array[b]) {Temp=Array[a]; Array[a]=Array[b]; ARRAY[B]=temp; }        }        Else{            if(Array[a] <Array[c]) {Temp=Array[a]; Array[a]=Array[c]; ARRAY[C]=temp; }        }        returnArray; }        //based on the heap sort parental dominance rule, returns a pile of results for an array of a given length     Public Static int[] Getheapsort (int[] Array,intLen) {        BooleanJudge =true;  while(judge) {//sort Parent Precedence rules according to the heap, sorting array arrays             for(inti = 1;i <= len/2;i++){               if((2*i+1) <len) Array= Getmaxa (Array,i, (2*i), (2*i+1)); if((2*i) = = len-1) {//when 2*i = = len-1, description array[i] There is only one left child node A[2*i]                   inttemp = 0; if(Array[i] < array[2*i]) {Temp=Array[i]; Array[i]= array[2*i]; array[2*i] =temp; }               }            }                        //iterate through the array of arrays, jumping out for loops once the root node is smaller than its leaf node            intJ;  for(j = 1;j < len/2;j++){                if((2*j+1) <Len) {                    if(Array[j] < array[2*J]) Break; if(Array[j] < array[2*j+1])                         Break; }                if((2*j) = = Len-1){                    if(Array[j] < array[2*J]) Break; }            }                        if(j = = LEN/2)//if J==LEN/2, indicates that the traversal result conforms to the heap collation, directly ends the while loopJudge =false; }                returnArray; }        //use array piles to sort an array element from small to large and return the sorted result     Public Static int[] Getresultsort (int[] Array,intLen) {Array= Getheapsort (array, len);//first, heap sorting is done on an array        inttemp = 0;//array Value Exchange Intermediate variable        intSortlen = Len;//During sorting, the array length of the heap ordering needs to be re-ordered and initialized to the total length of the array         while(Sortlen > 2){    //for (int i = 1;i < len;i++)//System.out.print (array[i]+ "\ t");//System.out.println ();temp = array[1];//Exchange Array[0] and array[sortlen-1] values, that is, place the largest value in the unsorted array the last oneARRAY[1] = array[sortlen-1]; Array[sortlen-1] =temp; Sortlen= sortLen-1;//An unordered array length is automatically reduced by 1 after a successful exchangeArray = Getheapsort (Array,sortlen);//re-ordering the heap for unsorted arrays        }                returnArray; }        //Initializes a random array of length n     Public Static int[] Initarray (intN) {        int[] result =New int[n]; result[0] = 0;  for(inti = 1;i < n;i++) Result[i]= (int) (Math.random () *1000);//Randomly generating numbers between 0~1000 using random functions        returnresult; }         Public Static voidMain (String args[]) {int[] array = {0,1,4,5,3,5,23,45,12,23,34,56,78,23,24,25};//define arrays here, sort array[1] to array[len-1]        intLen =Array.Length; int[] result =Getresultsort (Array,len); System.out.println ("Enter the array manually, use the heap sort, and finally sort the result:");  for(inti = 1;i < len;i++) {System.out.print (Result[i]+ "\ T");        } System.out.println ();        System.out.println (); int[] Onearray = Initarray (1000); intLEN1 = 1000; int[] Result1 =Getresultsort (ONEARRAY,LEN1); System.out.println ("The system randomly generates an array of length 1000 (whose values are between 0~1000), uses the heap sort, and finally sorts the result:");  for(intj = 1;j < len1;j++) {System.out.print (Result1[j]+ "\ T"); if(j%15 = = 0) System.out.println (); }        }}
2.4 running results

Algorithm Note 005: Heap sequencing "variable Cure method"

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.