博主打算将算法导论上的所有算法用C++实现一遍,以锻炼自己的算法能力和熟悉C++。我把第二章——算法基础 所有能用C++实现的算法都实现了一遍,包括练习题和给的伪代码。还是先贴代码。
#include<iostream>
using namespace std;
//insertion-sert
template<class T>
void Insertion_sort(T* a, int n)
{
int i, j;
T key;
for (j = 1; j < n; ++j)
{
key = a[j];
i = j - 1;
while (i >=0 && a[i] > key)
{
a[i + 1] = a[i];
i--;
}
a[i + 1] = key;
}
}
//solution to 2.1-2
//Insertion_sort sort by decrease order
template<class T>
void Insertion_d_sort(T* a, int n)
{
int i, j;
for (j = 1; j < n; j++)
{
T key;
key = a[j];
i = j - 1;
while (i >= 0 && a[i] < key)
{
a[i + 1] = a[i];
i--;
}
a[i + 1] = key;
}
}
//also solution to 2.2-2
//this is the select sort
template<class T>
void exchange(T&a, T&b)
{
T c;
c = a;
a= b;
b = c;
}
template<class T>
void Select_sort(T*a, int n)
{
for (int j = 0; j < n - 1; j++)
{
int smallest = j;
for (int i = j + 1; i < n; i++)
if (a[i] < a[smallest])
smallest = i;
exchange(a[j], a[smallest]);
}
}
//solution to 2.1-3
template<class T>
int find_value(T*a, T v, int n)
{
int i;
int NIL = -1;
for ( i = 0; i < n; i++)
if (a[i] == v)
return i;
if (i == n)
return NIL;
}
//solution to 2.1-4
void binary_add(int *a, int *b,int *C, int n)
{
int Cy = 0;
int i;
for (i = 0; i < n; i++)
{
int sum = a[i] + b[i] + Cy;
if (sum > 1)
Cy = 1;
else
Cy = 0;
C[i] = sum % 2;
}
if (i == n&&Cy == 1)
C[n] = 1;
}
//test for binary_add ;
/*int main()
{
int a[5] = { 1,1,0,1,0 };
int b[5] = { 1,0,0,1,0 };
int c[6] = { 0 };
binary_add(a, b, c, 5);
for (int i = 0; i < 6; i++)
cout << c[i];
cout << endl;
}
*/
//test for find_value;
/*
int main()
{
double x[5] = { 3,1.2,4.3,3,6};
Insertion_sort1(x, 5);
for (int i = 0; i < 5; i++)
cout << x[i] << " ";
cout << endl;
int c;
c=find_value(x, 6.0, 5);
cout << c<< endl;
}
*/
//merge_sort
//the sub-program of the merge_sort
template<class T>
void Merge(T*a, int p, int q, int r)
{
int n1 = q-p+1;
int n2 = r-q;
T* b=new T[n1];
for (int i = 0; i < n1; i++)
b[i] = a[p + i ];
T* c=new T[n2+1];
for (int j = 0; j < n2; j++)
c[j] = a[q + j + 1];
int i = 0, j = 0, k;
for ( k = p; k <= r; k++)
if( b[i] <= c[j])
{
a[k] = b[i];
i++;
if (i == n1)
break;
}
else
{
a[k] = c[j];
j++;
if (j == n2)
break;
}
if (i == n1)
for (; j <n2; j++)
{
a[k+1] = c[j];
k++;
}
if (j == n2)
for (; i < n1; i++)
{
a[k+1] = b[i];
k++;
}
delete[]c;
delete[]b;
}
//this is the real Merge_sort
template<class T>
void Merge_sort(T* a,const int p, const int r)
{
if (p <r)
{
int q = (p + r) / 2;
Merge_sort(a, p, q);
Merge_sort(a, q+1, r);
Merge(a, p, q, r);
}
}
//solution to 2.3-4 utilize the Insertion_sort in recursive way
template<class T>
void rec_Insertion_sort(T* a, int n)
{
T temp;
int i;
if (n > 1)
rec_Insertion_sort(a, n - 1);
i = n - 2;
temp = a[n - 1];
while (a[i] > temp&&i >= 0)
{
a[i + 1] = a[i];
i--;
}
a[i + 1] = temp;
}
//solution to 2.3-5 . this assumes the order is sorted!
template<class T>
int binary_locate(T*a, T v,int low,int high)
{
if (low <= high)
{
int half = (low + high);
if (a[half] == v)
return half;
else if (a[half] > v)
return binary_locate(a, v, low, half - 1);
else if (a[half] < v)
return binary_locate(a, v, half + 1, high);
}
else
return -1;
}
//solution to 2.3-6 use binary search to reduce the time consumed
//and the first sub-program is the binaray search
template<class T>
int binary_search(T* a, T v, int len)
{
int low = 0, high = len - 1, mid;
while (low <=high)
{
mid = (low + high) / 2;
if (v <= a[mid])
high = mid - 1;
else
low = mid + 1;
}
return low;
}
template<class T>
void binary_Insert_sort(T*a,int n)
{
int i, j;
T temp;
int mid;
for(i=1;i<n;i++)
{
temp = a[i];
j = i - 1;
mid=binary_search(a, temp, i);
for (; j >= mid; j--)
{
a[j + 1] = a[j];
}
a[j+1] = temp;
}
}
int main()
{
double a[11] = { 3.4,5.6,43,55.3,2.1,0.4,-4.2,5.4,2.09,3.2222,3.21 };
for (int i = 0; i < 10; i++)
cout << a[i] << " ";
cout << endl;
binary_Insert_sort(a, 11);
for (int i = 0; i < 11; i++)
cout << a[i] << " ";
cout << endl;
//int c,t;
//c = binary_locate(a, 3.0, 0, 11);
//t = binary_locate(a,
5.6, 0, 11);
//cout << "C" << c << " " << "t" << t << endl;
return 0;
}
该算法是所有的实现,注
有部分算法是有问题的,比如2进制的想加,用数组存储,数组的第一位是最低位,数组的最高位是最后一位,因为题目并没有规定是以什么顺序排列,我就直接用了最简单的方式,还有一个归并排序,算法导论上说设置一个哨兵,但是本人感觉那个哨兵的定义只有在特定的问题中才能找到那个值,
而算法导论上说的无穷,对于计算机还真表示不出来,所以我直接实现了不用哨兵的。
如果发现我的程序有问题,欢迎大家指出!