Data structure---C language for fast sorting (multiple versions)

Source: Internet
Author: User

Quick sort basically has the following version

First, the domestic teaching materials two-way scanning version
Two, one-way scan version
Third, the version of randomization
Four or three-digit segmentation method
Five, non-recursive version


Here are my top two versions (more commonly used)

Version One:

Bidirectional Scan version:







The code is as follows:


Quick Sort (Version one)//pivot//Xin Yang # include <stdio.h> #include <stdlib.h> #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 ("========= fast sort Version One ==========\n"), int i = 0, J, t, Count = 0;int t;printf ("Enter the number of data to sort by: \ n"); scanf ("%d", &T); while (t--) {scanf ("%d", &a[i]);    i++;} QuickSort (0, i-1);p rintf ("sorted data is: \ n"), for (j = 0; J < i; J + +) {printf ("%d", A[j]);} return 0;}




Version two:

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;//data Interchange for the same address, which results in 0a=a^b;b=a^b;a=a^b;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structure---C language for fast sorting (multiple versions)

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.