第二章 插入排序
#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;
}