码迷,mamicode.com
首页 > 编程语言 > 详细

快速排序

时间:2019-10-11 00:35:08      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:代码   排序   i++   str   clu   快速排序   oid   def   c语言   

代码

#include<iostream>
/*一趟快速排序*/
#define N 8
int Partition(int A[], int low, int high) {
    int pivot = A[low];
    while (low<high)
    {
        while (low < high&&A[high] >= pivot) --high;
        A[low] = A[high];
        while (low < high&&A[low] <= pivot) ++low;
        A[high] = A[low];
    }
    A[low] = pivot;
    return low;
}
/*快速排序*/
void QuickSort(int A[],int low,int high) {
    
    if (low < high) {
        int pivotops = Partition(A, low, high);
        QuickSort(A, low, pivotops - 1);
        QuickSort(A, pivotops + 1, high);
    }
}
/*输出数组*/
void OutPrint(int A[]) {
    int i;
    for (i = 0; i < N; i++)
    {
        printf("%d  ", A[i]);
    }
}
int main() {
    int i,low=0,high=N-1;
    int A[N] = { 48, 62, 35, 77, 55,14,35,98 };
    printf("排序前数组\n");
    OutPrint(A);
    QuickSort(A, low, high);
    printf("\n排序后数组\n");
    OutPrint(A);
    system("pause");
    return 0;
    
}

快速排序

标签:代码   排序   i++   str   clu   快速排序   oid   def   c语言   

原文地址:https://www.cnblogs.com/brainstorm-yc/p/11651273.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!