About sorting algorithm - bucket sort

Introduction:

Bucket sort is the easiest fastest sort principle is to be assigned to a limited number of array bucket. Each individual bucket and then sorted (it is possible to re-use or other sorting algorithm recursively continue using bucket sort sorting),

Finally, in order to record individual bucket list to remember the ordered sequence.

Time complexity: O ( m + n- ) .

Space complexity: O (n * k)

For example :( example Aha algorithm derived)

To be finished with the final exam teachers learn their scores in descending order. Only the class 5 classmates, this 5 classmates were exam, 5 points, 3 points, 5 points, 2 points and 8 points,

(Score is 10 points). Next scores sorted in descending order, the ordering is 85,532 . Do you have any good way to write a program that allows the computer to read a random 5 number then these

5 Number descending output?

Solution:

 
Size fraction according to create a one-dimensional array to represent fractions 11, the number represented by numeric values, the score represents the number in the array corresponding to
Then the barrel corresponding number printed.
code show as below:
int main () 
{ 
    int A [ 11 ], I, J, T;
     for (I = 0 ; I <= 10 ; I ++ ) // loop 11 
    A [I] = 0 ; // initializes to 0 
    for (I = . 1 ; I <= 5 ; I ++) // loop 5 reads the number of the number of input through a keyboard 5 
    { 
        Scanf ( " % D " , & t); // the number of each of the read variable t 
        A [t] ++; // counts 
    }
       for   (I = 0 ; I <= 10 ; I ++) // sequentially determines a [0] ~ a [10 ] 11 times
            for (J = . 1 ; J <= A [I]; J ++) // there have been several times to print 
                the printf ( " % D " , I); // 5 cycles 
       getchar (); getchar (); 
    / / where getchar (); to pause the program in order to view the contents of program output
     // can also use system ( "pause"); and the like instead of the 
    return  0 ; 
}        

 

 
 
 

Guess you like

Origin www.cnblogs.com/fanhua666/p/11260806.html