6-7 快速排序 (15 分)

给一个无序表,使用快速排序算法对它进行排序。

函数接口定义:

int Partition(SqList &L,int low,int high);  
void QuickSort(SqList &L, int low, int high);

其中L是待排序表,low和high是排序的区间。

裁判测试程序样例:

#include <iostream>
using namespace std;

#define MAXSIZE 50
typedef int KeyType;

typedef  struct                     
{ KeyType  key;                                             
} ElemType;  

typedef  struct
{ ElemType   r[MAXSIZE+1]; 
  int   length;
} SqList;                      

void Create(SqList &L)
{ int i;
  cin>>L.length;
  for(i=1;i<=L.length;i++)
     cin>>L.r[i].key;   
}

void Output(SqList L)
{ int i;
  for(i=1;i<=L.length;i++)
     cout<<L.r[i].key<<" ";
  cout<<endl;;
}

int Partition(SqList &L,int low,int high);  
void QuickSort(SqList &L, int low, int high);

int main () 
{  SqList L;  int low,high;
   Create(L);
   low=1; high=L.length;
   QuickSort(L,low,high);
   Output(L);
   return 0;
}

/* 请在这里填写答案 */

输入样例:

在这里填写一组输入
5
3 1 9 5 7

结尾无空行

输出样例:

1 3 5 7 9

结尾无空行

输入样例:

在这里填写一组输入
5
0 -1 8 -1 2

结尾无空行

输出样例:

-1 -1 0 2 8

结尾无空行

int Partition ( SqList  &L,int low,  int  high ){
	L.r[0].key=L.r[low].key;
	int pivotkey=L.r[0].key;
	while(low<high){
		while(low<high&&L.r[high].key>=pivotkey) high--;
		L.r[low].key=L.r[high].key;
		while(low<high&&L.r[low].key<=pivotkey) low++;
		L.r[high].key=L.r[low].key;
	}
	L.r[low].key=L.r[0].key;
	return low;
} 
void QuickSort ( SqList  &L,int low,  int  high ) 
{  int  pivotloc;
   if(low<high)
    {  
	pivotloc = Partition(L, low, high ) ;
        QuickSort (L, low, pivotloc-1) ; 
        QuickSort (L, pivotloc+1, high );
     }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值