上述代码用c++语言呈现
时间: 2024-02-09 10:11:54 浏览: 20
好的,下面是使用 C++ 语言实现的上述代码:
1. 简单选择排序:
```c++
void selection_sort(int array[], int n) {
for (int i = 0; i < n - 1; i++) {
int min_index = i;
for (int j = i + 1; j < n; j++) {
if (array[j] < array[min_index]) {
min_index = j;
}
}
if (min_index != i) {
int temp = array[i];
array[i] = array[min_index];
array[min_index] = temp;
}
}
}
```
2. 直接插入排序:
```c++
void insertion_sort(int array[], int n) {
for (int i = 1; i < n; i++) {
int key = array[i];
int j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = key;
}
}
```
3. 冒泡排序:
```c++
void bubble_sort(int array[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
```
4. 希尔排序:
```c++
void shell_sort(int array[], int n) {
for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i++) {
int temp = array[i];
int j = i;
while (j >= gap && array[j - gap] > temp) {
array[j] = array[j - gap];
j -= gap;
}
array[j] = temp;
}
}
}
```
5. 快速排序:
```c++
void quick_sort(int array[], int left, int right) {
if (left >= right) {
return;
}
int pivot_index = partition(array, left, right);
quick_sort(array, left, pivot_index - 1);
quick_sort(array, pivot_index + 1, right);
}
int partition(int array[], int left, int right) {
int pivot = array[right];
int i = left - 1;
for (int j = left; j < right; j++) {
if (array[j] < pivot) {
i++;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
int temp = array[i + 1];
array[i + 1] = array[right];
array[right] = temp;
return i + 1;
}
```
最后,在主函数中测试这些算法的代码如下:
```c++
#include <iostream>
using namespace std;
void selection_sort(int array[], int n);
void insertion_sort(int array[], int n);
void bubble_sort(int array[], int n);
void shell_sort(int array[], int n);
void quick_sort(int array[], int left, int right);
int partition(int array[], int left, int right);
int main() {
int array[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
int n = sizeof(array) / sizeof(int);
cout << "Original array: ";
for (int i = 0; i < n; i++) {
cout << array[i] << " ";
}
cout << endl;
selection_sort(array, n);
cout << "Selection sort: ";
for (int i = 0; i < n; i++) {
cout << array[i] << " ";
}
cout << endl;
int array2[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
insertion_sort(array2, n);
cout << "Insertion sort: ";
for (int i = 0; i < n; i++) {
cout << array2[i] << " ";
}
cout << endl;
int array3[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
bubble_sort(array3, n);
cout << "Bubble sort: ";
for (int i = 0; i < n; i++) {
cout << array3[i] << " ";
}
cout << endl;
int array4[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
shell_sort(array4, n);
cout << "Shell sort: ";
for (int i = 0; i < n; i++) {
cout << array4[i] << " ";
}
cout << endl;
int array5[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
quick_sort(array5, 0, n - 1);
cout << "Quick sort: ";
for (int i = 0; i < n; i++) {
cout << array5[i] << " ";
}
cout << endl;
return 0;
}
```
希望这些代码能够帮到你,如果还有其他问题,欢迎随时提问。