冒泡排序
#include<iostream>
using namespace std;
void BubbleSort1(int *a,int n)
{
int i,j;
for(i=0;i<n;i++)
{
bool flag=false;
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
flag=true;
}
}
if(!flag)
return;
}
}
int main()
{
int k;
int a[10]={5,7,9,2,1,0,4,8,3,6};
BubbleSort1(a,10);
for(k=0;k<10;k++)
cout<<a[k]<<" ";
return 0;
}
选择排序
#include<iostream>
using namespace std;
void SelectSort(int *a,int n)
{
int i,j,min_index;
for(i=0;i<n-1;i++)
{
min_index=i;
for(j=i+1;j<n;j++)
if(a[j]<a[min_index])
min_index=j;
if(min_index!=i)
{
int temp=a[i];
a[i]=a[min_index];
a[min_index]=temp;
}
}
}
int main()
{
int k;
int a[10]={0,2,5,4,8,7,1,6,9,3};
SelectSort(a,10);
for(k=0;k<10;k++)
cout<<a[k]<<" ";
return 0;
}
插入排序
#include<iostream>
using namespace std;
void InsertSort(int *a,int n)
{
int i, j;
for (i = 1; i < n; i++)
for (j = i - 1; j >= 0 && a[j] > a[j + 1]; j--)
swap(a[j], a[j + 1]);
}
int main()
{
int k;
int a[10]={6,5,4,7,8,9,2,3,1,0};
InsertSort(a,10);
for(k=0;k<10;k++)
{
cout<<a[k]<<" ";
}
return 0;
}
归并排序
#include<iostream>
using namespace std;
void Merge(int *data,int low,int mid,int high);
void MergeSort(int *data,int low,int high)
{
if(low<high)
{
int mid=(low+high)/2;
MergeSort(data,low,mid);
MergeSort(data,mid+1,high);
Merge(data,low,mid,high);
}
}
void Merge(int *data,int low,int mid,int high) //high取9,让其取等号
{
int i=low;
int j=mid+1;
int k=0;
int *array=new int[high-low+1];
while(i<=mid&&j<=high)
{
if(data[i]<data[j])
array[k++]=data[i++];
else
array[k++]=data[j++];
}
while(i<=mid)
array[k++]=data[i++];
while(j<=high)
array[k++]=data[j++];
for(k=0,i=low;i<=high;k++,i++)
data[i]=array[k];
}
int main()
{
int t;
int a[10]={7,4,2,3,1,9,0,5,6,8};
MergeSort(a,0,9);
for(t=0;t<10;t++)
cout<<a[t]<<" ";
return 0;
}
希尔排序
#include<iostream>
using namespace std;
void shellSort(int *a,int n)
{
int i,j,gap;
for(gap=n/2;gap>0;gap/=2)
for(i=gap;i<n;i++)
for(j=i-gap;j>=0&&a[j]>a[j+gap];j-=gap)
swap(a[j],a[j+gap]);
}
void main()
{
int k;
int a[10]={0,2,1,4,5,9,7,8,6,3};
shellSort(a,10);
for(k=0;k<10;k++)
cout<<a[k]<<" ";
}
快速排序
#include<iostream>
using namespace std;
int partation(int *a,int low,int high)
{
int pivot=a[low];
while(low<high)
{
while(low<high&&a[high]>=pivot)
high--;
a[low]=a[high];
while(low<high&&a[low]<=pivot)
low++;
a[high]=a[low];
}
a[low]=pivot;
return low;
}
void quickSort(int *a,int low,int high)
{
if(low<high)
{
int index=partation(a,low,high);
quickSort(a,low,index-1);
quickSort(a,index+1,high);
}
}
int main()
{
int k;
int a[10]={9,3,0,5,4,7,8,2,1,6};
quickSort(a,0,9);
for(k=0;k<10;k++)
cout<<a[k]<<" ";
return 0;
}
归并排序
#include<iostream>
using namespace std;
void Merge(int *data,int low,int mid,int high);
void MergeSort(int *data,int low,int high)
{
if(low<high)
{
int mid=(low+high)/2;
MergeSort(data,low,mid);
MergeSort(data,mid+1,high);
Merge(data,low,mid,high);
}
}
void Merge(int *data,int low,int mid,int high) //high取9,让其取等号
{
int i=low;
int j=mid+1;
int k=0;
int *array=new int[high-low+1];
while(i<=mid&&j<=high)
{
if(data[i]<data[j])
array[k++]=data[i++];
else
array[k++]=data[j++];
}
while(i<=mid)
array[k++]=data[i++];
while(j<=high)
array[k++]=data[j++];
for(k=0,i=low;i<=high;k++,i++)
data[i]=array[k];
}
int main()
{
int t;
int a[10]={7,4,2,3,1,9,0,5,6,8};
MergeSort(a,0,9);
for(t=0;t<10;t++)
cout<<a[t]<<" ";
return 0;
}
堆排序
#include <iostream>
using namespace std;
void MinHeapFixdown(int a[],int i,int n);
void MakeMinHeap(int a[],int n)
{
for(int i=n/2-1;i>=0;i--)
MinHeapFixdown(a,i,n);
}
void MinHeapFixdown(int a[],int i,int n)
{
int temp;
temp=a[i];
for(int j=2*i+1;j<n;j=2*i+1)
{
if(j+1<n&&a[j+1]<a[j])
j++;
if(a[j]>=temp)
break;
a[i]=a[j];
i=j;
}
a[i]=temp;
}
void Minheapsort(int a[],int n)
{
for(int i=n-1;i>0;i--)
{
swap(a[i],a[0]);
MinHeapFixdown(a,0,i);
cout<<a[0]<<" "<<endl;
}
}
int main()
{
int a[]={9,0,4,3,5,6,7,1,2,8};
MakeMinHeap(a,10);
Minheapsort(a,10);
for(int i=0;i<10;i++)
cout<<a[i]<<" ";
return 0;
}