算法导论 第二章

本文详细介绍了多种排序算法,包括插入排序、选择排序、归并排序、冒泡排序等,并提供了每种算法的实现代码示例。此外,还涉及了查找算法如线性查找、二分查找及其在排序中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第二章 插入排序

#include <iostream>
#include <string>
using namespace std;
template<class T>
void InsertSort(T array[], int length){
    int i;
    for (int j = 1; j < length; j++){
        T key = array[j];
        i = j - 1;
        while (i >= 0 && array[i] > key){//array[i]<key则为倒序排序
            array[i + 1] = array[i];
            i--;
        }
        array[i + 1] = key;
    }
};
int main(){
    int n;
    cout << "输入数组长度:" << endl;
    cin >> n;
    int *arrays = new int[n];
    cout << "输入数组的各个数字:" << endl;
    int j = 0;
    while (j < n){
        cin >> arrays[j];
        j++;
    }
    InsertSort(arrays, n);
    j = 0;
    while (j < n){
        cout << arrays[j] << " ";
        j++;
    }
    cout << endl;
    delete arrays;
    return 0;
}

P12 2.1-3 查找元素,返回下标

#include <iostream>
#include <string>
using namespace std;
template<class T>
int LinearlSearch(T array[], int length, T value){
	for (int i = 0; i < length; i++){
		if (value == array[i]){
			return i;
		}
	}
	return -1;
}
int main(){
	int n;
	cout << "输入数组长度:" << endl;
	cin >> n;
	int *arrays = new int[n];
	cout << "输入数组的各个数字:" << endl;
	int j = 0;
	while (j < n){
		cin >> arrays[j];
		j++;
	}
	cout << "输入要查找的数字:" << endl;
	int number;
	cin >> number;
	int v = LinearlSearch(arrays, n, number);
	cout << v << endl;
	delete arrays;
	return 0;
}

P12 2.1-4 二进制加法

#include <iostream>
#include <string>
using namespace std;
void BinarryAdd(int A[], int B[], int length, int C[]){
	int flag = 0;
	for (int i = length - 1; i >= 0; i--){
		C[i + 1] = A[i] + B[i] + flag;
		int key = C[i + 1] % 2;
		if (C[i + 1] >= 2){
			C[i + 1] = key;
			flag = 1;
		}
		else{
			flag = 0;
		}
	}
	if (flag == 1){
		C[0] = 1;
	}
}
int main(){
	int n;
	cout << "输入二进制位数:" << endl;
	cin >> n;
	int *arrays = new int[n];
	int *arrays1 = new int[n];
	cout << "输入第一个" << n << "位二进制数:" << endl;
	int j = 0;
	while (j < n){
		cin >> arrays[j];
		j++;
	}
	cout << "输入第二个" << n << "位二进制数:" << endl;
	j = 0;
	while (j < n){
		cin >> arrays1[j];
		j++;
	}
	int *result = new int[n + 1];
	result[0] = 0;
	BinarryAdd(arrays, arrays1, n, result);
	if (result[0] == 1){
		j = 0;
		while (j < n + 1){
			cout << result[j] << " ";
			j++;
		}
	}
	else{
		j = 1;
		while (j < n + 1){
			cout << result[j] << " ";
			j++;
		}
	}
	delete arrays, arrays1, result;
	return 0;
}

P16 2.2-2 选择排序

#include <iostream>
using namespace std;
template<class T>
void Swap(T &a, T &b){
	T temp = a;
	a = b;
	b = temp;
}
template<class T>
void SelectionSort(T array[], int length){
	for (int i = 0; i < length - 1; i++){
		int min = i;
		for (int j = i + 1; j < length; j++){
			if (array[j] < array[min]){
				min = j;
			}
		}
		Swap(array[i], array[min]);
	}
}
int main(){
	int n;
	cout << "输入数组长度:" << endl;
	cin >> n;
	int *arrays = new int[n];
	cout << "输入数组的各个数字:" << endl;
	int j = 0;
	while (j < n){
		cin >> arrays[j];
		j++;
	}
	SelectionSort(arrays, n);
	j = 0;
	while (j < n){
		cout << arrays[j] << " ";
		j++;
	}
	cout << endl;
	delete arrays;
	return 0;
}

第二章 归并排序

#include <iostream>
#define INT_MAX 0x7fffffff
using namespace std;
template<class T>
void Merge(T array[], int start1, int end1, int end2){
	int n1 = end1 - start1 + 1;
	int n2 = end2 - end1;
	T *Left = new T[n1 + 1];
	T *Right = new T[n2 + 1];
	int i, j;
	for (i = 0; i < n1; i++){
		Left[i] = array[start1 + i];
	}
	for (j = 0; j < n2; j++){
		Right[j] = array[end1 + 1 + j];
	}
	Left[n1] = INT_MAX;
	Right[n2] = INT_MAX;
	i = 0;
	j = 0;
	for (int k = start1; k <= end2; k++){
		if (Left[i] < Right[j]){
			array[k] = Left[i];
			i++;
		}
		else{
			array[k] = Right[j];
			j++;
		}
	}
	delete Left, Right;
}
template<class T>
void MergeSort(T array[], int start1, int end2){
	if (start1 < end2){
		int end1 = (start1 + end2) / 2;
		MergeSort(array, start1, end1);
		MergeSort(array, end1 + 1, end2);
		Merge(array, start1, end1, end2);
	}
}
int main(){
	int n;
	cout << "输入数组长度:" << endl;
	cin >> n;
	int *arrays = new int[n];
	cout << "输入数组的各个数字:" << endl;
	int j = 0;
	while (j < n){
		cin >> arrays[j];
		j++;
	}
	MergeSort(arrays, 0, n - 1);
	j = 0;
	while (j < n){
		cout << arrays[j] << " ";
		j++;
	}
	cout << endl;
	delete arrays;
	return 0;
}

