Thread: Counter Heap Sort

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    15

    Counter Heap Sort

    Hey I have this code and it counts 2 but i think it should count more then that see what you guys think
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #define num_items 75
    
    int heapSort(int numbers[], int array_size,int counter);
    void siftDown(int numbers[], int root, int bottom);
    int numbers[num_items];
    
    int main()
    {
    int i,counter;
    rand();
    for(i=0; i < num_items; i++)
    {
    	numbers[i]=rand();
    }
    counter=0;
    counter = heapSort(numbers, num_items, counter);
    
    printf("Done with Sort.\n");
    
    for(i=0; i < num_items; i++)
    {
    printf("%i\n", numbers[i]);
    }
    printf("Counter=%i\n", counter);
    }
    int heapSort(int numbers[], int array_size, int counter)
    {
    int i, j, temp;
    for(i=(array_size/2)-1; i >=0; i--)
    {
    	siftDown(numbers,i,array_size);
       counter++;
    		for(j=1; j<=1; j++)
          {
          counter++;
    		for(i=array_size-1; i>=1; i--)
          {
          temp=numbers[0];
          numbers[0]=numbers[i];
          numbers[i]=temp;
    		siftDown(numbers, 0, i-1);
    		}
         }
    	}
    	return counter;
    }
    void siftDown(int numbers[], int root, int bottom)
    {
    int done, maxChild, temp;
    done=0;
    while((root*2 <=bottom) && (!done))
    {
    if(root*2 == bottom)
    maxChild=root*2;
    else if (numbers[root*2] > numbers[root*2+1])
    maxChild=root*2;
    else
    maxChild=root*2+1;
    if(numbers[root] < numbers[maxChild])
    {
    temp=numbers[root];
    numbers[root]=numbers[maxChild];
    numbers[maxChild]=temp;
    root=maxChild;
    }
    else
    done=1;
    }
    }

  2. #2
    Registered User shuesty's Avatar
    Join Date
    Oct 2002
    Posts
    21
    I didn't look at your code long enough to completely understand it but I found some problems with it. There's a reason for only getting 2 ... it's because you don't actually sort the numbers.

    Look at the three for loop in heap sort. The for loop with j is only setup to run once ... is that what is supposed to happen?? Secondly the other two use the same index number. The third for loop will run until i gets to 1. Then when the initial loop try the condition it fails and it jumps out of the sort. Fix those two and your program will run MUCH better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  2. heap sort problem
    By Cpro in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2008, 04:54 AM
  3. Heap Sort
    By silicon in forum C++ Programming
    Replies: 1
    Last Post: 05-18-2005, 12:53 PM
  4. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 05:03 AM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM