почему у меня получается что быстрая сортировка скорее слиянием
Quick sort
#include<iostream>
#include<time.h>
#include<Windows.h>
#define N 80000
using namespace std;
int* ar;
int swaps = 0, compares = 0;
double Th()
{
SYSTEMTIME time;
GetSystemTime(&time);
double chas;
chas = time.wHour * 60 * 60 * 1000 + time.wMinute * 60 * 1000 + time.wSecond * 1000 + time.wMilliseconds;
return chas;
}
void funcprint(int arr[], int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
void quickSort(int a[], int size) {
if (size < 2) {
return;
}
long i = 0, j = size - 1;
int p = a[size >> 1];
while (i < j) {
while (a[i] < p) { compares++; i++; }
while (a[j] > p) { compares++; j--; }
if (i < j) {
swaps++;
swap(a[i++], a[j--]);
}
}
quickSort(a, j);
quickSort(a + i, size - i);
}
int main() {
int* a = new int[N];
ar = a;
srand(time(NULL));
for (int i = 0; i <= N; i++) {
a[i] = rand();
}
double T, t1, t0;
t0 = Th();
quickSort(a, N);
t1 = Th();
T = (t1 - t0) / 1000.0;
cout << "Quick sort" << endl;
cout << "Number of elements:" << N << endl;
cout << "compares=" << compares << endl;
cout << "swaps=" << swaps << endl;
cout << "Time:" << T << endl;
}
Merge sort
#include <iostream>
#include<time.h>
#include<Windows.h>
#define N 80000
int swaps = 0, compares = 0;
using namespace std;
double Th()
{
SYSTEMTIME time;
GetSystemTime(&time);
double chas;
chas = time.wHour * 60 * 60 * 1000 + time.wMinute * 60 * 1000 + time.wSecond * 1000 + time.wMilliseconds;
return chas;
}
void Merge(int* A, int first, int last) {
int middle, start, final, j;
int* mas = new int[N];
middle = (first + last) / 2;
start = first;
final = middle + 1;
for (j = first; j < last; j++) {
swaps++;
if ((start <= middle) && (final <= last))
{
compares++;
mas[j] = (A[start] <= A[final])
? A[start++] : A[final++];
}
else {
compares++;
mas[j] = (start <= middle)
? A[start++] : A[final++];
}
}
for (j = first; j < last; j++) {
swaps++;
A[j] = mas[j];
}
delete[] mas;
};
void MergeSort(int* A, int first, int last) {
if (first < last) {
MergeSort(A, first, (first + last) / 2);
MergeSort(A, (first + last) / 2 + 1, last);
Merge(A, first, last);
}
}
int main() {
int i;
int* A = new int[N];
srand(time(NULL));
for (i = 0; i < N; i++) {
A[i] = rand();
}
cout << endl;
double T, t1, t0;
t0 = Th();
MergeSort(A, 0, N);
t1 = Th();
T = (t1 - t0) / 1000.0;
cout << "Merge sort" << endl;
cout << "Number of elements:" << N << endl;
cout << "compares=" << compares << endl;
cout << "swaps=" << swaps << endl;
cout << "Time:" << T << endl;
delete[] A;
return 0;
}