//
// main.cpp
// Sorting
//
// Created by yanzhengqing on 12-12-6.
// Copyright (c) 2012年 yanzhengqing. All rights reserved.
#include <iostream>
using namespace std;
int input_array(intconst *p)
{
printf("please input the array numbers......\n");
for (uint i =0; i <10;i++)
{
scanf("%d",p++);
}
return0;
}
int output_array(intconst *p)
{
for (uint i =0; i <10;i++)
{
printf("%d\t",*(p++) );
}
return0;
}
/直接插入排序
int Straight_Insertion_sort(int *a,int n) //这里可以int a[]
{
uint i =0, j =0;
int temp =0;
for(i =1; i < n; ++i)
{
temp = a[i];
for (j = i; j >0 && temp < a[j -1]; j--) //精髓是从后向前比较
{
a[j] = a[j -1];
}
a[j] = temp;
}
//其实这里想了想还有比较好的方法可以减少比较的次数,但移动的次数不变!那就是比较的时候进行折半查找
//换句话说就是利用有序表进行折半查找再插入。原先的是比较一次移动一下,现在是直接定位然后一次性移动。
printf("Straight Insertion sort\n");
return0;
}
///希尔排序 Diminishing increment sort也属于插入排序的一种
/*
基本思想是先将整个待排记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录基本有序的时候,在对全体进行一次直接插入排序
同时注意应使增量序列中没有除1以外的公因子,并且最后一个增量值必须等于1
实例数据
//21 32 43 22 56 87 65 49 76 10
//a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
//a[0] a[3] a[6] a[9]
//21 32 43 22 10 87 65 49 76 56
*/
int shell_sort(int *a,int n)
{
int k =5;//初始化增量,我这里取的5然后是3最后是1.具体增值设为多少为好这还是个数学难题(严蔚敏数据结构上介绍,大家可以探索探索)
int raid =0, num =1;
int temp =0, j =0;
printf("Shell's sort\n");
while (k>0)
{
while(raid<k) //一共分为k个批次,k个批次排序结束为一趟
{
for(int i = k+raid; i < n; i+=k)
{
temp = a[i];
for (j = i; j-k >=0 && temp < a[j - k]; j-=k) //精髓是从后向前比较
{
a[j] = a[j - k];
}
a[j] = temp;
}
raid++;
}
cout<<"k="<<k<<endl;
cout<<"这是第~~"<<num<<"~~趟排序结果"<<endl;
output_array(a);
cout<<endl;
raid =0;
num ++;
k -=2;
}
return0;
}
/* 冒泡排序其属于快速排序范畴,快速排序是冒泡排序的改进
若初始为正序,则只需要进行一趟排序,在排序的过程中进行n-1次元素之间的比较
若初始为逆序,则需要进行n-1趟排序,进行n(n-1)/2次元素之间的比较
数据范例
21 32 43 22 56 87 65 49 76 10
第一趟: 21 32 22 43 56 65 49 76 10 87
第二趟: 21
*/
int Bubble_sort(int *a,int n)
{
int temp =0,l =1;
bool flag =false; //标记使用,当循环里没有变化时表示排序结束。
for(int i =1 ;i<=n;n--)
{
for(int j =1 ;j<n ;j++)
{
if(a[j-1]>a[j])
{
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
flag =true;
}
}
if(!flag)
{
cout<<"bubble_sort最后一趟排序"<<endl;
output_array(a);
cout<<endl;
break;
}
else
{
cout<<"bubble_sort第~~"<<l<<"~~趟排序"<<endl;
output_array(a);
cout<<endl;
flag =false;
l++;
}
}
return0;
}
/**********************************************************************************************/
//快速排序
//数据范例
//21 32 43 22 56 87 65 49 76 10
//第一趟 102143 22 56 876549 76 32
int Quicksort_swap(int *pleft ,int *pright)
{
int temp =0;
temp = *pleft;
*pleft = *pright;
*pright = temp;
return0;
}
int Quick_sort(int *a,int begin ,int end)
{
int compare = a[begin],left = begin,right = end;
if (left >= right)
{
return0;
}
while (left < right)//此下循环为一趟排序,每一趟排序总有一个元素在最终确定位置
{
while((left < right) && a[right] >= compare)
{
right --;
}
swap(a[left],a[right]);
while ((left < right) && (a[left] < compare))
{
left ++;
}
swap(a[right],a[left]);
}
Quick_sort(a,begin,right-1);
Quick_sort(a,right+1,end);
return0;
}
/**********************************************************************************************/
//简单选择排序
int simple_selecting_sort(int *a,int n)
{
//21 32 43 22 56 87 65 49 76 10
int temp =0 , k =0;
for(int i =0;i<n;i++)
{
k = a[i];
for(int j = i+1;j<n;j++) //寻找最小的元素
{
if(a[j]<k)
{
temp = a[j];
a[j] = k;
k = temp;
}
}
a[i] = k;
}
return0;
}
/**********************************************************************************************/
//二路归并排序
int Merge(int *r,int temp[],int s,int m,int t)
{
int i = s;
int j = m+1;
int k =s;
while ((i<=m) && (j<=t)) //保证两个序列比较中一个序列的排序完全结束
{
if (r[i] <= r[j])
{
temp[k++] = r[i++];
}
else
{
temp[k++] = r[j++];
}
}
if(i<=m) //赋值剩下的序列
{
while(i<=m)
{
temp[k++]=r[i++];
}
}
else
{
while (j<=t)
{
temp[k++]=r[j++];
}
}
for (int l = s;l<=t;l++) //将辅助数组的值赋值给原始数组
{
r[l] = temp[l];
}
return0;
}
//21 9 43 22 56 87 65 49 76 10
int merging_sort(int *r,int temp[],int s,int t)
{
if(s==t)
return0;
else
{
int m = (s+t)/2;
merging_sort(r,temp,s,m);
merging_sort(r,temp,m+1,t);
Merge(r,temp,s,m,t);
}
return0;
}
int show_faction(int &a_input)
{
cout<<"排序算法选择"<<endl;
cout<<"1.直接插入排序"<<endl;
cout<<"2.希尔排序"<<endl;
cout<<"3.冒泡排序"<<endl;
cout<<"4.快速排序"<<endl;
cout<<"5.归并排序"<<endl;
cout<<"请输入排序算法选择......."<<endl;
cin>>a_input;
return0;
}
int main(int argc,constchar * argv[])
{
int array[10] = {0},temp[10] ={0};
int choose =0;
input_array(array);
show_faction(choose);
switch(choose)
{
case1:
Straight_Insertion_sort(array,sizeof(array)/sizeof(int));
case2:
shell_sort(array,sizeof(array)/sizeof(int));
case3:
Bubble_sort(array,sizeof(array)/sizeof(int));
case4:
Quick_sort(array,0,((sizeof(array)/sizeof(int))-1));
case5:
merging_sort(array,temp,0,((sizeof(array)/sizeof(int))-1));
default:
break;
}
output_array(array);
return0;
}
please input the array numbers......
1
23
43
12
54
98
665
34
21
23
排序算法选择
1.直接插入排序
2.希尔排序
3.冒泡排序
4.快速排序
5.归并排序
请输入排序算法选择.......
4
112 21 23 23 3443 54 98 665