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

📄 yes.cpp

📁 这个是快速排序法的算法实现
💻 CPP
字号:
#include<iostream.h>
#include<stdlib.h>
#include<malloc.h>
#define list_init_size 21  
typedef struct{
	int key;
}redtype;
typedef struct{
	redtype *r;
	int length;
}sqlist;

void partition(sqlist&L,int s,int t)
{
	int low=s,high=t;
	int m,n;
	int pivotkey;
	if(s<t)
	{
	  L.r[0]=L.r[s];
	  pivotkey=L.r[s].key ;
	   while(low<high)
	   {
		 while(low<high&&L.r[high].key >=pivotkey) --high;
		 L.r[low]=L.r[high];
		 while(low<high&&L.r[low].key <=pivotkey) ++low;
	     L.r[high]=L.r[low];
	   }
     L.r[low]=L.r[0];
	 cout<<"{"<<" ";
	 for(m=s;m<low;m++)
		 cout<<L.r[m].key<<" " ;
	 cout<<"}"<<" "<<L.r[low].key<<" "<<"{"<<" ";
     for(n=high+1;n<=t;n++)
		 cout<<L.r[n].key <<" ";
	 cout<<"}";
	 cout<<endl;
	 partition(L, s,high-1);
     partition(L, high+1,t);
	
	}
}
void main()
{    
    int a,i;
	int n=0;
	sqlist L;
	cout<<"请输入要排序的数列,以零作为结束符:"<<endl;
    L.r=(redtype*)malloc(list_init_size*sizeof(redtype));
	if(!L.r) exit(0);
	L.length =0;
	cin>>a;
	while(a!=0)
	{
		L.r[++n].key =a;
        cin>>a;
		L.length ++;
	}
	cout<<endl;
	cout<<"待排序的数列为:"<<endl;
	for(i=1;i<=L.length ;i++)
		cout<<L.r[i].key<<"   ";
	cout<<endl<<endl;
    partition(L,1,n);
	cout<<"用快速排序法升序排序后的序列为:"<<endl;
	for(i=1;i<=L.length ;i++)
		cout<<L.r[i].key<<"   ";
	cout<<endl;
}
 

⌨️ 快捷键说明

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