| | |
Pigeonhole Sort Help
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2008
Posts: 7
Reputation:
Solved Threads: 0
I have managed to concur bubble sort, quick sort and heap sort but I've got stuck on pigeonhole sort. I haven't been able to find much on the internet about it.
I have wrote some code for it but it doesn't seem to work. I was wondering if any of you nice people could point me in the direction of a tutorial or something, even just an example.
What I basically want is to to sort random numbers into order.
I will post my code below so you can point out any of the many errors you will find if you wish too.
Any help at all will be appreciated!
I have wrote some code for it but it doesn't seem to work. I was wondering if any of you nice people could point me in the direction of a tutorial or something, even just an example.
What I basically want is to to sort random numbers into order.
I will post my code below so you can point out any of the many errors you will find if you wish too.
Any help at all will be appreciated!
C Syntax (Toggle Plain Text)
void pigeonsort( int max, int random[] , int pigeon[] ) { typedef struct node { int data; struct node* next; }NODE; NODE **hashtable, *temp, *p1, *p2; int i; int j; int hashnum; int hashed; float tempdata; int counter[2] = {0,0}; //Copy random array to new array memcpy( pigeon, random, (sizeof(int)*max)); hashnum= (int)(max*1.5); hashtable = (NODE**) malloc(sizeof(NODE*)*hashnum); for(i = 0; i < hashnum; i++) { hashtable[i] = NULL; }// End of for loop. for (i = 0; i < max; i++) { hashed = (int)((long)pigeon[i]*(long)hashnum/1000); if (hashtable[hashed]==NULL) { counter[0]++; hashtable[hashed] = (NODE*) malloc(sizeof(NODE)); hashtable[hashed]->data = pigeon[i]; hashtable[hashed]->next=NULL; } //End of if statement. else { temp = (NODE*)malloc(sizeof(NODE)); p2 = hashtable[hashed]; p1 = p2; counter[0]++; if(pigeon[i]<= p2->data) { hashtable[hashed] = temp; temp->data = pigeon[i]; temp->next = p1; }//End of if statement. else { while(pigeon[i] > p2->data ) { counter[0]++; p1 = p2; p2 = p2->next; if(p2 == NULL) break; }// End While loop. counter[1]++; p1->next = temp; temp->data = pigeon[i]; temp->next = p2; } //End else statement. } // End outer Else statement. }//End For loop. for (i = 0, j = 0; i < hashnum; i++) { if(hashtable[i] != NULL) { p1 = hashtable[i]; while(p1 != NULL) { pigeon[j] = p1->data; p2 = p1; p1 = p1->next; j++; free(p2); } //End While loop. }//End if statement. }//End For loop. //Print out unsorted & sorted data printf(" Unsorted || Sorted\n"); printf("----------||----------\n"); for(i = 0; i < max; i++) { printf(" %4d || %4d \n", random[i], pigeon[i]); } printf("Press Any Key to Continue"); getch(); free(hashtable); }//End PigeonHole Sort.
>I haven't been able to find much on the internet about it.
It's really nothing more than a bucket sort where each bucket holds a count for each number on the range:
>I will post my code below so you can point out any
>of the many errors you will find if you wish too.
Why are you using a hash table?
It's really nothing more than a bucket sort where each bucket holds a count for each number on the range:
c Syntax (Toggle Plain Text)
#include <stdlib.h> int pigeonhole_sort ( int a[], int n, /* Array and size of the array */ int lo, int hi ) /* The range of values stored */ { int *buckets = malloc ( ( hi - lo ) * sizeof *buckets ); int i, j, k; if ( buckets == NULL ) return 0; /* Clear all counts */ for ( i = 0; i < ( hi - lo ); i++ ) buckets[i] = 0; /* Count the number of occurrences for each value */ for ( i = 0; i < n; i++ ) buckets[a[i] - lo] = buckets[a[i] - lo] + 1; k = 0; /* Copy non-empty buckets back to the array */ for ( i = 0; i < ( hi - lo ); i++ ) { /* Repeat the value as necessary */ for ( j = 0; j < buckets[i]; j++ ) a[k++] = i + lo; } return 1; }
>of the many errors you will find if you wish too.
Why are you using a hash table?
New members chased away this month: 3
•
•
Join Date: Apr 2008
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
It's really nothing more than a bucket sort where each bucket holds a count for each number on the range
I was told hash tables were required. Is this not true?
>I did read about it being pretty much the same as the bucket sort.
Yes and no. It's sort of half way between counting sort and bucket sort.
>I was told hash tables were required.
By whom? There is such a beast as a "hash sort", but I'm more inclined to believe that whoever told you hash tables are required doesn't really know what he's talking about.
Yes and no. It's sort of half way between counting sort and bucket sort.
>I was told hash tables were required.
By whom? There is such a beast as a "hash sort", but I'm more inclined to believe that whoever told you hash tables are required doesn't really know what he's talking about.
New members chased away this month: 3
•
•
Join Date: Apr 2008
Posts: 7
Reputation:
Solved Threads: 0
>By whom? There is such a beast as a "hash sort", but I'm more inclined to believe that whoever told you hash tables are required doesn't really know what he's talking about.
Sorry, I was not actually told they were needed but I was told we need to use them to show we understand how to use them. He is what the specification I have says:
"The pigeon hole sort algorithm using a hash table where hash collisions are handled using linked lists (chained hashing). The hash table must be dynamically allocated based on the number of items to be sorted. If your code does not handle hash collisions you will be awarded limited marks."
Sorry, I was not actually told they were needed but I was told we need to use them to show we understand how to use them. He is what the specification I have says:
"The pigeon hole sort algorithm using a hash table where hash collisions are handled using linked lists (chained hashing). The hash table must be dynamically allocated based on the number of items to be sorted. If your code does not handle hash collisions you will be awarded limited marks."
![]() |
Other Threads in the C Forum
- Previous Thread: C for Web development?
- Next Thread: finding prime numbers that add up to even number?
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file floatingpointvalidation fork forloop framework function functions getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux hacking histogram homework ide include incrementoperators input intmain() iso kernel keyboard kilometer km lazy license linked linkedlist linux list lists looping loopinsideloop. lowest matrix microsoft mqqueue number oddnumber odf openwebfoundation overwrite pause pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprogramming spoonfeeding standard string student systemcall testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






