School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

C Source Code


Welcome to Dream.In.Code
Become an Expert!

Join 340,098 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,826 people online right now. Registration is fast and FREE... Join Now!




Bucket Sort

Bucket Sort demo containing reuseable methods for bucket sorting arrays. The program uses the bucketsort methods on 100 arrays each of 100 pieces of data and prints whether the search key is found or not each time. Also prints the arrays to check the programme is working correctly

Submitted By: Ellie
Actions:
Rating:
Views: 7,509

Language: C

Last Modified: January 30, 2007

Snippet


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. const int RANGE = 200; //range for random numbers in the array
  6.  
  7. enum SearchResult {
  8.      FOUND,
  9.      NOT_FOUND
  10.      };
  11.  
  12. void initLookupTable(enum SearchResult lookup[], int array[], int sizeOfData, int RANGE)
  13. {
  14. int v;
  15. for(int m=0; m<RANGE; m++)
  16. {
  17. lookup[m] = NOT_FOUND;
  18. }
  19.  
  20. for(int mm=0; mm<sizeOfData; mm++)
  21. {
  22. v = array[mm];
  23. lookup[v] = FOUND;
  24. }
  25.  
  26. }
  27.  
  28. enum SearchResult searchLookupTable(enum SearchResult lookup[], int key)
  29. {
  30. return lookup[key];
  31. }
  32. void fill_array(int array[], int sizeOfData)
  33. {
  34.  
  35. for (int i = 0; i<=sizeOfData; i++)
  36. {
  37. array[i] = rand() % RANGE;
  38. }
  39. }
  40. void print_array(int array[], int sizeOfData)
  41. {
  42. for (int j = 0; j<=sizeOfData; j++)
  43. {
  44. printf("Item %d is %d\t", j, array[j]);
  45. }
  46. }
  47.  
  48. int main()
  49.   {
  50.         srand ( time (0) );
  51.  
  52.  
  53.  
  54.         for (int ij=0; ij <100; ij++)
  55.     {
  56.      const int arraySize = 100;
  57.      int a [arraySize];
  58.      enum SearchResult lookup[RANGE];
  59.  
  60.      enum SearchResult result;
  61.  
  62.      int searchKey = rand() % RANGE;
  63.  
  64.      fill_array(a, arraySize);
  65.      print_array(a, arraySize);
  66.  
  67.      initLookupTable(lookup, a, arraySize, RANGE);
  68.  
  69.      result = searchLookupTable(lookup, searchKey);
  70.      if (result == FOUND)
  71.         printf("\nKEY %d FOUND \n", searchKey);
  72.      else
  73.         printf("\nKEY %d NOT  FOUND \n", searchKey);
  74.   }
  75.   return (0);
  76. }

Copy & Paste


Comments

There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month