å¯ä»¥ç¨å泡æåºæ³ï¼æ大å æåºï¼ç´æ¥éæ©æåºæ³ï¼ç´æ¥æå
¥æåºæ³ççåï¼ï¼ï¼ï¼#ifndef DATALIST_H
#define DATALIST_H
#include<iostream>
const int DefaultSize=100;
template<class T>
class Element { //æ°æ®è¡¨å
ç´ å®ä¹
public:
T key; //æåºç
T otherdata; //å
¶ä»æ°æ®æå
Element<T>& operator=(Element<T>& x) //å
ç´ xçå¼èµç»this
{key=x.key;otherdata=x.otherdata;return this;}
bool operator==(Element<T>& x) {return key==x.key;} //å¤*thisä¸xç¸ç
bool operator<=(Element<T>& x) {return key<=x.key;} //å¤*thiså°äºæçäºx
bool operator>(Element<T>& x) {return key>x.key;} //å¤*this大äºx
bool operator<(Element<T>& x) {return key<x.key;} //å¤*thiså°äºx
};template<class T>
class dataList { //æ°æ®è¡¨ç±»å®ä¹
public:
dataList(int maxSz=DefaultSize):maxSize(maxSz),currentSize(0)
{Vector=new Element<T>[maxSize];} //æé å½æ°
void Swap(Element<T>& x,Element<T>& y) //交æ¢x,y
{Element<T>temp=x;x=y;y=temp;}
int Length() {return currentSize;} //å表é¿åº¦
Element<T>& operator[](int i) {return Vector[i];} //å第i个å
ç´
int Partition(const int low,const int high); //å¿«éæåºåå
private:
Element<T>* Vector; //åå¨æåºå
ç´ çåé
int maxSize; //åéä¸æ大å
ç´ ä¸ªæ°
int currentSize; //å½åå
ç´ ä¸ªæ°
};
#endif
template<class T>
void merge(dataList<T>& L1,dataList<T>& L2,
const int left,const int mid,const int right) {
//L1.Vector[left:mid]ä¸L1.Vector[mid+1:right]æ¯ä¸¤ä¸ªæåºè¡¨å½å¹¶
//æä¸ä¸ªæåºè¡¨L1.Vector[left:right]ã
for(int k=left;k<=right;k++)
L2[k]=L1[k];
int s1=left,s2=mid+1,t=left; //s1,s2æ¯æ£æµæéï¼tæ¯åæ¾æé
while(s1<=mid&& s2<=right) //两个表é½æªæ£æµå®ï¼ä¸¤ä¸¤æ¯è¾
if(L2[s1]<=L2[s2]) L1[t++]=L2[s1++];
else L1[t++]=L2[s2++];
while(s1<=mid) L1[t++]=L2[s1++]; //è¥ç¬¬ä¸ä¸ªè¡¨æªæ£æµå®ï¼å¤å¶
while(s2<=right) L1[t++]=L2[s2++]; //è¥ç¬¬äºä¸ªè¡¨æªæ£æµå®ï¼å¤å¶
};
template<class T>
void mergeSort(dataList<T>& L,dataList<T>& L2,int left,int right) {
if(left>=right) return;
int id=(left+right)/2; //ä»ä¸é´åå为两个ååºå
mergeSort(L,L2,left,mid); //对左侧ååºåè¿è¡éå½æåº
mergeSort(L,L2,mid+1,right); //对å³ä¾§ååºåè¿è¡éå½æåº
merge(L,L2,left,mid,right); //å并
};void bubblesort(int a[],int n)
{
int p=0,q=0;
for(int j=0;j<n-1;j++)
{
for(int i=0;i<n-1-j;i++)
{
if(a[i]>a[i+1])
{
int temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
p++;
}
q++;
}
}
cout<<"å泡æåºåç»æï¼";
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"æ¯è¾æ¬¡æ°ï¼"<<q<<endl;
cout<<"交æ¢æ¬¡æ°ï¼"<<p<<endl;
}
void binaryinsertsort(int a[], int n)
{
int i, j;
int p=0,q=0;
int low, high, mid;
int temp;
for (i=1; i<n; i++)
{
temp = a[i];
low = 0;
high = i - 1;
while (low <= high) //å¨a[lowãããhigh]ä¸æåæ¥æ¾æåºæå
¥çä½ç½®
{
mid = (low + high) / 2;
if (a[mid] > temp)
high = mid - 1;
else
low = mid + 1;
q++;
}
for (j=i-1; j>high; j--)
{//å
ç´ å移
a[j+1] = a[j];
p++;
}
a[high+1] = temp; //æå
¥
}
cout<<"æåæåºåç»æï¼";
for( i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"æ¯è¾æ¬¡æ°ï¼"<<q<<endl;
cout<<"交æ¢æ¬¡æ°ï¼"<<p<<endl;
}
void insertsort(int R[],int n)
{
int p=0,q=0;
for(int t=1;t<n;t++)
{
if(R[t]<R[t-1])
{
int temp=R[t];
int j=t-1;
do
{
R[j+1]=R[j];
j--;
}while(temp<R[j]&&j>=0);
R[j+1]=temp;
p++;
}
q++;
} cout<<"ç´æ¥æå
¥æåºåç»æï¼";
for(int i=0;i<n;i++)
{
cout<<R[i]<<" ";
}
cout<<endl;
cout<<"æ¯è¾æ¬¡æ°ï¼"<<q<<endl;
cout<<"交æ¢æ¬¡æ°ï¼"<<p<<endl;
}
void selectsort(int a[],int n)
{
int p=0,q=0;
for(int i=0;i<n;i++)
{
int k=i;
for(int j=i+1;j<n;j++)
{
if(a[j]<a[k])
{
k=j;
}q++;
}
if(k!=i)
{
swap(a[i],a[k]);
p++;
}
}
cout<<"ç´æ¥éæ©æåºåç»æï¼";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"æ¯è¾æ¬¡æ°ï¼"<<q<<endl;
cout<<"交æ¢æ¬¡æ°ï¼"<<p<<endl;
}void shellsort(int a[], int len)
{
int p=0,q=0;
for (int increment=len/2; increment>0; increment/=2)
{
for (int i=increment; i<len; i++)
{
int temp = a[i];
int j = i;
for (; j>=increment; j-=increment)//å
ç´ å移
{
if (temp < a[j-increment])
a[j] = a[j-increment];
else
break;
q++;
}
a[j] = temp; //æå
¥
p++;
}//for
}//for
cout<<"å¸å°æåºåç»æï¼";
for(int i=0;i<len;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"æ¯è¾æ¬¡æ°ï¼"<<q<<endl;
cout<<"交æ¢æ¬¡æ°ï¼"<<p<<endl;
}
#include<iostream>
using namespace std;
#include"dataList.h"void main()
{
int n,k;
cout<<"è¾å
¥æ°ç»ä¸ªæ°:";
cin>>n;
int a[100];
cout<<"è¾å
¥æ°ç»å
ç´ ï¼";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"请éæ©éè¦ä½¿ç¨çæåºæ¹æ³ï¼"<<endl;
cout<<"1.å泡æåºæ³ 2.æåæåºæ³ 3.å¸å°æåºæ³ 4.ç´æ¥æå
¥æåºæ³ 5.ç´æ¥éæ©æåºæ³"<<endl;
cout<<"请è¾å
¥åºå·ï¼";
cin>>k;
switch(k)
{
case 1:
bubblesort(a,n);
cout<<endl;
break;
case 2:
binaryinsertsort(a,n);
cout<<endl;
break;
case 3:
shellsort(a,n);
cout<<endl;
break;
case 4:
insertsort(a,n);
cout<<endl;
break;
case 5:
selectsort(a,n);
break;
}
}
温馨提示:内容为网友见解,仅供参考