自建测试class
class commonsort {
public:
void QuickSort(int arr[], int left, int right); //传入数组和排序区间[left,right]
bool MergeSort(int arr[], int len); //传入数组和数组长度
void BubbleSort(int arr[],int len);//传入数组和数组长度
private:
static int Mypartition(int arr[], int left, int right);
static void mergesort(int arr[], int first, int last, int temp[]);
static void mergearray(int arr[], int first, int mid, int last, int temp[]);
private:
static void swap(int arr[],int len,int a, int b);
};
code
void commonsort::QuickSort(int arr[], int left, int right) {
int pivot = 0;
if (left < right) {
pivot = Mypartition(arr, left, right);
QuickSort(arr, left, pivot - 1);
QuickSort(arr, pivot + 1, right);
}
}
int commonsort::Mypartition(int arr[], int left, int right) {
int key = arr[left];
while (left < right) {
while (left < right && arr[right] >= key) {
right--;
}
arr[left] = arr[right];
while (left < right && arr[left] <= key) {
left++;
}
arr[right] = arr[left];
}
arr[left] = key;
return left;
}
code
bool commonsort::MergeSort(int arr[], int len) {
int* temp = new int[len];
if (temp == NULL)
return false;
mergesort(arr, 0, len - 1, temp);
delete[] temp;
return true;
}
void commonsort::mergesort(int arr[], int first, int last, int temp[]) {
if (first < last)
{
int mid = (first + last) / 2;
mergesort(arr, first, mid, temp); //左边有序
mergesort(arr, mid + 1, last, temp); //右边有序
mergearray(arr, first, mid, last, temp); //再将二个有序数列合并
}
}
void commonsort::mergearray(int arr[], int first, int mid, int last, int temp[]) {
int i = first, j = mid + 1;
int m = mid, n = last;
int k = 0;
while (i <= m && j <= n)
{
if (arr[i] <= arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
while (i <= m)
temp[k++] = arr[i++];
while (j <= n)
temp[k++] = arr[j++];
for (i = 0; i < k; i++)
arr[first + i] = temp[i];
}
void commonsort::swap(int arr[],int len,int a, int b) {
if (arr == nullptr) return;
if (a < 0) return;
int temp;
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
void commonsort::BubbleSort(int arr[],int len) {
if (arr == nullptr) return;
for (int i = 0; i < len - 1; ++i) {
for (int j = 0; j < len - 1 - i; ++j) {
if (arr[j] > arr[j + 1]) {
swap(arr, len, j, j + 1);
}
}
}
}
int main() {
vector<int> arr(10000);
int arr1[10000];
int arr2[10000];
int arr3[10000];
commonsort s;
for (int i = 0; i < 10000; ++i) {
arr[i] = rand() % 1000;
arr1[i] = rand() % 1000;
arr2[i] = rand() % 1000;
arr3[i] = rand() % 1000;
}
clock_t time1 = clock();
sort(arr.begin(), arr.end());
clock_t time2 = clock();
s.QuickSort(arr1, 0, 10000 - 1);
clock_t time3 = clock();
s.MergeSort(arr2, 10000);
clock_t time4 = clock();
s.BubbleSort(arr3, 10000);
clock_t time5 = clock();
cout << "STL自带sort消耗时间:" << time2 - time1 << "ms" << endl;
cout << "快速排序消耗时间:" << time3 - time2 << "ms" << endl;
cout << "归并排序消耗时间:" << time4 - time3 << "ms" << endl;
cout << "冒泡排序消耗时间:" << time5 - time4 << "ms" << endl;
return 0;
}
在vs下Release版本测得数据
STL自带sort消耗时间:0ms
快速排序消耗时间:1ms
归并排序消耗时间:1ms
冒泡排序消耗时间:102ms
原文:https://www.cnblogs.com/Joy2013Ru/p/13704690.html