快速排序

include

include "stdafx.h"

define N 7

void Print2(int *a)
{
for (int i = 0; i < N; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}

void Sort(int *a, int left,int right)
{
if (left >= right)
{
return;
}

int i = left;
int j = right;
int key = a[i];

while (i= a[i])
    {
        i++;
    }
    a[j] = a[i];

}
a[i] = key;
Sort(a,left,i-1);
Sort(a,i+1 ,right);

}

void Sort2(int *data, int left, int right)
{
if (left >= right)
{
return;
}
int i = left;
int j = right;
int key = data[i];

while (ikey) //需要从右边向左 直到发现一个比key小的萝卜
    {
        j--;
    }
    data[i] = data[j];              //取出来填旧坑,形成新坑

    while (i

}

int main()
{
int a[7] = { 2,1,4,5,7,6,3 };
Sort2(a,0,N-1);
Print2(a);
}

你可能感兴趣的:(快速排序)