Fast sorting algorithm

Source: Internet
Author: User

The idea of quick sorting is divided into two parts of the data to be sorted into separate, part of the data is smaller than the other part of the data, and then according to this method of the two parts of the data are quickly sorted, the entire sequencing process can be recursive, in order to achieve the entire data into an ordered sequence.

The code is as follows:

basic structure of all sorting algorithms

typedef struct

{

int data[max_size + 1]; data

int length; length

}sqlist;

Exchange two data

void Swap (sqlist *list,int i,int j)

{

if (!list) return;

int temp = list->data[i];;

list->Data[i] = list->data[j];

list->Data[j] = temp;

}

void printfsqlist (sqlist *list)

{

if (list) {

for (int i =0; i< list->length; i++) {

printf("%d",list->data[i]);

}

}

}

swaps the records of a list sub-table, determines the pivot, and returns its position

int Partition (sqlist *list,int low ,int. )

{

int pivotkey;

int m = low + (high-low)/2;

if (list->data[low]>list->data[high]) {

Swap(list, low, high); swap left and right end, smaller at the beginning

}

if (list->data[m]>list->data[high]) {

Swap(list, high, m); swap the middle and right end to make the middle smaller

}

if (list->data[m]>list->data[low]) {

Swap(list, high, m); swaps the middle and left side, leaving a smaller

}

PivotKey = list->data[low];

while (low

while (Lowdata[high]>=pivotkey) {

high--;

}

Swap(list, low, high); Exchange, will be smaller than the pivot value to low ;

while (Lowdata[low]<=pivotkey) {

low++;

}

Swap(list, low, high);

}

return low ;

}

do a sort of list->data[low...high] . Low : minimum subscript, high: maximum subscript

void QuickSort (sqlist *list,int low ,Int. High )

{

int pivot; pivot

if (low

Pivot = Partition(list,low,high);

QuickSort(list, low, pivot-1);

QuickSort(list, pivot+1, high);

}

}

In the main method, add

{

int array[] = {7,6,9,ten,5,1,8};

sqlist liststruct;

printf("source data:");

sqlist *plist = &listStruct;

for (int i =0 ; i<7; i++) {

plist->Data[i] = array[i];

printf("%d",plist->data[i]);

}

plist->Length = 7;

printf("\ r \ n");

QuickSort(plist, 0, 6);

printf("final data:");

printfsqlist(plist);

}

On this basis can also be optimized for fast algorithms, there is time to engage in.

Fast sorting algorithm

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.