冒泡排序
#include <iostream>
using namespace std;
void BubbleSort(int* pData,int Count)//冒泡排序
{
int iTemp,i,j;
for (i = 1;i < Count;i++)
{
for (j = Count - 1;j >= i;j--)
{
if (pData[j] < pData[j-1])
{
iTemp = pData[j-1];
pData[j-1]=pData[j];
pData[j]=iTemp;
}
}
}
}
void main()
{
int i,data[] = {10,9,8,7,6,5,4};
BubbleSort(data,7);
for (i = 0;i<7;i++)
{
cout<<data[i]<<" ";
}
cout<<"\n";
}
鸡尾酒排序
#include <iostream>
using namespace std;
void Cocktail(int* arr, int size)//鸡尾酒排序
{
int i,j,temp,tail = size - 1;
for (i = 0;i<tail;)
{
for (j = tail;j > i;j--)
{
if(arr[j] < arr[j-1])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
}
}
i++;
for (j = i;j < tail;j++)
{
if (arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
tail--;
}
}
void main()
{
int i,arr[] = {1,3,5,7,6,56,76,122,3};
Cocktail(arr,sizeof(arr)/sizeof(int));
for (i = 0;i<9;i++)
{
cout<<arr[i]<<" ";
}
cout<<"\n";
}
选择排序
#include <iostream>
using namespace std;
void SelectSort(int* pData,int Count)//选择排序
{
int i,j,iTemp,iPos;
for (i = 0;i<Count-1;i++)
{
iTemp =pData[i];
iPos = i;
for(j=i+1;j<Count;j++)
{
if (pData[j]<iTemp)
{
iTemp = pData[j];
iPos = j;
}
}
pData[iPos] = pData[i];
pData[i] = iTemp;
}
}
void main()
{
int i,data[] = {10,9,8,7,6,5,4};
SelectSort(data,7);
for (i=0;i<7;i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
}
插入排序
#include <iostream>
using namespace std;
void InsertSort(int* pData,int Count)//插入排序
{
int i,iPos,iTemp;
for (i=1;i<Count;i++)
{
iTemp = pData[i];
iPos = i-1;
while ((iPos>=0)&&(iTemp<pData[iPos]))
{
pData[iPos+1] =pData[iPos];
iPos--;
}
pData[iPos+1] = iTemp;
}
}
void main()
{
int i,data[] = {10,9,8,7,6,5,4};
InsertSort(data,7);
for (i=0;i<7;i++)
{
cout<<data[i]<<" ";
}
cout<<"\n";
}
归并排序
#include <iostream>
using namespace std;
void merge(int *pData,int p,int q,int r)//归并排序
{
int i,k;
int begin1,end1,begin2,end2;
int *temp=new int[r-p+1];
begin1=p;
end1=q;
begin2=q+1;
end2=r;
k=0;
while(begin1<=end1 && begin2<=end2)
{
if (pData[begin1]<pData[begin2])
{
temp[k++]=pData[begin1++];
}
else
{
temp[k++]=pData[begin2++];
}
}
while(begin1<=end1)
{
temp[k++]=pData[begin1++];
}
while (begin2<=end2)
{
temp[k++]=pData[begin2++];
}
for (i=0;i<(r-p+1);i++)
{
pData[p+i]=temp[i];
}
delete []temp;
}
void merge_sort(int *pData,int first,int last)
{
int mid=0;
if (first<last)
{
mid=(first+last)/2;
merge_sort(pData,first,mid);
merge_sort(pData,mid+1,last);
merge(pData,first,mid,last);
}
}
void main()
{
int i,array[] ={1,5,2,6,3,10,9,18};
merge_sort(array,0,7);
for (i=0;i<8;i++)
{
cout<<array[i]<<" ";
}
}
快速排序
int Partition(int *pData,int low,int high)//快速排序
{
int i = low;
int j = high + 1;
int temp = pData[low];
while(true){
while(pData[++i]<temp&&i<high);
while(pData[--j]>temp);//循环跳出后,下标为j的数组值一定小于等于temp
if(i>=j)
break;
if(i<j){
int temp = pData[i];
pData[i] = pData[j];
pData[j] = temp;
}
}
pData[low] = pData[j];
pData[j] = temp;
return j;
}
void QuickSort(int *pData,int low,int high)
{
if(low<high)
{
int tx = Partition(pData,low,high);
QuickSort(pData,low,tx - 1);
QuickSort(pData,tx+1,high);
}
}
int main()
{
int i,pData[17]={23,32,41,55,86,7,37,100,5,9,3,4,1,8,2,6,10};
cout<<endl;
QuickSort(pData,0,16);
for(i = 0;i<17;++i)
cout<<pData[i]<<' ';
return 0;
}