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

📄 qs.cpp

📁 vc++2005编写的快速排序算法,将随机产生的一组数进行排序,学习参考一下,不错的.
💻 CPP
字号:
#include<iostream.h>
#include<stdio.h>
template<class Type>
void QuickSort(Type a[],int p,int r)
{
	if(p<r){
	int q=Partition(a,p,r);
	QuickSort(a,p,q-1);//sorting the left part 
	QuickSort(a,q+1,r);//sorting the right part
	}
}

template<class Type>
int Partition(Type a[],int p,int r)
{
    int i=p;
	int j=r+1;
	Type x=a[p];
//move the number to left which <x
//move the number to right which >x
	while(true){
	  while(a[++i]<x);
	  while(a[--j]>x);
	  if(i>=j) 
		  break;
	  swap(a[i],a[j]);
	}
    a[p]=a[j];
	a[j]=x;
	return j;
}


template <class Type>
void swap(Type &i,Type &j)
{
Type temp=i;
i=j;
j=temp;
}

void main(){
int a[100];
int m=99,n=0;
  cout<<"bfore sort:"<<endl;
  for(m=99;m>=0;m--)
  {a[99-m]=m;
   cout<<a[99-m];
  }
int p=0,r=99;
QuickSort(a,p,r);
   cout<<"\nafter sort:"<<endl;
for(n=0;n<100;n++)
   cout<<a[n];
   cout<<endl<<"done!!!"<<endl;
   getchar();
}

⌨️ 快捷键说明

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