Sorting Algorithm (2)

Source: Internet
Author: User

I. Sort buckets

(1) Algorithm Description: assume that the value of the array element ranges from 0 ~ N-1. We need n buckets marked as 0, 1, 2,..., n. If the element value is I, the element is put into the bucket I. Then ~ Put the elements in the n-1 bucket back into the original array to obtain the sequence from small to large.

Public static <E> void bucketsort (E [] list) {// list is an array E [] buckets = (E []) New Java. util. arraylist [N]; // put the element into the bucket for (INT I = 0; I <list. length; I ++) {int key = list [I]; If (buckets [Key] = NULL) {buckets [Key] = new Java. util. arraylist ();} buckets [Key]. add (list [I]);} // put the elements in the bucket back into the array int K = 0; For (INT I = 0; I <buckets. length; I ++) {If (buckets [I]! = NULL) {for (Int J = 0; j <buckets [I]. size (); j ++) {list [k ++] = buckets [I]. get (j );}}}}

(2) Algorithm Implementation

Package algorithm; import Java. util. arraylist; public class bucketssort {public static <E> void bucketsort (E [] list, int max) {// list is an array E [] buckets = (E []) new Java. util. arraylist [Max + 1]; // declare for (INT I = 0; I <list. length; I ++) {int key = (integer) list [I]; If (buckets [Key] = NULL) {buckets [Key] = (e) New Java. util. arraylist (); // initialization} (arraylist) buckets [Key]). add (list [I]);} int K = 0; For (INT I = 0; I <buckets. length ; I ++) {If (buckets [I]! = NULL) {for (Int J = 0; j <(arraylist) buckets [I]). size (); j ++) {list [k ++] = (E) (arraylist) buckets [I]). get (j) ;}}} public static void main (string [] ARGs) {INTEGER [] list = {8, 3, 2, 4, 7, 9}; int max = 9; bucketssort. <integer> bucketsort (list, max); For (integer U: List) system. out. print (U + ",");}}

(3) Conclusion: the bucket sorting time is O (n + n), and N indicates the size of the linear table. Bucket sorting is not based on comparative sorting, so it is better than the lower limit O (nlogn) of the comparative sorting algorithm ). The bucket sorting is stable.

 

Ii. Base sorting

1. Least Significant digit first (LSD) Method

(1) algorithm description:

Step 1: create 10 buckets, corresponding to the number 0-9. Location keyword k = 1, indicating that the bucket starts from the lowest Bit.

Step 2: traverse the sequence to be sorted. The number in the sequence is allocated to the bucket corresponding to the K-digit number.

Step 3: traverse the bucket and store the data in the bucket from 0-9 to the original array in sequence!

Step 4: If K is equal to the maximum number of digits in the sequence, the sorting ends. Otherwise, go to step 2 with K = k + 1.

2. Most significant digit first (MSD) Method

(1) algorithm description:

Starting from the highest bit, we can actually guarantee an ascending sequence from small to large! However, if the number of digits is the same, it is not necessary! Actually, the bucket class is out of order, and the bucket class is out of order! At this time, the idea of recursion takes effect! Since the bucket is out of order, we focus on processing the data in the bucket regardless of the bucket. Starting from the next high level, create 10 more buckets and put the data in the bucket. The data will be processed in the first way until it is processed to the second bit!

3. Algorithm Implementation

Import Java. util. arraylist; public class radixsort {public static void main (string [] ARGs) {INTEGER [] A = {422,333,655,599,799, 422,333,655,599,799}; integer [] B = }; radixsort. LSD (A, 3); system. out. println (); radixsort. MSD (B, 3); For (integer U: B) system. out. print (U + ",") ;}// the least significant digit first method @ suppresswarnings ("unchecked") public static void LSD (integer [], int d) {Array List [] buckets = new Java. util. arraylist [10]; // data has the D-digit for (INT x = 0; x <D; X ++) {// put the data into the bucket for (INT I = 0; I <. length; I ++) {int [] r = getr (A [I], d); If (buckets [R [x] = NULL) buckets [R [x] = new Java. util. arraylist <integer> (); buckets [R [x]. add (A [I]) ;}// put the data in the bucket in the array int K = 0; For (Int J = 0; j <10; j ++) {If (buckets [J]! = NULL) {for (INT y = 0; y <buckets [J]. size (); y ++) {A [k ++] = (integer) buckets [J]. get (y);} buckets [J]. clear (); // clear the bucket. Otherwise, the elements are repeatedly clustered, leading to an array a subscript exception }}// print the data for (integer U: a) system. out. print (U + ",") ;}// the highest priority (most significant digit first) method public static void MSD (integer [] A, int d) {MSD (, 0,. length, d);} public static void MSD (integer [] A, int N1, int N2, int d) {// for a [N1, N1 + N2) partial Element sorting. N1 is the starting subscript of the sub-array and N2 is the number of elements. If (D> 0) {Array List [] buckets = new Java. util. arraylist [10]; // put the array data into the bucket for (INT I = N1; I <N1 + N2; I ++) {int [] r = getr (A [I], d); If (buckets [R [D-1] = NULL) buckets [R [D-1] = new Java. util. arraylist <integer> (); buckets [R [D-1]. add (A [I]) ;}// put the data in the bucket back into the array for (Int J = 0; j <10; j ++) {int num = 0; // record the number of elements in each bucket if (buckets [J]! = NULL) {for (INT y = 0; y <buckets [J]. size (); y ++) {A [N1 ++] = (integer) buckets [J]. get (y); num ++;} buckets [J]. clear (); // clear the bucket. Otherwise, the elements are repeatedly clustered, leading to an array a subscript exception. This step can be omitted for the MSD method, because each recursion creates a new buckets array without reusing the buckets array} MSD (A, n1-num, num, D-1 ); // recursive sorting of elements in each bucket} // returns an array composed of various data types, public static int [] getr (int n, int D) {int [] r = new int [d]; for (INT I = 0; I <D; I ++) {R [I] = n % 10; N = N/10;} return r ;}}

 

Iii. External sorting

To sort data stored in external files, you must first send the data to the memory and then sort the data. However, when the file size is too large, all data cannot be sent to the memory at the same time. Solution:

(1)RepeatedReads files into an array, calls the internal Sorting Algorithm to sort the array, and then outputs the array to a temporary file;

(2) merge each pair of ordered segments into a larger ordered segment, and store the new segment to the new temporary file. Continue the same process until an ordered segment is obtained. (Merge Sorting)

Sorting complexity: O (nlogn)

Sorting Algorithm (2)

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.