实现了选择排序, 插入排序, 冒泡排序, 快速排序, 改进后的快速排序, 以及两路合并排序.
通过随机函数随机生成100个数, 进行各种排序, 记录排序开始时间以及结束时间, 计算消耗的时间来比较算法的优略.
实现代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
#include "cstdlib"
#include "ctime"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1005;
template <class T>
void SelectSort(T A[], int n)
{
int small;
for(int i = 0 ; i < n; ++i) {
small = i;
for(int j = i + 1; j < n; ++j)
if(A[j] < A[small]) small = j;
swap(A[i], A[small]);
}
}
template <class T>
void InsertSort(T A[], int n)
{
for(int i = 1; i < n; ++i) {
int j = i;
T tmp = A[j];
while(j > 0 && tmp < A[j - 1]) {
A[j] = A[j - 1];