Thread: Bucket Sort or Myth

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    whiteflags, I see what you mean and glad you understand. I'm so happy I'm taking a JAVA course this semester. It's helping me to understand C++ ten time better every week. Now I get it ...

    Code:
    //        http://en.literateprograms.org/Bucket_sort_(Java)
    //        BucketSort Array
    
    #include <vector>
    #include <iostream> 
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
     void bucketSorter(int DATA[],int length, int COUNT);
    
     
    int main()
    {
    	int DATA[]    =    {90, 90, 90, 80, 70, 60, 50, 40, 30, 30};
    	int element   =    sizeof(DATA)/sizeof(int);
    	int COUNT     =    DATA[0];
    
    // .......................................
        cout << "Original  order  :";
        for (int i = 0; i < element; i++) {
            cout << "  " << DATA[i];   }
        cout << endl;
        cout << endl;
    // .......................................
        cout << "After Bucket sort:";
     	bucketSorter(DATA, element, COUNT);
    	getchar();
    }
    //  ..............................................
    //  ..............................................
    void bucketSorter(int DATA[],int length, int COUNT)
         {
         int* bSort = new int[COUNT];
         int SIZE = length;
    
         for( int B  = 0  ;  B <= COUNT ;  ++B )   {
              bSort[B] = 0; }
    
    
         for(int A = 0 ; A < SIZE ; ++A)          {
              ++bSort[DATA[A]];  }	
    
    
    
         for( int A = 0, B = 0  ;  B <= COUNT  ; ++B )   {
              for(int C = bSort[B]  ;  C > 0  ;  --C )   {
    		                      DATA[A++] = B;  } }  //  it matters here A++ not ++A
    
    
    
         for( int A = 0  ;  A < SIZE  ;  ++A )
    	 {
             cout << "  " << DATA[A];
         }	
             cout << endl;
    }
    This is my code and it do work! ... but when I cup and paste it from this link, it don't work so I don't know who has a possible bug.
    Last edited by sharris; 03-17-2011 at 08:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  2. Floating Exception (Core Dumped)
    By DarrenY in forum C Programming
    Replies: 9
    Last Post: 05-14-2007, 10:01 AM
  3. Clearing Buffer
    By AndyBomstad in forum C++ Programming
    Replies: 11
    Last Post: 04-30-2005, 01:04 AM
  4. Bucket Sort Problems... Please Help!
    By 67stangman in forum C++ Programming
    Replies: 1
    Last Post: 04-17-2002, 10:13 PM
  5. Bucket sort & list of lists ???
    By EasternStar in forum C Programming
    Replies: 1
    Last Post: 11-20-2001, 12:48 AM