The most understandable qsort Sorting Algorithm

Source: Internet
Author: User

The Quick Sort Algorithm Program can be written in a strange way, but the most understandable individual thinks it is still as follows:

#include<stdio.h>#include<time.h>#include<stdlib.h>void swap(int *a ,int *b){    int t = *a;    *a = *b;    *b = t;}int partition(int array[],int l,int r){    int pivot = array[r];    int curpos = l;    int j ;    for( j = l;j<r;j++)    {        if(array[j] < pivot)        {            swap(&array[j],&array[curpos]);            curpos++;        }    }    swap(&array[r],&array[curpos]);    return curpos;    }void quicksort(int array[],int l,int r){    int interval;        if(l < r)    {        interval = partition(array,l,r);        quicksort(array,l,interval-1);        quicksort(array,interval+1,r);            }        }int test_quicksort(){    int number = 10000000;    printf("hehe:%d\n",number);    int *array = (int*) malloc(number*sizeof(int));    if (array == NULL)    {        printf("malloc failed\n");        return -1;    }    int i;    printf("----------------------------------------before quick sort--------------\n");    srand(time(NULL));    for(i = 0;i<number;i++)    {        array[i] = rand()%100000000;        //printf("\tarray[%d] = %d\n",i,array[i]);    }    printf("----------------------------------------after quick sort-----------------\n");        quicksort(array,0,number-1);    for(i = 0;i<number;i++)    {        //printf("\tarray[%d] = %d\n",i,array[i]);    }    printf("ok\n");    return 0;}int main(){    test_quicksort();}

No comments. Do you understand?

 

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.