Bottom-up sorting-c #

Source: Internet
Author: User

The bottom-up sorting method is much faster than the Bubble sorting method. The basic idea is: first, arrange the order of two groups, one group, and the other four groups ....... 8 ........ there is a problem until all rows are completed. During the intermediate transition, a temporary array is required to save the data. In fact, each segment is a variant of the insert sort method. The Code is as follows: [csharp] private int [] sortedData; public int [] Sort (int [] data) {sortedData = new int [data. length]; int t = 1; while (t <data. length) {int dt = t * 2; int I = 0; while (I + dt <= data. length) {merge (data, I, I + t, I + dt-1); I = I + dt;} if (I + t <data. length) {merge (data, I, I + t, data. length-1);} t = t * 2;} return sortedData;} private void merge (int [] data, int index1, int index2, int end) {int j = index1; int k = index2; int I = index1; while (j <index2 & k <= end) {if (data [j] <data [k]) {sortedData [I] = data [j]; j ++; I ++ ;} else {sortedData [I] = data [k]; k ++; I ++ ;}} if (j = index2) {while (k <= end) {sortedData [I] = data [k]; k ++; I ++ ;}} else {while (j <index2) {sortedData [I] = data [j]; j ++; I ++ ;}} I = index1; for (; I <= end; I ++) {data [I] = sortedData [I] ;}}

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.