快速排序

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
    int NO;
    int Age;
    char Name[50];
}Student;

typedef struct{
    int StudentCount; 
    Student *data;
}Sqlist;

int Partition(Sqlist &L,int low,int high)
{
    Student pivot=L.data[low];
    while(low<high)
    {
        while(low<high&&L.data[high].NO>=pivot.NO)
            high--;
        L.data[low]=L.data[high];
        while(low<high&&L.data[low].NO<=pivot.NO)
            low++;
        L.data[high]=L.data[low];
    }
    L.data[low]=pivot;
    return low;
}

void QuickSort(Sqlist &L,int low,int high)
{
    if(low<high)
    {
        int pivotpos=Partition(L,low,high);
        QuickSort(L,low,pivotpos-1);
        QuickSort(L,pivotpos+1,high);
    }
}

int main()
{
    Sqlist L;
    L.StudentCount=4;
    L.data = (Student *)malloc(L.StudentCount * sizeof(Student));
    L.data[0].Age=20;
    strcpy(L.data[0].Name,"zhangsan");
    L.data[0].NO=1115;
    L.data[1].Age=20;
    strcpy(L.data[1].Name,"lisi");
    L.data[1].NO=1112;
    L.data[2].Age=20;
    strcpy(L.data[2].Name,"wanger");
    L.data[2].NO=1113;
    L.data[3].Age=20;
    strcpy(L.data[3].Name,"mazi");
    L.data[3].NO=1114;
    
    QuickSort(L,0,L.StudentCount-1);
    for (int i = 0; i < L.StudentCount; i++) {
        printf("NO: %d, Age: %d, Name: %s\n", L.data[i].NO, L.data[i].Age, L.data[i].Name);
    }
    return 0;
}

 

posted on 2023-12-02 15:27  四马路弗洛伊德  阅读(8)  评论(0)    收藏  举报

导航