注册 登录
编程论坛 数据结构与算法

快速排序

冲的一锤 发布于 2021-10-17 17:08, 3080 次点击
程序代码:

#include <stdio.h>
#include <stdlib.h>


 void QkSort(int *arr,int left ,int right){
     


     if (right>left){
         return;
     }
      int temp = arr[left];
     int i = left;
     int j = right;
   
     
     while(i!=j){
         while (arr[j]>=temp&&j>i)
         {
             j--;
         }
         while (arr[i]<=temp&&j>i)
         {
             i++;
         }

         if(j>i){
             int t = arr[i];
             arr[i] = arr[j];
             arr[j] =t;
         }
         
     }
   
    arr[left] = arr[i];
    arr[i] = temp;

    QkSort(arr, left, i - 1);
    QkSort(arr, i + 1, right);

 }

        int main(){
            int arr[] = {1, 32, 4, 56, 77, 55, 34, 12, 56, 75};
            int len = sizeof(arr) / sizeof(int);
            for (int i=0; i <= len - 1;i++){

                printf("%d ", arr[i]);
            }

            QkSort(arr, 0, len - 1);
            printf("\n");
            for (int i = 0; i <= len - 1;i++){
                printf("%d ", arr[i]);
            }
            
        }
1 回复
#2
apull2021-10-18 09:24
程序代码:

    if (right<=left){//if (right>left){
        return;
    }
1