排序算法习题2之快速排序

对给定关键字序号j(1

/*利用快速排序的划分思想在无序记录中查找给定关键字序号的记录*/
#include 
#include 
#include 
#define MAXSIZE 20
typedef int KeyType;
typedef int InfoType;

typedef struct
{
    KeyType key;
    InfoType otherinfo;
}RedType;

typedef struct
{
    RedType r[MAXSIZE+1];
    int length;
}SqList;

int Partition(SqList *L,int low,int high);
KeyType QSort(SqList *L,int low,int high,int i);
void QuickSort(SqList *L,int i);

int main()
{
    int c;
    SqList *L;
    L=(SqList *)malloc(sizeof(SqList));

    int i;
    printf("需排序的项数为:");
    scanf("%d",&L->length);

    for(i=0;ilength;i++)
    {
        printf("please input the number:");
        scanf("%d",&L->r[i+1].key);
    }
    printf("\n需要查找第几位?:");
    scanf("%d",&c);

    QuickSort(L,c);
    printf("\n");

    return 0;
}

/*交换顺序表L中子表L->r[low...high]的记录,使枢轴记录到位,并返回其位置*/
int Partition(SqList *L,int low,int high)
{
    int pivot;
    pivot=L->r[low].key;
    L->r[0]=L->r[low];
    while(lowr[high].key >= pivot)
            high--;
        L->r[low]=L->r[high];
        while(lowr[low].key <= pivot)
            low++;
        L->r[high]=L->r[low];
    }
    L->r[low]=L->r[0];
    return low;
}

KeyType QSort(SqList *L,int low,int high,int i)
{
    int pivot;
    pivot=Partition(L,low,high);
    if(i==pivot)
        return L->r[pivot].key;
    else if(ilength,i);
    printf("结果为:%d\n",t);
}

你可能感兴趣的:(排序习题)