排序的定义

稳定排序和非稳定排序

对一组数据进行排序过程当中,如果遇到两个连着的数据的大小相等,排序结束后,两个数据的先后顺序不变的排序称为稳定排序,反之称为非稳定排序。

内排序和外排序

在排序工程当中代排序的数据始终存放于内存储当中称为内排序,如果排序工程当中有用到外部存储器,称为外排序。

  • 内排序方法
    •   插入排序:直接插入排序、折半插入排序、链表插入排序、shell(希尔)排序
    •   交换排序
    •   选择排序
    •   归并排序

快速排序的实现

代码


int main(){
    int data[N] = {0};
    srandom(10);
    int i = 0;

    for(i= 0; i<N; i++){
        data[i] = random() % 100;
    }

    printf("data1:\n");
    for(i= 0; i<N; i++){
        printf("%d ",data[i] );
    }
    puts("");

    //quick_sort(data,0,15-1);
    qsort(data, N, sizeof(int), compare);

    printf("data2:\n");
    for(i= 0; i<N; i++){
        printf("%d ",data[i] );
    }
    puts("");

    return 0;
}

```c
//p1:带比较的第一个参数地址
//p2:带比较的第二个参数地址
int compare(const void *p1, const void *p2) {
    return (*(const int *)p1 - *(const int *)p2);
}


int partion(int *p,int low,int high){
    int temp = p[low];

    while (low<high)
    {
        while (low<high && temp<=p[high])
        {
           high--;
        }
        p[low] = p[high];

        while (low<high && temp>=p[high])
        {
           low++;
        }
        p[high] = p[low];



    }
    p[low] = temp;

    return low;


}

int quick_sort(int *p,int low,int high){
    int tmp;
    if(p == NULL){
        return -1;
    }

    if(low >= high){
        return -1;
    }

    tmp = partion(p,low,high);
    quick_sort(p,low,high-1);
    quick_sort(p,low+1,high);

    return 0;

}