排序算法
冒泡排序
- 从左向右扫描数据,选着最大的数据。
- 要点:比较相邻的两个数,如果左边的数大于右边的数就进行交换。
template<class T>
void BubbleSort(T *array, const int length)
{
for (int i = 0; i <length-1; i++)
{
for (int j = i+1; j <length; j++)
{
if (array[i]> array[j])
std::swap(array[i], array[j]);
}
}
}
选择排序
- 从当前未排序的整数中选着一个最小的整数,将他放在已排序按的整数列表的最后。
- 要点:选择排序最小的
- 冒泡排序与选择排序
template<class T>
void SelectSort(T *array, const int length)
{
for (int i = 0; i < length; i++)
{
int min = i;
for (int j = i + 1; j < length; j++)
{
if (array[j] < array[min])
min = j;
}
std::swap(array[i], array[min]);
}
}
查找算法
顺序查找
- 数据没排序,查找速度慢
template<class T>
int SequentSearch(T *array, const int length,const T x)
{
int i;
for (i = 0; i < length; i++)
{
if (array[i] == x)
return i;
}
return -1;
}
二分查找
- 数据需要排序,查找速度快
template<class T>
int BinarySearch(T *array, const int length, const T x)
{
int low, hight, mid;
low = 0, hight = length - 1;
while (low <= hight)
{
mid = (low + hight) / 2;
if (array[mid] == x)
return mid;
else if (array[mid] < x)
low = mid + 1;
else if (array[mid] >x)
hight = mid - 1;
}
return -1;
}
递归算法
- 难点:多入口递归算法的实现,递归实现的代码都可以是使用使用循来实现,递归耗费内存,在自己调用自己是,是否应该加return,需要考虑,一般需要加上return。递归算法需要深入研究才能掌握。
递归二分查找算法
template<class T>
int Recursive_BinarySearch(T * array, const int length, const T x,const int left ,const int right)
{
if (left <= right)
{
int mid = (left + right) / 2;
if (x < array[mid])
return Recursive_BinarySearch(array, length, x, left, mid - 1);
// 注意函数前需要加一个return,没加return,结果不正确
else if(x > array[mid])
return Recursive_BinarySearch(array, length, x, mid+ 1, right);
else return mid;
}
return-1;
}
递归实现排列组合问题
template<class T>
void Permutations(T * array,int left,int right)
{
if (left == right)
{
for (int i = 0; i < right; i++)
cout << array[i];
cout << endl;
}
else
{
for (int i = left; i < right; i++)
{
std::swap(array[left], array[i]);
Permutations(array,left+1,right);
std::swap(array[left], array[i]);
}
}
}
#include<iostream>
using namespace std;//排序算法
template<class T> void BubbleSort(T *array, const int length);//冒泡
template<class T> void B_sort(T *array, const int length);
template<class T> void SelectSort(T *array, const int length);//选择//T *array,使用指针,速度快,传递的数数组的地址,T array[],这种写法为传值,需要大量复制数据//查找算法
template<class T> int SequentSearch(T *array, const int length, const T x);//顺序查找,没排序的数据
template<class T>int Recursive_BinarySearch(T *array, const int length, const T x,const int left=0,const int right=0);//递归二分查找,排序的数据
template<class T>int BinarySearch(T *array, const int length, const T x);//二分查找,排序的数据
template<class T>void Permutations(T *array,int left,int right);//实现数组中元素的所有组合
int main()
{double a[] = { 1.1,1.9,2.5,9.2,9.5,4.0,3.2,4.5,6.7,8.4 };BubbleSort(a, 10); cout << "冒泡排序:" << endl;for (int i = 0; i < 10; i++){cout << a[i] << endl;}B_sort(a, 10); cout << "冒泡排序:" << endl;for (int i = 0; i < 10; i++){cout << a[i] << endl;}int b[] = {5,6,1,4,8,3,6,7,2,9,0 };SelectSort(b, 10); cout << "选择排序:" << endl;for (int i = 0; i < 10; i++){cout << b[i] << endl;}double x = 4.0;int i = SequentSearch(a, 10, x);if (i > 0){cout << "顺序查找:" << endl; cout << i<<","<<a[i] << endl;}int y =8;int j = BinarySearch(b, 10,y);if (j> 0)cout << j<< "," << b[j] << endl;double z =5;int k = Recursive_BinarySearch(a, 10,z,0,10);if (k > 0){cout << "递归查找:" << endl;cout << k << "," << b[k] << endl;}char ch[] = { 'a','b','c' };cout << "字母组合:" << endl;Permutations(ch,0, 3);system("pause");return 0;
}template<class T> void BubbleSort(T *array, const int length)
{for (int i = 0; i < length; i++){for (int j = 0; j < length - 1 - i; j++){if(array[j]> array[j+1])std::swap(array[j],array[j+1]);}}
}template<class T> void B_sort(T *array, const int length)
{for (int i = 0; i <length-1; i++){for (int j = i+1; j <length; j++){if (array[i]> array[j])std::swap(array[i], array[j]);}}
}template<class T>
void SelectSort(T *array, const int length)
{for (int i = 0; i < length; i++){int min = i;for (int j = i + 1; j < length; j++){if (array[j] < array[min])min = j;}std::swap(array[i], array[min]);}
}template<class T>
int SequentSearch(T *array, const int length,const T x)
{int i;for (i = 0; i < length; i++){if (array[i] == x)return i;}return -1;
}template<class T>
int Recursive_BinarySearch(T * array, const int length, const T x,const int left ,const int right)
{if (left <= right){int mid = (left + right) / 2;if (x < array[mid])return Recursive_BinarySearch(array, length, x, left, mid - 1);// 注意函数前需要加一个return,没加return,结果不正确else if(x > array[mid])return Recursive_BinarySearch(array, length, x, mid+ 1, right);else return mid;}return-1;
}template<class T>
int BinarySearch(T *array, const int length, const T x)
{int low, hight, mid;low = 0, hight = length - 1;while (low <= hight){mid = (low + hight) / 2;if (array[mid] == x)return mid;else if (array[mid] < x)low = mid + 1;else if (array[mid] >x)hight = mid - 1;}return -1;
}template<class T>
void Permutations(T * array,int left,int right)
{if (left == right){for (int i = 0; i < right; i++)cout << array[i];cout << endl;}else{for (int i = left; i < right; i++){std::swap(array[left], array[i]);Permutations(array,left+1,right);std::swap(array[left], array[i]);}}
}
结果: