8 essential sorting for programmers (4) --- Merge Sorting and base sorting (implemented in Java)

Source: Internet
Author: User

Last article: 8 major sorting tasks required by programmers (3) ------- Bubble sorting and quick sorting (implemented in Java)

 

I have sorted the eight types of sorting into Word documents. If you are interested, you can download them:Http://download.csdn.net/detail/pzhtpf/4313015

 

 

7. Merge and sort

 

(1) Basic sorting: the Merge Sorting method combines two (or more) ordered tables into a new ordered table, that is, the sequence to be sorted is divided into several subsequences, each sub-sequence is ordered. Then combine the ordered subsequences into the overall ordered sequence.

(2) instance:

(3) implemented in Java

Import Java. util. arrays; public class mergingsort {int A [] = {, 25, 53,51}; publicmergingsort () {sort (A, 0,. length-1); For (INT I = 0; I <. length; I ++) system. out. println (A [I]);} public void sort (INT [] data, int left, int right) {// todo auto-generated method stub if (left <right) {// locate the intermediate index int center = (left + right)/2; // recursive sort (data, left, center) on the left array ); // recursive sort (data, center + 1, right) for the right array; // merge Merge (data, left, center, right );}} public void merge (INT [] data, int left, int center, int right) {// todo auto-generated method stub int [] tmparr = new int [data. length]; int mid = center + 1; // index of the middle array of the third record int third = left; int TMP = left; while (left <= center & Mid <= right) {// extract the smallest value from the two arrays and put it into the intermediate array if (data [left] <= data [Mid]) {tmparr [third ++] = data [left ++];} else {tmparr [third ++] = data [Mid ++];} // The remaining parts are sequentially placed in the middle array while (mid <= right) {tmparr [third ++] = data [Mid ++];} while (left <= center) {tmparr [third ++] = data [left ++];} // copy the content in the intermediate array back to the original array while (TMP <= right) {data [TMP] = tmparr [TMP ++];} system. out. println (arrays. tostring (data ));}}


8. Base sorting
 
(1) Basic Idea: unify all the values to be compared (positive integers) into the same digit length, and add zero before the numbers with shorter digits. Then, sort the data by bit. In this way, the sequence is changed to an ordered sequence after the ranking is completed until the sorting is completed by the highest bit.
(2) instance:

(3) implemented in Java
Import Java. util. arraylist; import Java. util. list; public class radixsort {int A [] =, 51}; Public radixsort () {sort (a); For (INT I = 0; I <. length; I ++) system. out. println (A [I]);} public void sort (INT [] array) {// first determine the number of sorted partitions; int max = array [0]; for (INT I = 1; I <array. length; I ++) {If (array [I]> MAX) {max = array [I] ;}} int time = 0; // number of digits to be determined; while (max> 0) {max/= 10; time ++;} // create 10 queues; List <arraylist> queue = new arraylist <arraylist> (); for (INT I = 0; I <10; I ++) {arraylist <integer> queue1 = new arraylist <integer> (); queue. add (queue1);} // time allocation and collection; For (INT I = 0; I <time; I ++) {// array element allocation; for (Int J = 0; j <array. length; j ++) {// obtain the time + 1 digit of the number. Int x = array [J] % (INT) math. pow (10, I + 1)/(INT) math. pow (10, I); arraylist <integer> queue2 = queue. get (x); queue2.add (array [J]); queue. set (x, queue2);} int COUNT = 0; // element counter; // collect queue elements; For (int K = 0; k <10; k ++) {While (queue. get (k ). size ()> 0) {arraylist <integer> queue3 = queue. get (k); array [count] = queue3.get (0); queue3.remove (0); count ++ ;}}}}}

 

 

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.