《c++程序设计》第5版郑莉老师第九章部分答案来啦~~
分享以下有关排序的函数模板,以备不时之需!
初始化 int类型数组 datal[]={1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20}
头文件都是:
#include<iostream>
using namespace std;
-
应用直接插入排序函数模板进行排序
template<class T>
void insertionSort(T a[], int n) {
int i, j;
T temp;
for (i = 1; i < n; i++) {
j = i;
temp = a[i];
while (j > 0 && temp < a[j - 1]) {
a[j] = a[j - 1];
j--;
}
a[j] = temp;
for (int k = 0; k < n; k++)
cout << a[k] << " ";
cout << endl;
}
}
int main() {
int datal[] = { 1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20 };
cout << "排序前:" << endl;
for (int i = 0; i < 20; i++)
cout << datal[i] << " ";
cout << endl;
cout << "排序中:" << endl;
insertionSort(datal, 20);
cout << "排序后:" << endl;
for (int i = 0; i < 20; i++)
cout << datal[i] << " ";
cout << endl;
return 0;
}
-
直接选择排序函数模板
void mySwap(T& x, T& y) {
T temp = x;
x = y;
y = temp;
}
template<class T>
void selectionSort(T a[], int n) {
for (int i = 0; i < n - 1; i++) {
int leastIndex = i;
for (int j = i + 1; j < n; j++)
if (a[j] < a[leastIndex])
leastIndex = j;
mySwap(a[i], a[leastIndex]);
for (int k = 0; k < n; k++)
cout << a[k] << " ";
cout << endl;
}
}
int main() {
int datal[] = { 1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20 };
cout << "排序前:" << endl;
for (int i = 0; i < 20; i++)
cout << datal[i] << " ";
cout << endl;
cout << "排序中:" << endl;
selectionSort(datal, 20);
cout << "排序后:" << endl;
for (int i = 0; i < 20; i++)
cout << datal[i] << " ";
cout << endl;
return 0;
}
-
起泡排序函数(升序)
template<class T>
void mySwap(T& x, T& y) {
T temp = x;
x = y;
y = temp;
}
template<class T>
void bubbleSort(T a[], int n) {
int i = n - 1;
while (i > 0) {
int lastExchangeIndex = 0;
for(int j=0;j<i;j++)
if (a[j + 1] < a[j]) {
mySwap(a[j], a[j + 1]);
lastExchangeIndex = j;
}
i = lastExchangeIndex;
for (int k = 0; k < n; k++)
cout << a[k] << " ";
cout << endl;
}
}
int main() {
int datal[] = { 1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20 };
cout << "排序前:" << endl;
for (int i = 0; i < 20; i++)
cout << datal[i] << " ";
cout << endl;
cout << "排序中:" << endl;
bubbleSort(datal, 20);
cout << "排序后:" << endl;
for (int i = 0; i < 20; i++)
cout << datal[i] << " ";
cout << endl;
return 0;
}
-
降序起泡排序
#include<iostream>
using namespace std;
template<class T>
void mySwap(T& x, T& y) {
T temp = x;
x = y;
y = temp;
}
template<class T>
void bubbleSort1(T a[], int n) {
int i = n - 1;
while (i > 0) {
int lastExchangeIndex = 0;
for (int j = 0; j < i; j++)
if (a[j + 1] > a[j]) { //与升序的相比,就改了个大于号
mySwap(a[j], a[j + 1]);
lastExchangeIndex = j;
}
i = lastExchangeIndex;
for (int k = 0; k < n; k++)
cout << a[k] << " ";
cout << endl;
}
}
int main() {
int datal[] = { 1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20 };
cout << "排序前:" << endl;
for (int i = 0; i < 20; i++)
cout << datal[i] << " ";
cout << endl;
cout << "排序中:" << endl;
bubbleSort1(datal, 20);
cout << "排序后:" << endl;
for (int i = 0; i < 20; i++)
cout << datal[i] << " ";
cout << endl;
return 0;
}
-
顺序查找函数模板
#include<iostream>
using namespace std;
template<class T>
int seqSearch(const T list[], int n, const T & key) {
for (int i = 0; i < n; i++)
if (list[i] == key)
return i;
return -1;
}
int main() {
int datal[] = { 1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20 };
cout << "排序前:" << endl;
for (int i = 0; i < 20; i++)
cout << datal[i] << " ";
cout << endl;
int a;
cout << "请输入一个数字:" << endl;
cin >> a;
seqSearch(datal, 20,a);
cout << "排序后:" << endl;
for (int i = 0; i < 20; i++)
cout << datal[i] << " ";
cout << endl;
return 0;
}
-
折半查找函数
#include<iostream>
using namespace std;
template<class T>
void insertionSort(T a[], int n) { //随便用一种排序方法
int i, j;
T temp;
for (i = 1; i < n; i++) {
j = i;
temp = a[i];
while (j > 0 && temp < a[j - 1]) {
a[j] = a[j - 1];
j--;
}
a[j] = temp;
}
}
template<class T>
int binSearch(const T list[], int n, const T &key) { //得其位置
int low = 0;
int high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (key == list[mid])
return mid;
else if (key < list[mid])
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
int main() {
int datal[] = { 1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20 };
cout << "排序前:" << endl;
for (int i = 0; i < 20; i++)
cout << datal[i] << " ";
cout << endl;
insertionSort(datal, 20);
cout << "利用直接插入排序后:" << endl;
for (int i = 0; i < 20; i++)
cout << datal[i] << " ";
cout << endl;
int a;
cout << "请输入需要判断数组元素位置的数字(数组第一个元素序号为0):";
cin >> a;
cout << binSearch(datal, 20, a) << endl;
return 0;
}