5、排序
背景知识:各种排序方法
目的要求:
- 掌握常见的排序算法的思想及其适用条件。
- 掌握常见的排序算法的程序实现。
实验内容:
输入一组关键字序列分别实现下列排序: - 实现简单选择排序、直接插入排序和冒泡排序。
- 实现希尔排序算法。
- 实现快速排序。
- 实现堆排序。
测试数据:已知待排序的一组记录的初始排列如下
R(49),R(38),R(65),R(97),R(76),R(13),R(27),R(49)
#include<cstdio>
#include<iostream>
using namespace std;
#define copy for(int i = 1; i <= n; i++) a[i] = arr[i];
#define print for(int i = 1; i <= n; i++) cout << a[i] << ' '; putchar('\n');
int read() {
int x;cin >> x; return x;}
const int maxn = 1e4;
int n;
int arr[maxn],a[maxn];
void swap(int &x,int &y)
{
int t = x;
x = y;
y = t;
}
void bubbleSort()
{
copy
int kase = 0;
for(int i = 1; i <= n-1; i++){
for(int j = 1; j <= n-i; j++)
if(a[j] > a[j+1]) swap(a[j],a[j+1]);
printf("第 %d 趟: ",++kase);
print
}
}
void selectionSort()
{
copy
int kase = 0;
for(int i = 1