c_cpp C ++中的快速排序算法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp C ++中的快速排序算法相关的知识,希望对你有一定的参考价值。

/*
* Quick Sort Algorithm
* Language: C++
* Created by: Harish R
*/

#include<iostream>

using namespace std;

int partition(int *a, int m, int n)
{
    int i,j,pindex,pivot;
    pindex = m;
    pivot = a[n];
    for(i=m;i<n;i++)
    {
        if(a[i] <= pivot)
        {
            swap(a[pindex], a[i]);
            pindex++;
        }
    }
    swap(a[pindex], a[n]);
    return pindex;
}

int quicksort(int *a, int m, int n)
{
    int index;
    if(m>=n)
        return 0;
    {
        index = partition(a,m,n);
        quicksort(a, m, index-1);
        quicksort(a, index+1, n);
    }
}

int main()
{
    int a[] = {7,2,1,6,8,5,3,4};
    int i;
    quicksort(a,0,7);
    cout <<"After Sorting" << endl;
    for(i=0;i<8;i++)
        cout <<a[i] <<endl;
}

以上是关于c_cpp C ++中的快速排序算法的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp C ++中的合并排序算法

数据结构之八大排序算法(C语言实现)

C/C++ 七大排序算法 之 “快速排序”

排序算法系列——快速排序

c ++排序矢量快速配对作为遗传学习算法的一部分

快速排序实现(快排)