包括冒泡排序、选择排序、插入排序、快速排序、归并排序,堆排序、二分查找。
#include<iostream>
using namespace std;
#define max 100;
void copyArray(int * src,int * dest,int size);
void myswap(int * myarray,int idx0,int idx1);
void show(int * myarray,int size);
void popSort(int * myarray,int size);
void selectSort(int * myarray,int size);
void insertSort(int * myarray,int size);
void qSort(int * myarray,int low,int up);
void quickSort(int * myarray,int size);
void mSort(int * src,int * dest,int low,int size);
void mergeSort(int * myarray,int size);
void heapSort(int *myarray,int size);
void heapInsert(int *a,int value,int size);
int heapDelete(int *a,int size);
bool BinaryFind(int *a,int value,int size);
void copyArray(int *a,int *b,int num)
{
for (int i=0;i<num;i++)
b[i]=a[i];
}
/*
method description:
space O(1),time O(n^2),stable
pop the ith smallest data to the ith place in the ith time,i=0,1,...,size-2
the ith time : if(a[j]>a[j+1]) swap (a,j,j+1),j=size-2,size-1,...,i
*/
void popSort(int *a,int size)
{
if (size<2)
return;
int i = 0;
int j = 0;
for(i = 0;i<size-1;i++)
for(j = size-2;j>i-1;j--)
if(a[j]>a[j+1])
myswap(a,j,j+1);
}
/*
method description:
space O(1),time O(n^2),stable
select the ith smallest data and swap with the ith data in the ith time,i=0,1,...,size-2
minid = i,j=i+1,..,size-1,if(a[j]<a[minid]) minid = j; swap (a,i,minid);
*/
void selectSort(int *a,int size){
int i = 0;
int j = 0;
int minid = 0;
for(i=0;i<size-1;i++)
{
minid = i;
for(j=i+1;j<size;j++)
if(a[j]<a[minid])
minid = j;
myswap(a,i,minid);
}
}
/*
method description:
space O(1),time O(n^2),stable
for the ith loop:0~i is sorted,i=0,1,..,size-2,size>0
while(a[j+1]<a[j]) swap(a,j+1,j); j=i,..,0
*/
void insertSort(int *a,int size)
{
int i = 0;
int j =0;
if(size==1)
return;
for(i=0;i<size-1;i++)
for(j=i;j!=-1 && a[j+1]<a[j];j--)
myswap(a,j+1,j);
}
void quickSort(int *a,int size)
{
qSort(a,0,size-1);
}
/*
method description:
space O(1),time O(nlogn),not stable
input : int *a ,int l,int u; if (l>=u) return;
int i,j;
i=l,j=u+1;
while(true) while(a[++i]<a[l] && i<u ); while(a[--j]>a[l]); if (i<j) swap(a,i,j) else break; i:l+1~u;j:u~l+1
swap(a,l,j);
qSort(a,l,j-1);
qSort(a,j+1,u);
*/
void qSort(int *a,int l,int u)
{
if(u<=l)
return;
int i = l;
int j = u+1;
while(true)
{
while(a[++i]<a[l] && i<u);
while(a[--j]>a[l]);
if(i<j)
myswap(a,i,j);
else
break;
}
myswap(a,l,j);
qSort(a,l,j-1);
qSort(a,j+1,u);
}
void mergeSort(int * a,int size){
int *b = new int[size];
mSort(a,b,0,size-1);
delete []b;
}
/*
method description:
space O(n);time (nlogn);stable
input (int *src,int *dest,int l,int u); u<=l,return
int i=l,m=(l+u)/2,j=m+1,k=l;
mSort(a,b,l,m);
mSort(a,b,m+1,u);
i:l~m;j:m+1~u;
while(i<m+1 && j<u+1) {if(a[i]<=a[j])b[k++]=a[i++] else b[k++]=a[j++];}
while(i<m+1) b[k++]=a[i++];
while(j<u+1) b[k++]=a[j++];
copyArray(b+l,a+l,u-l+1);
*/
void mSort(int *a,int *b,int l,int u){
if(u<=l)
return;
int i = l;
int m = (l+u)/2;
int j = m+1;
int k =l;
mSort(a,b,l,m);
mSort(a,b,m+1,u);
while(i<m+1 && j<u+1)
{
if(a[i]<=a[j])
b[k++]=a[i++];
else
b[k++]=a[j++];
}
while(i<m+1) b[k++]=a[i++];
while(j<u+1) b[k++]=a[j++];
copyArray(b+l,a+l,u-l+1);
}
/*
method description:
space O(n), time:O(nlogn), stable
insert and build the heap;
delete the heap;
*/
void heapSort(int *a,int size){
int *b = new int[size+1];
for(int i = 0;i < size;i++)
heapInsert(b,a[i],i);
//show(b+1,size);
for(int i = 0;i<size;i++)
a[i]=heapDelete(b,size-i);
delete b;
}
void heapInsert(int *a,int value,int size)
{
a[size+1]=value;
int p = size+1;
while(p!=1 && a[p]<a[p/2])
{
myswap(a,p,p/2);
p = p/2;
}
}
int heapDelete(int *a,int size)
{
int tmp = a[1];
a[1]=a[size];
size--;
int i = 1;
int child = 1;
while(2*i<size+1)
{
child = 2*i;
if(child+1<size+1 && a[child]>a[child+1])
child++;
if(a[child]<a[i])
myswap(a,i,child);
else
break;
i=child;
}
return tmp;
}
/*
method time :O(logn)
l=0,u=size-1;
m = (l+u)/2;
if(l==u )
return a[l]==value;
while(l<u){m=(l+u)/2;if(a[m]>value) l=m+1;else if (a[m]<value) u=m-1;else return true;}
return false;
*/
bool BinaryFind(int *a, int value,int size)
{
int l=0;
int u=size-1;
int m=(l+u)/2;
if(l==u)
return a[l]==value;
while(l<u)
{
m=(l+u)/2;
if(a[m]<value)
l=m+1;
else if(a[m]>value)
u=m-1;
else
return true;
}
return false;
}
void myswap(int *a,int i,int j)
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
void show(int *a,int num)
{
for(int i=0;i<num;i++)
cout<<a[i]<<'\t';
cout<<endl;
}
int main(){
int a[10];
for (int i=0;i<10;i++)
a[i]=rand()%max;
cout<<"the origin data : "<<endl;
show(a,10);
int b[10];
cout<<"\npop Sort result"<<endl;
copyArray(a,b,10);
popSort(b,10);
show(b,10);
cout<<"\nselect Sort result"<<endl;
copyArray(a,b,10);
selectSort(b,10);
show(b,10);
cout<<"\ninsert Sort result"<<endl;
copyArray(a,b,10);
insertSort(b,10);
show(b,10);
cout<<"\nquick Sort result"<<endl;
copyArray(a,b,10);
quickSort(b,10);
show(b,10);
cout<<"\nmerge Sort result"<<endl;
copyArray(a,b,10);
mergeSort(b,10);
show(b,10);
cout<<"\nheap Sort result"<<endl;
copyArray(a,b,10);
heapSort(b,10);
show(b,10);
cout<<"\npls input the number to be found"<<endl;
int num;
cin>>num;
if(BinaryFind(b,num,10))
cout<<"data "<<num<<" is found";
else
cout<<"data "<<num<<" is not found";
system("pause");
return 0;
}