P22 2.3-2 归并排序不用哨兵

#include <iostream>
using namespace std;
template<class T>
void Merge(T array[], int start1, int end1, int end2){
	int n1 = end1 - start1 + 1;
	int n2 = end2 - end1;
	T *Left = new T[n1];
	T *Right = new T[n2];
	int i, j;
	for (i = 0; i < n1; i++){
		Left[i] = array[start1 + i];
	}
	for (j = 0; j < n2; j++){
		Right[j] = array[end1 + 1 + j];
	}
	i = 0;
	j = 0;
	for (int k = start1; k <= end2; k++){
		if (i == n1){
			array[k] = Right[j];
			j++;
		}
		else if (j == n2){
			array[k] = Left[i];
			i++;
		}
		else{
			if (Left[i] < Right[j]){
				array[k] = Left[i];
				i++;
			}
			else{
				array[k] = Right[j];
				j++;
			}
		}
	}
	delete Left, Right;
}
template<class T>
void MergeSort(T array[], int start1, int end2){
	if (start1 < end2){
		int end1 = (start1 + end2) / 2;
		MergeSort(array, start1, end1);
		MergeSort(array, end1 + 1, end2);
		Merge(array, start1, end1, end2);
	}
}
int main(){
	int n;
	cout << "输入数组长度:" << endl;
	cin >> n;
	int *arrays = new int[n];
	cout << "输入数组的各个数字:" << endl;
	int j = 0;
	while (j < n){
		cin >> arrays[j];
		j++;
	}
	MergeSort(arrays, 0, n - 1);
	j = 0;
	while (j < n){
		cout << arrays[j] << " ";
		j++;
	}
	cout << endl;
	delete arrays;
	return 0;
}

P22 2.3-4 插入排序递归算法

#include <iostream>
using namespace std;
template<class T>
void Insertion(T array[], int first, int last){
int i = last - 1;
T key = array[last];
while (i >= 0 && array[i] > key){
array[i + 1] = array[i];
i--;
}
array[i+1] = key;
}
template<class T>
void RecursionInsertionSort(T array[], int first,int last){
if (first < last){
last = last - 1;
RecursionInsertionSort(array, first, last);
Insertion(array, first, last+1);
}
}
int main(){
int n;
cout << "输入数组长度:" << endl;
cin >> n;
int *arrays = new int[n];
cout << "输入数组的各个数字:" << endl;
int j = 0;
while (j < n){
cin >> arrays[j];
j++;
}
RecursionInsertionSort(arrays, 0, n - 1);
j = 0;
while (j < n){
cout << arrays[j] << " ";
j++;
}
cout << endl;
delete arrays;
return 0;
}

P22 2.3-5 二分查找递归算法

#include <iostream>
#include <string>
using namespace std;
template < class T >
int BinarySearch(T array[], int first, int last, T value){
	if (first <= last){
		int mid = (first + last) / 2;
		if (array[mid] > value){
			return BinarySearch(array, first, mid - 1, value);
		}
		else if (array[mid] < value){
			return BinarySearch(array, mid + 1, last, value);
		}
		else{
			return mid;
		}
	}
	else{
		return -1;
	}
}
int main(){
	int n;
	cout << "输入数组长度:" << endl;
	cin >> n;
	int *arrays = new int[n];
	cout << "输入数组的各个数字:" << endl;
	int j = 0;
	while (j < n){
		cin >> arrays[j];
		j++;
	}
	cout << "输入要查找的数字:" << endl;
	int number;
	cin >> number;
	int v = BinarySearch(arrays, 0, n - 1, number);
	cout << v << endl;
	delete arrays;
	return 0;
}

P22 2.3-6 二分查找插入排序

