常见的排序算法有冒泡排序、简单选择排序、直接插入排序、shell排序、堆排序、归并排序和快速排序。具体代码如下:
#include <iostream>
#include <ctime>
using namespace std;
/********* 冒泡排序 *********/
void bubbleSort1(int* a, int Length){
for ( int i=0; i<Length ;i++)
{
for ( int j=0; j<Length- i-1; j++) //将最大值冒出
{
if ( a[j] >a[j+1] )
{
a[j]= a[j]^ a[j+1];
a[j+1]= a[j]^ a[j+1];
a[j]= a[j]^ a[j+1];
}
}
}
}
void bubbleSort2(int* a, int Length){
for ( int i=0; i<Length ;i++)
{
for ( int j=Length-1; j > i; j--) //将最小值冒出
{
if ( a[j-1] >a[j] )
{
a[j]= a[j]^ a[j-1];
a[j-1]= a[j]^ a[j-1];
a[j]= a[j]^ a[j-1];
}
}
}
}
/********* 简单选择排序 *********/
void selectSort( int *a, int Length){
int min;
for (int i=0; i< Length; i++)
{
min= i;
for (int j=i+1 ; j<Length ;j++)
{
if ( a[j]<a[min])
{
min=j;
}
}
if ( min!= i)
{
a[i]= a[i]^a[min]; //交换 当 i与min相等时, 交换不成了
a[min]= a[i]^a[min];
a[i]= a[i]^a[min];
}
}
}
/********* 直接插入排序 *********/
void insertSort( int *a, int Length){
int temp;
for ( int i=1 ;i<Length ;i++)
{
if ( a[i] <a[i-1])
{
temp=a[i];
int j;
for ( j=i-1 ;j>=0;j--)
{
if ( temp<a[j])
{
a[j+1] =a[j];
}
else break;
}
a[j+1]=temp;
}
}
}
/********* shell排序 *********/
//参考:http://zh.wikipedia.org/wiki/%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F
void shellSort( int *a, int Length){
for ( int gap= Length/2; gap>0 ;gap/=2){
for ( int i=gap; i<Length ;++i)
{
int temp= a[i]; //插入排序
int j= 0;
for ( j= i-gap ;j>=0&& a[j]> temp; j-=gap)
{
a[j+gap] =a[j];
}
a [j+gap] =temp;
}
}
}
/********* 堆排序 *********/
//从s节点开始调整, m为节点总数,构造大顶堆
void heapAdjust(int *a , int s, int m){
int temp ,j;
temp =a[s];
for ( j= 2*s; j<=m; j*=2)
{
if ( j<m && a[j]<a[j+1]) ++j;
if ( a[j] <=temp) break;
a[s]= a[j];
s=j;
}
a[s]= temp;
}
void heapSort(int *a, int Length){
int i;
for ( i= Length/2-1 ; i>=0; i--)
heapAdjust( a, i, Length) ;
for ( i= Length-1;i>0;i-- ){
a[0]=a[0]^a[i];
a[i]=a[0]^a[i];
a[0]=a[0]^a[i];
heapAdjust( a, 0, i-1);
}
}
/********* 归并排序 *********/
void merge( int *a , int left , int center, int Length){
int *temp=new int[ Length- left];
int i=left;
int j=center;
int k=0;
while ( i<center &&j< Length)
{
if ( a[i]<=a[j]) temp[k++]=a[i++];
else temp[ k++] = a[j++];
}
while ( i< center) temp[k++]=a[i++];
while ( j<Length) temp[k++] =a[j++];
for ( i=left, k=0; i< Length;i++,k++) a[i]=temp[k]; //将temp[]元素复制到a[]
delete[] temp;
}
void mSort (int *a, int left, int right){
if ( left < right)
{
int center= ( left + right)/2;
mSort( a, left, center); //左边有序
mSort( a, center+1, right); //右边有序
merge( a, left, center+1, right+1);
}
}
void mergeSort( int *a, int Length){
mSort( a, 0, Length-1);
}
//非递归形式
void mergeSort2(int* a, int Length){
int beforeLen; //合并前序列的长度
int afterLen = 1; //合并后序列的长度
for (beforeLen=1; afterLen<Length; beforeLen=afterLen)
{
afterLen = beforeLen << 1; //合并后序列的长度是合并前的两倍
int i = 0; //开始合并时第一个序列的起始位置下标,每次都是从0开始
for ( ; i+afterLen<Length; i+=afterLen)
merge(a, i, i+beforeLen, i+afterLen);
if (i+beforeLen < Length)
merge(a, i, i+beforeLen, Length);
}
}
/********* 快速排序 *********/
void swap( int *a, int left, int right){
int temp=a[left];
a[left]=a[right];
a[right]=temp;
//a[left]=a[left]^a[right];
//a[right]=a[left]^a[right];
//a[left]=a[left]^a[right];
}
void qSort1( int *a, int left, int right){
if ( left < right)
{
int low = left, high=right;
int pivot, pivotKey;
pivotKey=a[low];
while ( low< high){
while ( low<high && a[high]>=pivotKey) high--;
swap(a, low, high);
while ( low<high && a[low]<=pivotKey ) low++;
swap(a, low, high);
}
pivot=low;
qSort1(a, left, pivot-1);
qSort1(a, pivot+1,right);
}
}
int partition( int *a, int low ,int high){
int pivotKey;
pivotKey=a[low];
while ( low< high){
while ( low<high && a[high]>=pivotKey) high--;
swap(a, low, high);
while ( low<high && a[low]<=pivotKey ) low++;
swap(a, low, high);
}
return low;
}
void qSort2 ( int *a, int left, int right){
int pivot;
if ( left < right)
{
pivot=partition(a, left, right);
qSort2(a, left, pivot-1);
qSort2(a, pivot+1,right);
}
}
void qSort3(int a[], int left, int right)
{
if (left < right)
{
int i = left, j = right, pivotKey = a[left];
while (i < j)
{
while(i < j && a[j] >= pivotKey) // 从右向左找第一个小于x的数
j--;
a[i] = a[j];
while(i < j && a[i] <= pivotKey) // 从左向右找第一个大于等于x的数
i++;
a[j] = a[i];
}
a[i] = pivotKey;
qSort3(a, left, i - 1); // 递归调用
qSort3(a, i + 1, right);
}
}
void quickSort ( int *a, int Length){
qSort3(a, 0, Length-1);
}
void printArr(int *a ,int Length){
while ( Length)
{
cout<<*a<<" ";
a++;
--Length;
}
cout<<endl;
}
int main(){
clock_t ibegin , iend;
ibegin = clock();
int length=25;
int a[25]={12,7,45,6,4, 51,32,12,7,33, 62,23,19,37,28, 42,31,6,35,22 , 9, 8, 7, 6, 5} ;
printArr( a, length);
quickSort( a, length);
printArr( a, length);
int b[9]={50, 10, 90, 30, 70, 40, 80, 60, 20};
quickSort(b, 9);
printArr(b,9);
iend =clock();
cout<<iend-ibegin<<"毫秒"<<endl ;
}