快速排序QuickSort

 #include <iostream>
using namespace std;

//......................获取支点索引..............................//
int KeyPivot(int List[], int i,int j)
{
 int x = List[i];//x中始终存放支点的值,首先支点KeyPivot在数组的第一个元素i位置
 while (i<j)
 {
  while ((i<j) && (x<=List[j]))
  {
   j--;
  }
  if (i<j)//即x>List[j],支点由i改为j
  {
   List[i] = List[j];
   i++;
  }
  while ((i<j) && (x>=List[i]))
  {
   i++;
  }
  if (i<j)//即x<List[j],支点由j改为i
  {
   List[j] = List[i];
   j--;
  }
 }
 List[i]=x;//i=j时退出循环,所有元素都比较完,找到支点位置

 return (i);
}

 

//............................QuickSort递归进行排序..........................//
void QuickSort(int List[],int startIndex,int lastIndex)

 int key;//key是索引
 if (startIndex < lastIndex)
 {
  key = KeyPivot(List,startIndex,lastIndex);
  QuickSort(List,startIndex,key-1);
  QuickSort(List,key+1,lastIndex);
 }
}


int main()
{
 int arr[8] = {49,38,65,97,76,13,27,69};
 int i;

 /*cout << "put out numbers: " << endl;
 for (i=0;i<8;i++)
 {
  cin >> arr[8];
 }*/
 cout << "output arrey before qsorting:" << endl;
 for (i=0;i<8;i++)
 {
  cout << arr[i] << ",";
 }
 cout << endl;
 QuickSort(arr,0,7);
 cout << "output arrey after qsorting:" << endl;
 for (i=0;i<8;i++)
 {
  cout << arr[i] << ",";
 }
 cout << endl;


 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值