Data Structure-fast sorting by C language (multiple versions)

Source: Internet
Author: User

Data Structure-fast sorting by C language (multiple versions)

Quick sorting basically includes the following versions:

I,Two-way scanning of domestic textbooks
II,Unidirectional scan version
Iii. randomization version
4. Division of three numbers
5. Non-recursive Edition

 

Here are the first two versions (more common)

Version 1:

Bidirectional scan version:

 

 

 

 

 

 

The Code is as follows:

 

// Quick sorting (version 1) // With Pivot // Yang Xin # include
 
  
# Include
  
   
# Define MAXN 100int a [MAXN + 1], n; void QuickSort (int left, int right) {int I, j, t, temp; if (left> right) return; temp = a [left]; I = left; j = right; while (I! = J) {while (a [j]> = temp & I <j) {j --;} while (a [I] <= temp & I <j) {I ++;} if (I <j) {t = a [I]; a [I] = a [j]; a [j] = t ;}} a [left] = a [I]; a [I] = temp; QuickSort (left, I-1); QuickSort (I + 1, right);} int main () {printf (=========== Quick Sort version 1 ===========); int I = 0, j, t, count = 0; int T; printf (enter the number of data sorted :); scanf (% d, & T); while (T --) {scanf (% d, & a [I]); I ++;} QuickSort (0, I-1); printf (the sorted data is :); for (j = 0; j <I; j ++) {printf (% d, a [j]);} return 0 ;}
  
 

 

 

 

 

Version 2:

Single scan:

 

Void quickSort2 (int x [], int l, int r) {if (l> = r) return; int m = l; for (int I = l + l; I <= r; I ++) {if (x [I] <x [l]) {swap2 (x [++ m], x [I]);} swap2 (x [l], x [m]); quickSort2 (x, l, m-1); quickSort2 (x, m + 1, r );} void swap2 (int & a, int & B) {if (a = B) return; // the result of data exchange to the same address is 0a = a ^ B; B = a ^ B; a = a ^ B ;}


 

 

 

 

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.