标签:
快速排序是初学者比较难理解的几个算法之一,这里尽可简单化地讲解,希望能帮到大家。
快速排序基本步骤:
下面这幅图会帮助你理解。选中的pivot用蓝色表示:
原则上可以选择任何元素作为基准。
# choose pivot
swap a[1,rand(1,n)]# 2-way partition
k = 1
for i = 2:n, if a[i] < a[1], swap a[++k,i]
swap a[1,k]
→ invariant: a[1..k-1] < a[k] <= a[k+1..n]# recursive sorts
sort a[1..k-1]
sort a[k+1,n]
我们以数组arr[] = {2, 6, 4, 10, 8, 1, 9, 5, 11, 7}举个例子。
选最后一个元素作为pivot。
当PIVOT = 7的情况,所有比7小的数在7的左边,比7大的数在7右边,对左边部分和右边部分的数据在进行相同的排序,直到整个数组排完序。
下面是算法实现:
#include<stdio.h> //a simple function to swap two numbers void swap(int *i, int *j) { int temp = *i; *i = *j; *j = temp; } // a function to partition the array arr // having starting index as - start // and ending index as - end int partition(int arr[], int start, int end) { // we take the pivot to be the last element // that means all elements smaller // to it will be on left and larger on right int pivot = arr[end]; // taking i and j to define the range, we leave the pivot int i = start; int j = end-1; // loop till in range while(i<=j) { // keep moving till the left element is smaller than pivot while(arr[i]<pivot) i++; // keep moving left till right element is larger while(arr[j]>pivot) j--; // we need to swap the left and right if(i<=j) { swap(&arr[i],&arr[j]); i++; j--; } } // once partitioned, we need to put the pivot at correct place swap(&arr[i],&arr[end]); // return the position of pivot return i; } void performQuickSort(int arr[], int start, int end) { //the terminating condition for recursion if(start<end) { // get the partition index int p = partition(arr, start, end); // perform quick sort on left sub part performQuickSort(arr, start, p-1); // perform quick sort on right sub part performQuickSort(arr, p+1, end); } } //defining a function to perform merge sort on array arr[] of given size void quickSort(int arr[], int size) { performQuickSort(arr, 0, size-1); } // driver program to test the above function int main(void) { int i; int arr[10] = {2, 6, 4, 10, 8, 1, 9, 5, 3, 7}; quickSort(arr,10); printf("SORTED array:- "); for(i=0;i<10;i++) printf("%d ",arr[i]); return 0; }
标签:
原文地址:http://www.cnblogs.com/programnote/p/4724417.html