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

📄 quicksort.cpp

📁 包含8种常用的排序。如快速排序
💻 CPP
字号:
#include <iostream.h>
#include <stdio.h>
#include <time.h>
#include<stdlib.h>
#include <algorithm>
using namespace std;
#define N 10000
int a[N];

int Partition(int left,int right)
{ 
 int i=left;
  int j=right+1;
  do{
	  do i++;while(a[i]<a[left]);
	  do j--;while(a[j]>a[left]);
	  if(i<j) 
		  swap(a[i],a[j]);
  }while(i<j);
 swap(a[left],a[j]);
 return j;
}

 void QuickSort(int left,int right)
{  
   
   int k;
   if(left<right)
   {
     k=Partition(left,right);
     QuickSort(left,k-1);
     QuickSort(k+1,right);
   }
 }

 void main()
 { clock_t start=clock();/*程序开始运行的时间*/
   int i;

   //srand((unsigned)time(NULL));//随时间的不同产生不同的随机数
   for(i=0;i<N;i++)
		a[i]=rand()%N+1;

   QuickSort(0,N-1);
   for(i=0;i<N-1;i++)
   {
	  cout<<a[i]<<"->";
   }
   cout<<a[N-1]<<"\n";
   clock_t end=clock();/*程序结束运行的时间*/
   cout<<"整个程序运行的时间为:";
   cout<<(end-start)<<"毫秒\n";
}

⌨️ 快捷键说明

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