⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 a.cpp

📁 快速排序法 //基本的思想:通过一趟排序将待排的记录分割成独立的两部分
💻 CPP
字号:
// a.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include "iostream.h"
#include "stdlib.h"

//交换两变量值
void swap(int &a,int &b)
{
 int c;
 c=a;a=b;b=c;
}

//将数组分成两部分,前一部分的值均比后一部分值小
//返回分界点
int Partition(int data[],int low,int high)
{
 int pivokey;
 pivokey=data[low];
 while(low<high)
 {
  while(low<high&&data[high]>=pivokey)
   high--;
  swap(data[low],data[high]);

  while(low<high&&data[low]<=pivokey)
   low++;
  swap(data[low],data[high]);
 }
 return low;
}

//进行的递归的调用,达到排序的目的
void QSort(int data[],int low,int high)
{
 if(low<high)
 {
  int pivokey=Partition(data,low,high);
  QSort(data,low,pivokey-1);
  QSort(data,pivokey+1,high);
 }
}

void main()
{
 int i;
 int mydata[50];
 for(i=0;i<50;i++)
 {
  //每行显示 10 个数据
  if(i%10==0)
   cout<<endl;
  mydata[i]=rand()%100;
  cout<<mydata[i]<<"  ";  
 }

 QSort(mydata,0,49);

 cout<<endl<<endl;
 for(i=0;i<50;i++)
 {
  //每行显示 10 个数据
  if(i%10==0)
   cout<<endl;
  cout<<mydata[i]<<"  ";  
 }
}

⌨️ 快捷键说明

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