排序_快排

快排的基本思想:通过一趟排序使得要排序的数据分割成两个独立的部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,来使整个数据变的有序。

假设要排序的数据为n个,用数组将这n个数据存储起来从arr[0]到arr[n-1],首先任意选取一个数据(通常选用数组的第一个数arr[0])作为关键数据,然后将所有比它小的数都放到它左边,所有比它大的数都放到它右边,这个过程称为一趟快速排序。然后按照这个方式进行多趟排序直到数据有序

一趟快排的流程是:
<1>、设置两个变量i、j,排序开始的时候:i=0,j=N-1;(分别表示第一个和最后一个的下标)
<2>、以第一个数组元素作为关键数据,赋值给key,即key=arr[0];
<3>、从j开始向前搜索,由后向前(j--),找到第一个小于key的值A[j],将arr[j]和arr[i]的值交换;
<4>、从i开始向后搜索,由前向后(i++),找到第一个大于key的值A[i],将arr[i]和arr[j]的值交换;
<5>、重复第3、4步,直到i=j; (3,4步中,没找到符合条件的值,即3中A[j]不小于key,改变j的值(j--),直到找到符合条件的值,4中A[i]不大于key的时候改变i的(i++),直到找到符合条件的值。找到符合条件的值进行交换的时候i, j指针位置不变。且当i==j这一过程一定是i+或j-完成的时候,此时令循环结束)。


#include<stdio.h>
#include<stdlib.h>
#include<stack>
#include<time.h>

void Quick(int arr[], int startindex, int endindex);
int partition(int arr[], int left, int right);
void Show(int arr[], int len)'

 

void QuickSort(int arr[], int len)
{
    if(arr == NULL || len<2)
    return ;
    Quick(arr, 0, len - 1);
}

void Quick(int arr[], int startindex, int endindex)
{
    if (startindex < endindex)
    {            
        int left = -1;
        int right = -1;
        int k = partition(arr, startindex, endindex);
        Quick(arr, startindex, k - 1);
        Quick(arr, k + 1, endindex);
    }
}

int partition(int arr[], int left, int right)
{
    int key = arr[left];
    while (left < right)
    {
        while (left < right && arr[right] >= key)right--;
        arr[left] = arr[right];
        while (left < right && arr[left] <= key)left++;
        arr[right] = arr[left];
    }
    arr[left] = key;
    return left;
}

void Show(int arr[], int len)
{
    for (int i = 0; i < len; ++i)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

void Quick_stack(int arr[], int startindex, int endindex)
{
    std::stack<int> q;
    if (startindex < endindex)
    {
        q.push(startindex);
        q.push(endindex);
        int l = -1;
        int r = -1;
        while (!st.empty())
        {
            r = q.top(); q.pop();
            l = q.top(); q.pop();
            int k = partition(arr, l, r);
            if (k + 1 < r)
            {
                q.push(k + 1);
                q.push(r);
            }
            if (l < k - 1)
            {
                q.push(l);
                q.push(k - 1);
            }
        }
    }
}
void Quick_queue(int arr[], int startindex, int endindex)
{
    queue<int> s;
    if(startindex >= endindex) return ;
    s.push(startindex);
    s.push(endindex);
    while(!s.empty())
    {
        int  left = s.front();
        s.pop();
        int  left = s.front();
        s.pop();        
        int mid = partition(arr,left ,left );
        if(left < mid-1)
        {
            s.push(left );
            s.push(mid-1);
        }
        if(mid+1 < left )
        {
            s.push(mid+1);
            s.push(left );
        }
    }
}

int main()
{
    int arr[] = { 32, 23, 28, 5, 2, 31, 21, 1, 9, 42, 53, 22 };
    int len = sizeof(arr) / sizeof(arr[0]);
    QuickSort(arr, len);
    Show(arr, len);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值