#include <iostream>
#include <string>
using namespace std;
template <class T >
int BinarySearchForInsertionSort(T array[], int first, int last, T value){
	if (first < last){
		int mid = (first + last) / 2;
		if (array[mid] > value){
			return BinarySearchForInsertionSort(array, first, mid - 1, value);
		}
		if (array[mid] < value){
			return BinarySearchForInsertionSort(array, mid + 1, last, value);
		}
	}
	else{
		if (array[first] > value){
			return first;
		}
		else{
			return first + 1;
		}
	}
}
template<class T>
void InsertSort(T array[], int first, int last){
	if (first < last){
		for (int j = 1; j <= last; j++){
			T key = array[j];
			int i = j - 1;
			int position = BinarySearchForInsertionSort(array, first, i, key);
			for (int x = i; x >= position; x--){
				array[i + 1] = array[i];
			}
			array[position] = key;
		}
	}
}
int main(){
	int n;
	cout << "输入数组长度:" << endl;
	cin >> n;
	int *arrays = new int[n];
	cout << "输入数组的各个数字:" << endl;
	int j = 0;
	while (j < n){
		cin >> arrays[j];
		j++;
	}
	InsertSort(arrays, 0, n - 1);
	j = 0;
	while (j < n){
		cout << arrays[j] << " ";
		j++;
	}
	cout << endl;
	delete arrays;
	return 0;
}

P22 2.3-7

#include <iostream>
#include <string>
using namespace std;
template < class T >
int BinarySearch(T array[], int first, int last, T value){
	if (first <= last){
		int mid = (first + last) / 2;
		if (array[mid] > value){
			return BinarySearch(array, first, mid - 1, value);
		}
		else if (array[mid] < value){
			return BinarySearch(array, mid + 1, last, value);
		}
		else{
			return mid;
		}
	}
	else{
		return -1;
	}
}
int main(){
	int n;
	cout << "输入数组长度:" << endl;
	cin >> n;
	int *arrays = new int[n];
	cout << "输入数组的各个数字:" << endl;
	int j = 0;
	while (j < n){
		cin >> arrays[j];
		j++;
	}
	cout << "输入要查找的数字:" << endl;
	int number;
	cin >> number;
	for (int i = 0; i < n; i++){
		int v = BinarySearch(arrays, 0, n - 1, number - arrays[i]);
		if (v != -1){
			cout << arrays[i] << "+" << arrays[v] << "=" << number << endl;
		}
	}
	delete arrays;
	return 0;
}

P23 2-2 冒泡排序

#include <iostream>
using namespace std;
template<class T>
void Swap(T &a, T &b){
	T temp = a;
	a = b;
	b = temp;
}
template<class T>
void BubbleSort(T array[], int length){
	for (int i = 0; i < length - 1; i++){
		for (int j = 0; j < length - 1 - i; j++){
			if (array[j] > array[j + 1]){
				Swap(array[j], array[j + 1]);
			}
		}
	}
}
int main(){
	int n;
	cout << "输入数组长度:" << endl;
	cin >> n;
	int *arrays = new int[n];
	cout << "输入数组的各个数字:" << endl;
	int j = 0;
	while (j < n){
		cin >> arrays[j];
		j++;
	}
	BubbleSort(arrays, n);
	j = 0;
	while (j < n){
		cout << arrays[j] << " ";
		j++;
	}
	cout << endl;
	delete arrays;
	return 0;
}

P23 2-3 霍纳规则求多项式

#include <iostream>
using namespace std;
double Horner(double array[], int length, double x){
	double y = 0;
	for (int i = length - 1; i >= 0; i--){
		y = array[i] + x*y;
	}
	return y;
}
int main(){
	int n;
	cout << "输入数组长度:" << endl;
	cin >> n;
	double *arrays = new double[n];
	cout << "输入数组的各个数字:" << endl;
	int j = 0;
	while (j < n){
		cin >> arrays[j];
		j++;
	}
	double x;
	cout << "输入x的值:" << endl;
	cin >> x;
	double result = Horner(arrays, n, x);;
	cout << result << endl;
	delete arrays;
	return 0;
}

P24 2-4 归并排序算逆序对

#include <iostream>
using namespace std;
int number = 0;
template<class T>
void Merge(T array[], int start1, int end1, int end2){
	int n1 = end1 - start1 + 1;
	int n2 = end2 - end1;
	T *Left = new T[n1];
	T *Right = new T[n2];
	int i, j;
	for (i = 0; i < n1; i++){
		Left[i] = array[start1 + i];
	}
	for (j = 0; j < n2; j++){
		Right[j] = array[end1 + 1 + j];
	}
	i = 0;
	j = 0;
	for (int k = start1; k <= end2; k++){
		if (i == n1){
			array[k] = Right[j];
			j++;
		}
		else if (j == n2){
			array[k] = Left[i];
			i++;
		}
		else{
			if (Left[i] < Right[j]){
				array[k] = Left[i];
				i++;
			}
			else{
				array[k] = Right[j];
				number = number + n1 - i;
				j++;
			}
		}
	}
	delete Left, Right;
}
template<class T>
void MergeSortForInversion(T array[], int start1, int end2){
	if (start1 < end2){
		int end1 = (start1 + end2) / 2;
		MergeSortForInversion(array, start1, end1);
		MergeSortForInversion(array, end1 + 1, end2);
		Merge(array, start1, end1, end2);
	}
}
int main(){
	int n;
	cout << "输入数组长度:" << endl;
	cin >> n;
	int *arrays = new int[n];
	cout << "输入数组的各个数字:" << endl;
	int j = 0;
	while (j < n){
		cin >> arrays[j];
		j++;
	}
	MergeSortForInversion(arrays, 0, n - 1);
	cout << number << endl;
	delete arrays;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值