打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
用VC++类实现快速排序(并输出过程)



&&&&&&&&&&&&&&&&&&&&&&&&&&&&主函数&&&&&&&&&&&&&&&&&&&&&&
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "WangQi.h"
using namespace std;
#define MAX 100
void main(){
SeqList L;
int num;
cout<<"请输入要排序的元素个数:"<<endl;
cin>>num;
cout<<"请输入要排序的元素:"<<endl;
for(int i=1;i<=num;i++)
cin>>L.r[i];
L.length=num;
//输出排序前的顺序表
L.output(&L,1,L.length,-1);
L.quicksort(&L,1,L.length);
L.output(&L,1,L.length,-2);
}
&&&&&&&&&&&&&&&&&&&含有类定义的头文件&&&&&&&&&&&&&&&&&&&&&&&&&
#include <iostream>
using namespace std;
#define MAX 100
class SeqList{
public:
int r[MAX+1];
int length;

void output(SeqList *L,int low, int high,int pivotloc){
int i;

if(pivotloc==-1||pivotloc==-2){
  if(pivotloc==-1) 
       cout<<"初始状态:{"<<'\t';
  else cout<<"排序结果:{"<<'\t';
     for(i=low;i<=high;i++)
      cout<<L->r[i]<<'\t';
      cout<<"}";
      }else {
     cout<<"划分结果:{"<<'\t';
   for(i=low;i<pivotloc;i++)
      cout<<L->r[i]<<'\t';
      cout<<"}"<<L->r[pivotloc]<<"{";
    for(i=pivotloc+1;i<=high;i++)
     cout<<L->r[i]<<'\t';
    cout<<"}";
 }
  cout<<'\n'<<endl;
}


int partition(SeqList *L,int low,int high){
  int pivotkey;
  int temp1=low,temp2=high;
  L->r[0]=L->r[low];
  pivotkey=L->r[low];
  while (low<high){
     while (low<high && L->r[high]>=pivotkey)
     --high;
     L->r[low]=L->r[high];
    while(low<high && L->r[low]<=pivotkey)
    ++low;
    L->r[high]=L->r[low];
   }
    L->r[low]=L->r[0];
    output(L,temp1,temp2,low);
    return low;
    }


void quicksort(SeqList *L,int low,int high){
int pivotloc;
   if(low<high)
     pivotloc=partition(L,low,high);
    if(low<pivotloc-1)
     quicksort(L,low,pivotloc-1);
    if(high>pivotloc+1)
     quicksort(L,pivotloc+1,high);
}
};

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
第三十七课 实验八 排序实验
快速排序
2013年搜狐SOHU实习生技术笔试题
第25讲n
将数组中负数放在正数前面
9.9.1 快速排序算法(2)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服