Binary Sorting Algorithm (stable and Minimum switching times sorting)

Source: Internet
Author: User

# Include <iostream. h> void swap (int & a, int & B) // implement the simple exchange between two data elements a and B {int t = a; a = B; B = t ;} void swap (int & a, int & B, int & c) // implements the minimum number of exchanges between the three elements, so that a <= B <= c; the function implementation can be obtained from the classification discussion that can be exchanged by three elements {if (a> = B & B> c) | (a> B & B = c) swap (a, c); else if (a> B & B <c) {if (a <= c) swap (a, B); else {swap (a, B); swap (B, c );}} else if (a <B & B> c) {if (a <= c) swap (B, c); else {swap (B, c); swap (, b) ;}} void swap (int A [], int a, int B, int c) // This function implements the conversion function, simplify array calling to simple comparison and exchange of the three elements {swap (A [a], A [B], A [c]);} int sortcheck (int A [], int n) // implement carry and return values to indicate whether the sorting has been completed {int check = 0; for (int I = 0; I + 1 <n; I + = 2) {if (A [I]> A [I + 1]) {swap (A [I], A [I + 1]); check ++ ;} // Step 1 if (I + 2 <n & A [I + 1]> A [I + 2]) check ++; // Step 2} return check ;} void sort (int A [], int n, int r = 1) // implement binary sorting, sort the corresponding elements in step 2 {// The basic idea is to regard the address of array A as A binary tree, and implement the function by sorting the binary tree, make R <= L <= R root node value less than left subtree value less than right subtree value if (2 * r + 1 <= n) {swap (A, R-1, 2 * R-1, 2 * r); sort (A, n, 2 * r); sort (A, n, 2 * r + 1); swap (A, R-1, 2 * R-1, 2 * r); // this step is a backtracking algorithm, re-order the root node after left and right subtree sorting} else if (2 * r = n & A [r-1]> A [2 * r-1]) swap (A [r-1], A [2 * r-1]);} int Sort (int A [], int n) // This function is used to wrap the sorting population to call {int count = 0; while (sortcheck (A, n) // when sorting is not completed, {int r = 1; sort (A, n, r); // call two groups of sorting count ++;} return count; // The Return Value of count indicates the number of times the two groups are called together with the work} void BinarySort (int A [], int n) {cout <endl <"the array before sorting is:" <endl; for (int I = 0; I <n; I ++) cout <A [I] <"; cout <endl <" Total sorting experience "<Sort (A, n) <"period, involved in sorting elements" <n <"Period" <endl; cout <endl <"the sorted array is:" <endl; for (I = 0; I <n; I ++) cout <A [I] <""; cout <endl;} void main () {int A [35] = {, 3, 2, 1, 0}; BinarySort (A, 35 );}

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.