排序类
今天心血来潮,写了个排序的类,本来打算包含6种排序算法的,时间有限,只包含了快排和堆排,以后有时间再补充.这个类将具体的排序算法很好的隐藏起来了,用一个函数指针数组调用相应的排序功能,暴露的接口只有push(),pop(),length(),print(),以及选择排序的函数selectway()
类声明头文件
//sort.h
//by woodfish
#ifndef SORT_H
#define SORT_H
#include <iostream>
#include <cstring>
class List{
private:
enum {size=1000};
int a[size];
int n;
void (List::*ptr[10])(); //排序函数指针
void quick(int l,int r); //快速排序
void quicksort();
//维护堆性质
void shiftdown(int downn,int maxn);
void makeheap(); //建立一个大堆
void heapsort(); //堆排序
//void insertsort(); //插入排序
//void selectsort(); //选择排序
//void maopaosort(); //冒泡排序
//void mergesort; //归并排序
public:
List();
void push(int nn);
int length() const;
int pop();
//选择排序算法,默认为快排
void selectway(int nn=0) const;
void print() const;
};
#endif
//by woodfish
#ifndef SORT_H
#define SORT_H
#include <iostream>
#include <cstring>
class List{
private:
enum {size=1000};
int a[size];
int n;
void (List::*ptr[10])(); //排序函数指针
void quick(int l,int r); //快速排序
void quicksort();
//维护堆性质
void shiftdown(int downn,int maxn);
void makeheap(); //建立一个大堆
void heapsort(); //堆排序
//void insertsort(); //插入排序
//void selectsort(); //选择排序
//void maopaosort(); //冒泡排序
//void mergesort; //归并排序
public:
List();
void push(int nn);
int length() const;
int pop();
//选择排序算法,默认为快排
void selectway(int nn=0) const;
void print() const;
};
#endif
类实现cpp文件
1/* sort.h
2by woodfish
3*/
4
5#include "sort.h"
6using namespace std;
7
8List::List() {
9 n=1;
10 memset(a,0,sizeof(a));
11 ptr[0]=&List::quicksort;
12 ptr[1]=&List::heapsort;
13 //ptr[2]=&List::selectsort;
14 //ptr[3]=&List::insertsort;
15 //ptr[4]=&List::maopaosort;
16 //ptr[5]=&List::mergersort;
17}
18void List::push(int nn){
19 a[n++]=nn;
20}
21int List::length() const{
22 return n-1;
23}
24int List::pop(){
25 if(n==1) return 0;
26 return a[--n];
27}
28void List::quick(int l,int r){
29 if(l<r) {
30 int i,j;
31 i=l;j=r;
32 int temp=a[i];
33 while(i<j) {
34 while(i<j&&j>=l&&a[j]>=temp) j--;
35 if(i<j) a[i++]=a[j];
36 while(i<j&&i<=r&&a[i]<=temp) i++;
37 if(i<j) a[j--]=a[i];
38 }
39 a[i]=temp;
40 quick(l,i-1);
41 quick(i+1,r);
42 }
43}
44void List::quicksort(){
45 quick(1,n-1);
46}
47void List::selectway(int nn) const{
48 (this->*ptr[nn])();
49}
50void List::print() const{
51 for(int i=1;i<n;i++)
52 cout<<a[i]<<" ";
53 cout<<endl;
54}
55void List::shiftdown(int downn,int maxn){
56 int j,rc;
57 rc=a[downn];
58 for(j=2*downn;j<=maxn;j+=2){
59 if(j<maxn&&a[j]<a[j+1]) ++j;
60 if(rc>=a[j]) break;
61 a[downn]=a[j];
62 downn=j;
63 }
64 a[downn]=rc;
65}
66void List::heapsort(){
67 makeheap();
68 for(int i=n-1;i>=1;i--) {
69 int temp=a[1];
70 a[1]=a[i];
71 a[i]=temp;
72 shiftdown(1,i-1);
73 }
74}
75void List::makeheap(){
76 for(int i=(n-1)/2;i>=1;i--)
77 shiftdown(i,n-1);
78}
2by woodfish
3*/
4
5#include "sort.h"
6using namespace std;
7
8List::List() {
9 n=1;
10 memset(a,0,sizeof(a));
11 ptr[0]=&List::quicksort;
12 ptr[1]=&List::heapsort;
13 //ptr[2]=&List::selectsort;
14 //ptr[3]=&List::insertsort;
15 //ptr[4]=&List::maopaosort;
16 //ptr[5]=&List::mergersort;
17}
18void List::push(int nn){
19 a[n++]=nn;
20}
21int List::length() const{
22 return n-1;
23}
24int List::pop(){
25 if(n==1) return 0;
26 return a[--n];
27}
28void List::quick(int l,int r){
29 if(l<r) {
30 int i,j;
31 i=l;j=r;
32 int temp=a[i];
33 while(i<j) {
34 while(i<j&&j>=l&&a[j]>=temp) j--;
35 if(i<j) a[i++]=a[j];
36 while(i<j&&i<=r&&a[i]<=temp) i++;
37 if(i<j) a[j--]=a[i];
38 }
39 a[i]=temp;
40 quick(l,i-1);
41 quick(i+1,r);
42 }
43}
44void List::quicksort(){
45 quick(1,n-1);
46}
47void List::selectway(int nn) const{
48 (this->*ptr[nn])();
49}
50void List::print() const{
51 for(int i=1;i<n;i++)
52 cout<<a[i]<<" ";
53 cout<<endl;
54}
55void List::shiftdown(int downn,int maxn){
56 int j,rc;
57 rc=a[downn];
58 for(j=2*downn;j<=maxn;j+=2){
59 if(j<maxn&&a[j]<a[j+1]) ++j;
60 if(rc>=a[j]) break;
61 a[downn]=a[j];
62 downn=j;
63 }
64 a[downn]=rc;
65}
66void List::heapsort(){
67 makeheap();
68 for(int i=n-1;i>=1;i--) {
69 int temp=a[1];
70 a[1]=a[i];
71 a[i]=temp;
72 shiftdown(1,i-1);
73 }
74}
75void List::makeheap(){
76 for(int i=(n-1)/2;i>=1;i--)
77 shiftdown(i,n-1);
78}
测试该排序类
/*test.cpp
测试排序类
by woodfish
*/
#include "sort.h"
using namespace std;
int main(){
List l;
int n;
cin>>n;
for(int i=0;i<n;i++) {
int temp;
cin>>temp;
l.push(temp);
}
List ori=l;
cout<<"快速排序:";
l.selectway(1);
l.print();
cout<<endl;
cout<<"堆排序:";
ori.selectway(1);
l.print();
cin>>n;
return 0;
}
测试排序类
by woodfish
*/
#include "sort.h"
using namespace std;
int main(){
List l;
int n;
cin>>n;
for(int i=0;i<n;i++) {
int temp;
cin>>temp;
l.push(temp);
}
List ori=l;
cout<<"快速排序:";
l.selectway(1);
l.print();
cout<<endl;
cout<<"堆排序:";
ori.selectway(1);
l.print();
cin>>n;
return 0;
}