Heapsort

This is a discussion on Heapsort within the C Programming forums, part of the General Programming Boards category; Hello Code: Code: #include "stdio.h" #include "conio.h" void heap(int a[], int, int n); void sort(int a[], int n); int main() ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    1

    Heapsort

    Hello

    Code:
    Code:
    #include "stdio.h" 
    #include "conio.h" 
    void heap(int a[], int, int n); 
    void sort(int a[], int n); 
    int main() 
    { 
    const int n = 8; 
    int a<img src="/emoticons/emotion-45.gif" alt="No [N]" /> = {88, 66, 77, 33, 55, 44, 22, 99}; 
    int i; 
    clrscr(); 
    sort(a, n); 
    for(i = 0; i ); 
    getch(); 
    return 0; 
    } 
    //*************** 
    void sort(int a[], int n) 
    { 
    int i, t; 
    for(i = n/2 - 1; i >= 0; i--) 
    heap(a, i, n); 
    for(i = n - 1; i > 0; i--) 
    { 
    t = a[0]; 
    a[0] = a<img src="/emoticons/emotion-55.gif" alt="Idea [I]" />; 
    a<img src="/emoticons/emotion-55.gif" alt="Idea [I]" /> = t; 
    heap(a, 0, i); 
    } 
    } 
    //*************** 
    void heap(int a[], int k, int n) 
    { 
    int t, j; 
    t = a[k]; 
    while(k a[j]) 
    break; 
    a[k] = a[j]; 
    k = j; 
    } 
    a[k] = t; 
    }


    It is said:

    In array ,Leaves are saved in a<img src="/emoticons/emotion-45.gif" alt="No [N]" />..a[n/2],Why?


    And it is said:

    So first loop in sort () ,runs on a[0] ...a[n/2-1].Why?


    May someoen explain about the second loop?

    Thanks in advance

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    The first thing to do is edit all those img src= bits which are in your code to be actually what you have in your text editor. Your code makes no sense.

    Even with that, there are glaring syntax errors which mean it won't even compile, let alone run.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >It is said:
    Where? I'm sure it is said better in many places, including here, which you might want to check out because the concept behind the algorithm is explained in detail.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Heapsort
    By myle in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2008, 01:44 AM
  2. Heapsort
    By xENGINEERx in forum C Programming
    Replies: 2
    Last Post: 03-30-2008, 07:17 PM
  3. heapsort help please!
    By MAC77 in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2005, 11:28 AM
  4. heapsort problem...
    By talz13 in forum C++ Programming
    Replies: 9
    Last Post: 09-23-2003, 04:06 PM
  5. heapsort help plz
    By nsssn73 in forum C# Programming
    Replies: 0
    Last Post: 06-02-2002, 06:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21