热门

最新

红包

立Flag

投票

同城

我的

发布
weixin_44740184
猥琐技术流
6 年前
trueweixin_44740184

// cpp array quick sort
#include<iostream>
using namespace std;

void print(int a[], int n)
{
    for(int j = 0; j < n; j++)
    {
        cout << a[j] << ' ';
    }
    cout << endl;
}

void swap(int *a, int *b)
{
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

int partition(int a[], int low, int high)
{
    int privotKey = a[low];
    while(low < high)
    {
        while(low < high && a[high] >= pri
votKey) --high;
        swap(&a[low], &a[high]);
        while(low < high && a[low] <= priv
otKey) ++low;
        swap(&a[low], &a[high]);
    }
    print(a, 8);
    return low;
}

void quickSort(int a[], int low, int high)
{
    if(low < high)
    {
        int privotLoc = partition(a, low,
high);
        quickSort(a, low, privotLoc -1);
        quickSort(a, privotLoc + 1, high);
    }
}

int main()
{
    int a[8] = {1, 5, 3, 6, 2, 4, 8, 7};
    cout << "initial value: ";
    print(a, 8);
    quickSort(a, 0, 7);
    cout << "result: ";
    print(a, 8);
}

CSDN App 扫码分享
分享
评论
点赞
打赏
  • 复制链接
  • 举报
下一条动态
立即登录