C语言实现快速排序算法

#include <stdio.h>
#define MAXSIZE  10 
typedef  int  KeyType; 
typedef  struct {
   KeyType   key;             // 关键字项
 //   int   otherinfo;  // 其它数据项
} RedType;                     

typedef  struct {
    RedType    r[MAXSIZE+1]; // r[0]闲置
    int               length;            // 顺序表长度
} SqList;                                
 

int Partition (RedType R[], int low, int high) {

int pivotkey;
	R[0] = R[low];  pivotkey = R[low].key;  // 枢轴  
while (low<high) {

while(low<high&& R[high].key>=pivotkey)
        -- high;      // 从右向左搜索
R[low] = R[high];
while (low<high && R[low].key<=pivotkey) 
        ++ low;      // 从左向右搜索
R[high] = R[low];
}
R[low] = R[0];     return low;

}// Partition         


void QSort (RedType  R[],  int s,  int  t ) {
  // 对记录序列R[s..t]进行快速排序
  int pivotloc;
	if (s < t) {             // 长度大于1
    
pivotloc = Partition(R, s, t);
                        // 对 R[s..t] 进行一次划分
QSort(R, s, pivotloc-1);
      // 对低子序列递归排序,pivotloc是枢轴位置
QSort(R, pivotloc+1, t); // 对高子序列递归排序

  }
} // QSort


void QuickSort( SqList & L) {
   // 对顺序表进行快速排序
     QSort(L.r, 1, L.length);
} // QuickSort

void main()
{
	SqList  L={{0,21,34,12,6,78,45,10,20,70,30},10};
    QuickSort(L);
	int i;
	for(i=1;i<=L.length;i++)
		printf("%d  ",L.r[i]);


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值