Java implementation based on the idea of bucket sorting and counting sorting to realize the base order

Source: Internet
Author: User

Count sort

Premise: All the keywords to be sorted must be different from each other in the sorted table;

Thought: Counting sorting algorithm for each record in the table, scan the table to be sorted a trip, the statistics of how many records of the key code is smaller than the key code of the record, assuming that for a certain record, the statistical value of the count is C, then the record in the new ordered table storage location is C.

Performance: space complexity: O (n), time complexity: O (n^2);

1      Public int[] Countsort (int[] Array) {2         int[] Temparray =New int[Array.Length];//Introducing Auxiliary Arrays3          for(inti=0;i<array.length;i++){4             intCount = 0;5              for(intj=0;j<array.length;j++){6                 if(array[i]>Array[j]) {7count++;8                 }9             }TenTemparray[count] =Array[i]; One         } A         returnTemparray; -}

Bucket sort

Bucket ordering requires the sequence to be sorted to meet the following two characteristics:

All values in the pending sequence are in an enumerable range, and so on;

This enumerable range of pending sequences should not be too large, otherwise the sort overhead is too large.

The exact steps to sort are as follows:

(1) construct an buckets array for this enumerable range to record the number of elements in each bucket that "falls";

(2) The buckets array obtained in (1) is recalculated and recalculated as follows:

Buckets[i] = Buckets[i] +buckets[i-1] (where 1<=i<buckets.length);
1       Public Static voidBucketsort (int[] data) {  2         //getting the maximum and minimum values in the element to be sorted3         intMax=data[0],min=data[0];4          for(inti=1;i<data.length;i++){5             if(data[i]>max) {6Max =Data[i];7             }8             if(Data[i] <min) {9Min =Data[i];Ten             } One         } A          -         //Cache Array -         int[] tmp =New int[Data.length];  the         //buckets used to record information about the elements to be sorted -         //The buckets array defines max-min+1 buckets -         int[] Buckets =New int[max-min+1];  -         //Count the number of occurrences of each element in a sequence +          for(inti = 0; i < data.length; i++) {   -Buckets[data[i]-min]++;  +         }   A         //calculates the position of an element in an ordered sequence that "falls in" the bucket at          for(inti = 1; i < max-min; i++) {   -            Buckets[i] = buckets[i] + buckets[i-1];   -         }   -         //Copy the elements in data into the TMP array completely -System.arraycopy (data, 0, TMP, 0, data.length);  -         //places the elements of the pending sequence into place according to the information in the buckets array in          for(intK = data.length-1; K >= 0; k--) {   -             Data[--buckets[tmp[k]-min]] = tmp[k];  to         }   +}

Based on the idea of bucket sorting and counting sorting, the base order is realized:

Each group of keywords in the element to be sorted is then sequentially allocated in the bucket.

1      Public int[] Radixsortbuckets (int[] Array,intRadix) {2         //find the largest element in the sequence to be sorted3         intmax = Array[0];4          for(inti = 1; i < Array.Length; i++) {5             if(Array[i] >max) {6Max =Array[i];7             }8         }9         //determines the number of bits of the largest element maxbitsTen         intMaxbits = 0; One          while(Max > 0) { Amax = MAX/10; -maxbits++; -         } the          -         int[] Temparray =New int[Array.Length];//for staging elements -         int[] Buckets =New int[Radix];//for bucket sorting -         intRate = 1; +         //maxbits sub-allocation and collection -          for(inti=0; i< maxbits;i++){ +             //copies the elements in an array to the arraytemp array completely ASystem.arraycopy (array, 0, Temparray, 0, array.length); atArrays.fill (buckets, 0);//Resetting the buckets array -              -             //Assign: Calculates the sub-keywords of each element to be sorted and adds the number of times to the corresponding bucket -              for(intj=0;j<temparray.length;j++){ -                buckets[(temparray[j]/rate)%radix] + +; -             } in             //calculates the position of an element in an ordered sequence that "falls in" the bucket -              for(intk=1;k<buckets.length;k++){ to                Buckets[k] = buckets[k]+buckets[k-1]; +             } -              the             //Collect: Sorts the specified data by sub- keywords *              for(intm=temparray.length-1;m>=0;m--){ $                 intsubkey = (temparray[m]/rate)%Radix;Panax Notoginseng                Array[--buckets[subkey]] = temparray[m]; -             } the              +Rate *=Radix; A         } the         returnArray; +}

Java implementation based on the idea of bucket sorting and counting sorting to realize the base order

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.