A)需求分析:
1、冒泡排序
在要排序的一组数中,对当前还未排好序的范围内的全部数,自上 而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较 小的往上冒。
冒泡排序是稳定的。算法时间复杂度O(n2)--[n的平方] 2、选择排序
在要排序的一组数中,选出最小的一个数与第一个位置的数交换; 然后在剩下的数当中再找最小的与第二个位置的数交换,如此循环 到倒数第二个数和最后一个数比较为止。
选择排序是不稳定的。算法复杂度O(n2)--[n的平方] 3、插入排序
直接插入排序是稳定的。算法时间复杂度O(n2)--[n的平方] 4、折半插入排序
折半插入排序是对插入排序的改进,主要通过二分查找,获得插入的位置 折半插入是一种稳定的排序 排序时间复杂度O(n^2)附加空间O(1) 5、快速排序
快速排序是不稳定的。最理想情况算法时间复杂度O(nlog2n),最坏O(n2) 6、希尔排序
算法先将要排序的一组数按某个增量d分成若干组,每组中
记录的下标相差d.对每组中全部元素进行排序,然后再用一个较小的增量 对它进行,在每组中再进行排序。当增量减到1时,整个要排序的数被分成 一组,排序完成。
7、堆排序需要两个过程,一是建立堆,二是堆顶与堆的最后一个元素 交换位置。所以堆排序有两个函数组成。一是建堆的渗透函数,二是反复调用渗透函数
实现排序的函数。有最大堆和最少堆之分
堆排序是不稳定的。算法时间复杂度O(nlog2n)。 8、归并排序
归并排序是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。
归并排序是一种较稳定的排序 时间复杂度为时间O(nlogn) 9、基数排序
基数排序的方式可以采用LSD(Least significant digital)或MSD(Most significant digital),LSD的排序方式由键值的最右边开始,而MSD则相反,由键值的最左边开始。
基数排序是一种不稳定的排序,时间复杂度为:O(d(n+radix))
B)概要设计:
void insertsort(int *a );//插入排序函数
void Binsertsort(int *a ); //折半插入排序函数 void bubble_sort(int *a); //冒泡排序
void quick_sort(int *a , int low , int high) ;//快速排序
int one_quick_sort(int *a , int low , int high) ; //一趟快速排序
void select_sort(int *a); //直接选择排序
void merge_sort(int *a , int low , int high); //归并排序
void msort(int *a , int low , int high,int mid); //归并排序调用函数
void head_sort(int *a); //堆排序函数
void head_adgust(int *a , int low , int high); // 堆排序调用函数
int max_select_sort(int *a, int t); //选择最大数
void shell_insert(int *a , int dk); //希尔排序调用函数 void shell_sort(int *a); //希尔排序函数 void dadix_sort(int *a); //技术排序函数
int cmp1(int a,int b); //sort()函数里面的比较函数 int cmp2(int a,int b); //sort()函数里面的比较函数 void rand_sort(int *a) ; //随机产生函数 void display(int *a) ; //打印数组
C)详细设计:
// 12.cpp : Defines the entry point for the console application. //
#include \#include \#include \#include
int a[15]; //排序数组 int len = 15 ; //数组长度
void insertsort(int *a ); //插入排序函数
void Binsertsort(int *a ); //折半插入排序函数 void bubble_sort(int *a); //冒泡排序
void quick_sort(int *a , int low , int high) ; //快速排序 int one_quick_sort(int *a , int low , int high) ; //一趟快速排序
void select_sort(int *a); //直接选择排序 void merge_sort(int *a , int low , int high); //归并排序
void msort(int *a , int low , int high,int mid); //归并排序调用函数
void head_sort(int *a); //堆排序函数 void head_adgust(int *a , int low , int high); // 堆排序调
用函数
int max_select_sort(int *a, int t); //选择最大数
void shell_insert(int *a , int dk); //希尔排序调用函数
void shell_sort(int *a); //希尔排序函数 void dadix_sort(int *a); //技术排序函数
int cmp1(int a,int b); //sort()函数里面的比较函数
int cmp2(int a,int b); //sort()函数里面的比较函数
void rand_sort(int *a) ; //随机产生函数 void display(int *a) ; //打印数组
int main(int argc, char* argv[]) {
srand(unsigned(time(NULL))); //产生随即种子
cout<<\插入排序*********\ cout<<\随机产生数据:\ rand_sort(a); display(a);
cout<<\排序之后的数据\ insertsort(a); display(a); cout< cout<<\折半插入排序*********\ cout<<\随机产生数据:\ rand_sort(a); display(a); cout<<\排序之后的数据\ Binsertsort(a); display(a); cout< cout<<\冒泡排序*********\ cout<<\随机产生数据:\ rand_sort(a); display(a); cout<<\排序之后的数据\ bubble_sort(a); display(a); cout< cout<<\快速排序*********\cout<<\随机产生数据:\rand_sort(a); display(a); cout<<\排序之后的数据\quick_sort(a, 0 ,len-1); display(a); cout< cout<<\选择排序*********\cout<<\随机产生数据:\rand_sort(a); display(a); cout<<\排序之后的函数\select_sort(a); display(a); cout< cout<<\归并排序*********\cout<<\随机产生数据:\rand_sort(a); display(a); cout<<\排序之后的数据\merge_sort(a , 0 , len); display(a); cout< cout<<\堆排序*********\cout<<\随机产生数据:\rand_sort(a); display(a); cout<<\排序之后的数据\head_sort(a ); display(a); cout< cout<<\希尔排序*********\cout<<\随机产生数据:\rand_sort(a); display(a); cout<<\排序之后的数据\shell_sort(a); display(a); cout< cout<<\基数排序*********\ cout<<\随机产生数据:\ rand_sort(a); display(a); cout<<\排序之后的数据\ dadix_sort(a); display(a); cout< void insertsort(int *a ) { int temp; for (int i = 1 ; i < len ; ++i ) { if (a[i - 1] > a[i]) { temp = a[i]; a[i] = a[i-1]; for (int j = i - 1 ; temp < a[j] ; --j ) { a[j+1] = a[j]; } a[j+1] = temp; } } } void Binsertsort(int *a ) //折半插入排序函数 { int temp; int low,high,mid; for (int i = 1 ; i < len ; ++i ) { temp = a[i]; low = 0 ; high = i- 1 ; while (low <= high) { mid = (low + high ) / 2 ; if ( temp < a[mid]) { high = mid -1 ; }