其实问题很简单:随机生成100个数,编写以下4种排序算法对其从小到大排序。
- 冒泡排序
- 快速排序
- 希尔排序
- 堆排序
- 归并排序
- 选择排序
- 冒泡排序
\
算法理解:(具体参考:www.cnblogs.com/kkun/archiv…)
代码实现:
[cpp] view plain copy
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- //Swap-交换
- template<class T>
- void Swap(T &a,T &b)
- {
- T tmp=a;
- a=b;
- b=tmp;
- }
- //BubbleSorting Small->Large
- template<class T>
- void BubbleSorting(T A[])
- {
- for(int i=99;i>0;i--)//Times-外循环,n-1次
- {
- int flag=0;//引入flag,稍作性能提升
- for(int j=0;j<99;j++)
- {
- if(A[j]>A[j+1])
- {
- Swap(A[j],A[j+1]);
- flag=1;
- }
- }
- if(!flag)break;
- }
- }
- int main()
- {
- clock_t start_time = clock();
- srand(unsigned(time(NULL)));
- const int min=-100;
- const int max= 100;
- int A[200];
- for(int i=0;i<100;i++){A[i]=rand()%(max-min+1)+min;}//Generate 100 random figures
- cout<<"The random numbers are:"<<endl;
- for(int i=0;i<100;i++){cout<<A[i]<<"\t";}//Output the disordered numbers
- cout<<endl;
- cout<<"The ordered numbers are:"<<endl;
- BubbleSorting(A);//Bubble Sorting
- for(int i=0;i<100;i++){cout<<A[i]<<"\t";}//Output the disordered numbers
- cout<<endl;
- cout << "The elapsed time is:" << double(clock() - start_time) << 's' << endl;
- return 0;
- }
\
- 快速排序
\
算法理解:(具体参考:www.cnblogs.com/MOBIN/p/468…)
代码实现:
[cpp] view plain copy
[cpp] view plain copy
- #include<iostream>
- #include<cstdlib>
- #include<ctime>
- using namespace std;
- //交换
- template<class T>
- void Swap(T &a,T &b)
- {
- T tmp=a;
- a=b;
- b=tmp;
- }
- //划分
- template<class T>
- int Partion(T elem[],int low,int high)
- {
- while(low<high)//这里无需“=”即可
- {
- while(low<high&&elem[low]<=elem[high])high--;
- Swap(elem[low],elem[high]);
- while(low<high&&elem[low]<=elem[high])low++;
- Swap(elem[low],elem[high]);
- }
- return low;
- }
- //递归
- template<class T>
- void Help(T elem[],int low,int high)
- {
- if(low<high)
- {
- int KeyPoint=Partion(elem,low,high);
- Help(elem,low,KeyPoint-1);
- Help(elem,KeyPoint+1,high);
- }
- }
- template<class T>
- void QuickSort(T elem[],int n)
- {
- Help(elem,0,n-1);
- }
- int main()
- {
- clock_t start_time=clock();
- srand(unsigned(time(NULL)));
- const int min=-100;
- const int max= 100;
- int A[200];
- cout<<"The random numbers are:"<<endl;
- for(int i=0;i<100;i++)A[i]=rand()%(max-min+1)+min;
- for(int i=0;i<100;i++)cout<<A[i]<<"\t";
- cout<<endl;
- cout<<"The ordered numbers are:"<<endl;
- QuickSort(A,100);
- for(int i=0;i<100;i++)cout<<A[i]<<"\t";
- cout<<endl;
- cout<<"The elapsed time is "<< double(clock()-start_time) <<'s'<<endl;
- }
\
- 希尔排序
\
算法理解: (具体参考:www.cnblogs.com/skywang1234…)
代码实现:
[cpp] view plain copy
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- template<class T>
- void ShellInsert(T elem[],int n,int incr)<span style="font-family:Arial, Helvetica, sans-serif;">//incr代表间距</span>
- {
- for(int i=incr;i<n;i++)
- {
- T xp=elem[i];
- int j;
- for(j=i-incr;j>=0&&elem[j]<xp;j-=incr)//注意这里是xp,若写elem[i],数值容易被覆盖,导致错误排序。
- {
- elem[j+incr]=elem[j];
- }
- elem[j+incr]=xp;
- }
- }
- template<class T>
- void ShellSorting(T elem[],int n,int inc[],int t)
- {
- for(int k=0;k<t;k++)
- {
- ShellInsert(elem,n,inc[k]);
- }
- }
- int main()
- {
- clock_t start_time=clock();
- srand(unsigned(time(NULL)));
- const int min=-300;
- const int max= 300;
- int A[100];
- int inc[10];
- int num=100;
- for(int i=0;i<6;i++)
- {
- num=num/2;
- inc[i]=num;
- }
- cout<<"The random numbers are:"<<endl;
- for(int i=0;i<100;i++)A[i]=rand()%(max-min+1)+min;
- for(int i=0;i<100;i++)cout<<A[i]<<"\t";
- cout<<endl;
- cout<<"The ordered numbers are:"<<endl;
- ShellSorting(A,100,inc,6);
- for(int i=0;i<100;i++)cout<<A[i]<<"\t";
- cout<<endl;
- cout<<"The elapsed time is: "<<double(clock()-start_time)<<'s'<<endl;
- return 0;
- }
\
- 堆排序
\
算法理解: (具体参考:www.cnblogs.com/jingmoxukon…)
代码实现:
[cpp] view plain copy
-
#include <iostream>
-
#include <cstdlib>
-
#include <ctime>
-
using namespace std;
-
-
//Swap
-
void Swap(int &a,int &b){
-
int tmp=a;
-
a=b;
-
b=tmp;
-
}
-
//Adjust the big node
-
void AdjustHelp(int A[],int low,int high){
//调整堆
-
for(int p=low,i=low*2+1;i<=high;i=i*2+1){
-
if(i<high&&A[i]<A[i+1])i++;
-
if(A[p]>A[i])break;
-
Swap(A[p],A[i]);
-
p=i;
-
}
-
}
-
//HeapSort Algorithm
-
void HeapSort(int A[],int n){
-
for(int i=(n-2)/2;i>=0;i--){
//写入堆
-
AdjustHelp(A,i,n-1);
-
}
-
for(int i=n-1;i>0;i--){
//排序
-
Swap(A[i],A[0]);
-
AdjustHelp(A,0,i-1);
-
}
-
}
-
int main()
-
{
-
clock_t start_time = clock();
-
int A[100];
-
const int min=-99;
-
const int max=99;
-
srand(unsigned(time(NULL)));
-
cout << "The previous numbers are:" << endl;
-
for(int i=0;i<100;i++)A[i]=rand()%(max-min+1)+min;
-
for(int i=0;i<100;i++)cout<<A[i]<<"\t";
-
cout << endl;
-
cout << "The current numbers are:"<<endl;
-
HeapSort(A,100);
-
for(int i=0;i<100;i++)cout<<A[i]<<"\t";
-
cout << endl;
-
cout << "The elapsed time is: " << double(clock()-start_time)<<"s"<<endl;
-
return 0;
}
- 归并排序
算法理解: (具体参考:blog.csdn.net/yuehailin/a…)
代码实现:
[cpp] view plain copy
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- void Merge(int elem[],int tmp[],int low,int mid,int high){
- int i=low,j=mid+1;
- int m=mid,n=high;
- int k=low;
- while(i<=m&&j<=n){
- if(elem[i]<elem[j]){
- tmp[k++]=elem[i++];
- }
- else
- tmp[k++]=elem[j++];
- }
- while(i<=m)tmp[k++]=elem[i++];
- while(j<=n)tmp[k++]=elem[j++];
- for(i=low;i<=high;i++){
- elem[i]=tmp[i];
- }
- }
- //
- void Help(int elem[],int tmp[],int low,int high){
- if(low<high){
- int mid=(low+high)/2;
- Help(elem, tmp, low, mid);
- Help(elem,tmp,mid+1,high);
- Merge(elem, tmp, low, mid, high);
- }
- }
- //
- void MergeSort(int elem[],int n){
- int *p=new int[n];
- Help(elem,p,0,n-1);
- delete[] p;
- }
- int main() {
- // insert code here...
- clock_t start_time=clock();
- srand(unsigned(time(NULL)));
- int A[100];
- const int min=1;
- const int max=100;
- cout<<"The previous numbers are:"<<endl;
- for(int i=0;i<100;i++)A[i]=rand()%(max-min+1)+min;
- for(int i=0;i<100;i++)cout<<A[i]<<"\t";
- cout<<endl;
- cout<<"The current numbers are:"<<endl;
- MergeSort(A,100);
- for(int i=0;i<100;i++)cout<<A[i]<<"\t";
- cout<<endl;
- cout<<"The elapsed time is: "<<double(clock()-start_time)<<"s"<<endl;
- cout<<endl;
- return 0;
- }
- 选择排序(C)
\
\
代码实现:
[cpp] view plain copy
-
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
time_t ts;
srand((unsigned int)time(&ts));
int a[10];
printf("随机数为:");
for(int i=0; i<10; i++)
{
a[i]=rand()%100;
printf("%d ",a[i]);
}
for(int i=0; i<9; i++) //0-8 9个数 最后一个数不用比较
{
int max=i;//标识最大数下标 假定a[0]最大
for(int j=i+1; j<10; j++) //选择法排序
{
if(a[max]<a[j])
{
max=j;
}
}
if(max!=i)
{
int t=a[max];
a[max]=a[i];
a[i]=t;
}
}
printf("\n从大到小排序为:");
for(int i=0; i<10; i++)
{
printf("%d ",a[i]);
}
return 0;
}