MOOC清华《程序设计基础》第4章:折半插入排序(函数写法)

来源:互联网 发布:淘宝的减肥药有用吗 编辑:程序博客网 时间:2024/06/05 18:02
#include <iostream>using namespace std;int main(){int a[17]={56,32,67,12,23,89,3,14,2,25,43,31,1,24,44,35,76};void BinaryInsectionSort(int cards[],int n);void OutputOfArray(int cards[],int n);OutputOfArray(a,10);BinaryInsectionSort(a,10);OutputOfArray(a,10);return 0;}void BinaryInsectionSort(int cards[],int n){for(int i = 1; i < n; i++){int target = cards[i];int low = 0, high = i - 1, id = -1;while (low <= high){int middle = (low + high) / 2;if(cards[middle] >= target)high = middle - 1;elselow = middle + 1;  }id = high + 1;if(id != -1){for(int j = i; j > id; j--)  cards[j] = cards[j - 1];  cards[id] = target; } }}void OutputOfArray(int cards[],int n){for(int i = 0; i < n; i++)cout << cards[i] << '\t';cout << endl;}

其中

void BinaryInsectionSort(int cards[],int n);void OutputOfArray(int cards[],int n);
也可以省略数组名,写成

void BinaryInsectionSort(int [],int n);void OutputOfArray(int [],int n);


阅读全文
0 0
原创粉丝点击