A method of C implementation for fast sequencing of classical algorithms

Source: Internet
Author: User

The fast sort implemented here is a reference to the pseudo-code in the introduction to the algorithm, although the pseudo-code was previously knocked over, but now the pseudo-code, it is a bit hard to knock yourself. <-_->!!

In particular, the introduction of the algorithm in the fast-line method, and the class is not the same, the main lesson is to use the first two subscript to achieve, and the introduction of the algorithm, mainly from the front to the back, in order to judge, will be less than Privot value all moved to the front of I. Here I refers to the upper bounds of the subscript less than the Privot value.

The source code is as follows:

//===================== "quick Sort" ==================//@ author:zhyh2010//@ date:20150606//@ version:1.0//@ Description://===================== "quick Sort" ==================#include <stdio.h>#include <stdlib.h>#include <time.h>#define NUMintArr[num] = {0};voidInit () {time_t TM;    Time (&AMP;TM); Srand ((unsigned int) tm);intMax_item = -; for(inti =0; I! = NUM; i++) Arr[i] = rand ()% Max_item;}voidSwapint* PA,int* Pb) {intAA = *PA;intbb = *PB;    aa = aa ^ bb;    bb = aa ^ bb;    aa = aa ^ bb;    *PA = AA; *PB = BB;}intPartitionintLowintHigh) {inti = low-1;//mark the upper limit of <= Privot    intPrivot = Arr[high]; for(intj = Low; J! = high; J + +) {if(Arr[j] <= privot)//Move the small value to the front of ISwap (&arr[++i], &arr[j]); } Swap (&arr[i +1], &arr[high]);returni +1;}voidDisplayint* arr) { for(inti =0; I! = NUM; i++)printf("%-10d", Arr[i]);printf("\ n");}voidQuickSort (intLowintHigh) {if(Low >= High)return;intMID = partition (low, high); QuickSort (Low, mid-1); QuickSort (Mid +1, high);}voidMain () {init ();printf("Quick sort before \ n");    Display (arr); QuickSort (0, NUM-1);printf("Quick sort after \ n"); Display (arr);}

A method of C implementation for fast sequencing of classical algorithms

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.