#include<iostream>
#include <string>
#include <algorithm>
#include <time.h>
#include<windows.h>
using namespace std;
template < typename T >
void funoutput(T a){
cout << a << " ";
}
//防止数组越界a[]={5,4,9,8,7,6,0,1,3,2};//冒泡排序
void bublle_sort(int *a , int length){
if (a == NULL || length == 0){
return;
}
for (int i = length; i > 0; --i){
for(int j = 0 ;j < i;++j)
if (a[j] > a[j+1]){
a[j+1] ^= a[j];
a[j] ^= a[j+1];
a[j+1] ^= a[j];
}
//for_each(a,a+length+1,funoutput<int>);
//cout << endl;
}
}
// a[]={5,4,9,8,7,6,0,1,3,2};//选择排序
void select_sort(int *a, int length){
if (a == NULL || length == 0){
return;
}
//int i= 0;
int temp =0;
int flag = 0;
int j = 0;
int i = 0;
for(;i < length-1; ++i){
temp = a[i];
flag = i;
for (j = i+1; j< length; ++j){
if (a[j] < temp){
temp = a[j];
flag = j;
}
}
if (flag != i){
a[flag] = a[i];
a[i] = temp;
}
//for_each(a,a+length,funoutput<int>);
//cout << endl;
}
}
// a[]={5,4,9,8,7,6,0,1,3,2};//快速排序
void quick_sort(int *a,int low , int high){
if (a == NULL){
return;
}
if (low >= high){
return;
}
int index = a[low];
int i = low;
int j = high;
while (i < j )
{
while (i < j && index <= a[j]){
j--;
}
if (i < j){
a[i++] = a[j];
}
while(i < j && a[i] < index){
++i;
}
if (i < j){
a[j--] = a[i];
}
a[i] = index;
quick_sort(a, low, i-1);
quick_sort(a ,i+1,high);
}
}
// a[]={5,4,9,8,7,6,0,1,3,2};//插入排序
void insert_sort(int *a, int length){
if(a == NULL || length == 0)
return ;
int i = 0;
int j = 0;
int temp = 0;
for(i = 1; i < length; ++i){
temp = a[i];
for (j = i-1; j >= 0; --j){
if (a[j] > temp){
a[j+1] = a[j];
}
else
break;
}
a[j+1] = temp;
//for_each(a,a+length,funoutput<int>);
//cout << endl;
}
}
void shell_sort(int *a, int length){
if (a == NULL && length == 0 ){
return;
}
int temp = 0;
int i = 0;
int j = 0;
int h = 0 ;
for( h = length/2; h > 0; h /= 2){
for (i = h; i < length; ++i){
temp = a[i];
for (j = i-h; j >= 0; j -= h)
{
if (a[j] > temp){
a[j+h] = a[j];
}
else
break;
}
a[j+h] = temp;
}
}
}
//归并排序
// a[]={5,4,9,8,7,6,0,1,3,2};
void merge_sort(int *a, int low, int hight){
if (a == NULL){
return;
}
int m = (low + hight)/2; //( low ^ hight) >> 1+ ( low & hight);
int p = low;
int q = m+1;
int k =0;
int *c = new int[hight+1];
while (p < m+1 && q <= hight){
if(a[p] <= a[q])
c[k++]=a[p++];
else
c[k++]=a[q++];
}
while (p < m+1){
c[k++]=a[p++];
}
while (q <= hight){
c[k++] = a[q++];
}
for (int i =low; i <= hight ; i++){
a[i] = c[i-low];
}
delete[] c;
//for_each(a,a+10,funoutput<int>);
//cout << endl;
}
void merge1(int *a, int low, int hight){
if (low < hight){
int mid = (low + hight)/2; //(low & hight) + (low ^ hight) >> 1;
merge1(a, low, mid);
merge1(a, mid+1, hight);
merge_sort(a, low, hight);
}
}
//堆排序5,4,9,8,7,6,0,1,3,2,11,212,1212
void Maxheap(int *a, int index, int length){
if (a == NULL){
return;
}
int temp = 0;
int child = 0;
for(temp = a[index]; 2 * index + 1 <= length;index = child ){
child = 2* index +1;
if (child < length && a[child] < a[child + 1]){
++child;
}
if (a[child] > temp){
a[index] = a[child];
}
else
break;
}
a[index] = temp;
//for_each(a,a+13,funoutput<int>);
//cout << endl;
}
void swap1(int &a, int &b){
int temp = a;
a = b ;
b = temp;
}
void MaxHeap_sort(int *a, int length){
int i = 0;
for (i = length/2-1 ; i >= 0; --i){
Maxheap(a, i,length-1);
}
for(i = length - 1; i >=0 ; --i){
swap1(a[0],a[i]);
Maxheap(a, 0,i-1);
}
}
int main(){
int a[] ={ 5,4,9,8,7,6,0,1,3,2,11,212,1212};
int a1[]={ 5,4,9,8,7,6,0,1,3,2,11,212,1212};
int a2[]={ 5,4,9,8,7,6,0,1,3,2,11,212,1212};
int a3[]={ 5,4,9,8,7,6,0,1,3,2,11,212,1212};
int a4[]={ 5,4,9,8,7,6,0,1,3,2,11,212,1212};
int a5[]={ 5,4,9,8,7,6,0,1,3,2,11,212,1212};
int a6[]={ 5,4,9,8,7,6,0,1,3,2,11,212,1212};
int length = sizeof(a)/sizeof(a[0]);
//快速排序
quick_sort(a ,0, length-1);
cout <<"快速排序 a" << endl;
for_each(a,a+length,funoutput<int>);
cout << endl <<"------------------------------------------" <<endl;
//冒泡排序
bublle_sort(a1 ,length-1);
cout <<"冒泡排序 a1" << endl;
for_each(a1,a1+length,funoutput<int>);
cout << endl <<"------------------------------------------" <<endl;
//选择排序
select_sort(a2,length);
cout <<"选择排序 a2" << endl;
for_each(a2,a2+length,funoutput<int>);
cout << endl <<"------------------------------------------" <<endl;
//插入排序
insert_sort(a3,length);
cout <<"插入排序 a3" << endl;
for_each(a3,a3+length,funoutput<int>);
cout << endl <<"------------------------------------------" <<endl;
//归并排序
merge1(a4, 0, length-1);
cout <<"归并排序 a4" << endl;
for_each(a4,a4+length,funoutput<int>);
cout << endl <<"------------------------------------------" <<endl;
//希尔排序
shell_sort(a5,length);
cout <<"希尔排序 a5" << endl;
for_each(a5,a5+length,funoutput<int>);
cout << endl <<"------------------------------------------" <<endl;
//堆排序
MaxHeap_sort(a6,length);
cout <<"堆排序 a6" << endl;
for_each(a6,a6+length,funoutput<int>);
cout << endl <<"------------------------------------------" <<endl;
return 0;
}
实验结果: