main.cpp

来自「快速排序算法C实现。编译器使用的为dev C」· C++ 代码 · 共 42 行

CPP
42
字号
#include "stdio.h"
#include "iostream.h"
#include "stdlib.h"

void quickSort(int a[],int left,int right)
{
   int i,j,temp;
   i=left;
   j=right;
   temp=a[left];
   if(left>right)
      return;
   while(i!=j)/*找到最终位置*/
   {
      while(a[j]>=temp && j>i)
         j--;
      if(j>i)
         a[i++]=a[j];
       while(a[i]<=temp && j>i)
          i++;
       if(j>i)
          a[j--]=a[i];
         
   }
   a[i]=temp;
   quickSort(a,left,i-1);/*递归左边*/
   quickSort(a,i+1,right);/*递归右边*/
}

int main()
{
    int a[7]={8,2,6,12,1,9,5};
    int i;
    quickSort(a,0,6);
    /*排好序的结果*/
    for(i=0;i<7;i++)
        printf("%4d",a[i]);
    cout<<"\n";    
    system("pause");
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?