My heap sorting is wrong somewhere, help me fix it

Updated on home 2024-05-10
12 answers
  1. Anonymous users2024-02-10

    When maintaining the heap, your flag is not initialized, and whether it enters the while loop is undetectable.

    Just assign a non-0 value to the flag before while to ensure that it enters the loop.

    Also, I don't understand why you use a very small pile to arrange the array in reverse order, and then output it in reverse order, which is purely redundant. Just use the very large pile.

  2. Anonymous users2024-02-09

    *heapsort*/

    #include

    #include

    #include

    int a[1000];

    int n;

    void heap(int d ,int m) maintains the heap.

    int min,flag=1,temp;

    while(flag)

    flag=0;

    min=a[d];

    if(d*2<=m&&a[d*2]=1;i--)heap(i,n);The initial heap is built in an orderly manner from the bottom to the top.

    int main()

    int i,temp;

    printf("please enter the number(<999) of the number is to be sorted.");

    scanf("%d",&n);

    printf("please enter each number.");

    for(i=1;i<=n;i++)scanf("%d",&a[i]);

    long tim=gettickcount();

    buildheap();

    for(i=n;i>=2;i--)

    temp=a[1];

    a[1]=a[i];

    a[i]=temp;

    heap(1,i-1);

    for(i=n;i>=1;i--)printf("%d",a[i]);

    printf("it takes %ld (ms).",gettickcount()-tim);

    system("pause");

  3. Anonymous users2024-02-08

    It's correct to use the size of the left and right trees to sort the heaps. There's nothing wrong with that.

  4. Anonymous users2024-02-07

    I'm going to post my own template, my heap sorting.

    #include

    #include

    void heapadjust(int array, int a, int nlength)

    void heapsort(int array, int nlength)

    int main()

  5. Anonymous users2024-02-06

    Heap sorting1, heap definition.

    A heap is a sequence of n keywords with the following characteristics:

    ki<=k2i

    and ki<=k2i+1 (1<=i<=n 2) (1).

    or ki>=k2i

    and ki>=k2i+1 (1<=i<=n 2) (2).

    ki>=k2i

    Those that satisfy Eq. (1) are called minimalist heaps, or very small heaps, or small heaps, and those that satisfy Eq. (2) are called maximized heaps, or very large heaps. In this section, we will use a miniaturized heap as an example.

    The relationship between the heap and the full binary tree: The heap is a sequence of n elements (keywords), which satisfies the relationship between the nodes (the relationship between the parents and the children's serial numbers) in the sequential storage of the complete binary tree.

    17, 28, 51, 33, 62, 96, 87, 51 are small top piles.

    96, 51, 87, 33, 28, 62, 51, 17 are the big top piles.

    Binary heap 2, the basic problem of heap sorting.

    Since the top element (keyword) is the smallest element, it is the smallest element of the sorted sequence, and after output, the other elements are adjusted into a heap, and the new top element is the second element of the sorted sequence. In this way, a disordered sequence can be turned into an ordered sequence by using the heap. Therefore, the basic problem with heap ordering is:

    1) How to build a pile.

    2) How to adjust the pile.

    3. How to adjust the pile.

    After swapping the last element with the top element of the heap (equivalent to outputting the top element of the heap), all elements from the top of the heap to the penultimate element meet the definition of the heap. The following is a filter method to put all the elements, including the top element of the pile, into a pile. Big pile of roots.

    void sift(rectype r,int i,int m)

    for‖heapsort

    6. Analysis of heap sorting algorithm.

    The time complexity is o(nlogn) and only one secondary storage space of record size is required for swapping.

  6. Anonymous users2024-02-05

    Let's give you a stacked **!

    Smallroot tree * include

    using namespace std;

    int a[10000];

    int n;

    void check(int i)}}

    void bheap()

    int main()

    system("pause");

    return 0;}

  7. Anonymous users2024-02-04

    Number of deletions in the minimum heap.

    void minheapdeletenumber(int a, int n)

    The problem with this function is that you should first swap the deleted element with the last element, compare it and then decide whether to adjust it up or down.

  8. Anonymous users2024-02-03

    You're obviously wrong Heap sorting first of all there must be more than one data should be a set of data in the array int a 20 , and then assign a value to the array, heap sorting is not written by yourself...

  9. Anonymous users2024-02-02

    minheapaddnumber();

    minheapsorttodescendarray();

    The arguments passed by your two functions don't match the form parameters.

  10. Anonymous users2024-02-01

    Heap ordering is the equivalent of a sorting binary tree, except that the priority of the root node is greater than that of any child, so that the root node can be deleted each time and then the whole heap can be adjusted.

    program heap;

    var a:array[1..10000] of integer;

    n,i:integer;

    procedure down(i:integer);

    var x,j:integer;

    beginx:=a[i];

    j:=i*2;

    while j<=n do

    beginif a[j]>a[j+1] then j:=j+1;

    if a[j]begin

    a[i]:=a[j];

    i:=j;j:=i*2;

    end else break;

    end;a[i]:=x;

    end;procedure delete(i);

    beginn:=n-1;

    if (n=0)or(i=n+1) then exitelse

    begina[i]:=a[n+1];

    down(i);

    end;end;

    beginreadln(n);

    for i:=1 to n do read(a[i]);

    for i:=n div 2 downto 1 do down(i);

    for i:=1 to n do

    beginwrite(a[1]);

    delete(1);

    end;end.

  11. Anonymous users2024-01-31

    You can imagine that this data is used to form a binary tree, where the left child of each node is smaller than him, and the child is older than him, so that a sorting tree is established. Finally, in the middle order of traversing one side, read a point to print the data of this point.

    PS: Who is that master upstairs? The program seems right?!

  12. Anonymous users2024-01-30

    for (int i = heap[0] /2; i > 0; i++)

    heapmaxheapify(heap, i);

    This cycle is an endless loop.

Related questions
6 answers2024-05-10

No, it's a process that is related to the task, and I'm just as hungry as you are, so I have a lot more experience.

8 answers2024-05-10

Isn't it this one:

Sorry baby >>>More

22 answers2024-05-10

Dude: To be honest, after reading your problem, I will never have a problem with your lover It's not that I blow the wind and light the fire, so that anyone will move away from ghosts >>>More

5 answers2024-05-10

I'll take a look at the title.

15 answers2024-05-10

First of all, look at the source of milk.

The temperate grassland between 40-50 ° north and south latitude on the earth has a suitable temperature, a pleasant climate, abundant rainfall, fertile soil, more than 100 kinds of high-quality natural pastures, coupled with no pollution in the environment, these elements jointly determine the production environment of pasture and dairy cows and sheep, and also become the decisive factor for the quality of fresh milk (protein content, milk fat content, milk dry matter or total milk solids, cleanliness and other indicators), so this area is called "** milk source belt". >>>More