#include<iostream>
using namespace std;
#include<Windows.h>
#include<fstream>
#define times 10
#define Len 50000 //选择十万以内的正整数
void swap(int a[], int x, int y)
{
int temp = a[x];
a[x] = a[y];
a[y] = temp;
return;
}
void Direct_Insert_Sort(int a[])
{
for (int i = 2; i <= Len; i++)
{
a[0] = a[i];
int j = i;
for (; j > 1; j--)
{
if (a[0] < a[j - 1]) a[j] = a[j - 1];
else
break;
}
a[j] = a[0];
}
return;
}
void Shell_Insert_Sort(int a[],int d[],int t)
{
for (int k = 0; k < t; k++)
{
for (int i = d[k] + 1; i <= Len; i++)
{
a[0] = a[i];
int j = i;
for (; j > d[k]; j=j-d[k])
{
if (a[0] < a[j - d[k]]) a[j] = a[j - d[k]];
else
break;
}
a[j] = a[0];
}
}
return;
}
void Bubble_Sort(int a[])
{
for (int i = 1; i <= Len - 1; i++)
{
for (int j = 1; j <= Len - i; j++)
{
if (a[j] > a[j + 1])
swap(a, j, j + 1);
}
}
return;
}
void Quick_Sort(int a[],int l, int r)
{
if (l >= r) return;
int mid = l + r >> 1;
int i = l - 1, j = r + 1;
int x = a[mid];
while (i < j)
{
do i++; while (a[i] < x);
do j--; while (a[j] > x);
if (i < j) swap(a, i, j);
}
Quick_Sort(a, l, j);
Quick_Sort(a, j + 1, r);
return;
}
void Select_Sort(int a[])
{
for (int i = 1; i < Len; i++)
{
int min = a[i];
int k = i;
for (int j = i + 1; j <= Len; j++)
{
if (min > a[j])
{
min = a[j];
k = j;
}
}
swap(a, i, k);
}
return;
}
void Heap_Just(int a[], int l, int r)
{
a[0] = a[l];
int i = l, j = 2 * l;
while (j <= r)
{
if (j + 1 <= r&&a[j] < a[j + 1])j++;
if (a[0] > a[j]) break;
a[i] = a[j];
i = j;
j = 2 * i;
}
a[i] = a[0];
return;
}
void Heap_Sort(int a[])
{
for (int i = Len / 2; i > 0; i--)
{
Heap_Just(a, i, Len);
}
for (int i = Len; i > 1; i--)
{
swap(a, 1, i);
Heap_Just(a, 1, i - 1);
}
return;
}
void Merge_Sort(int a[], int l, int r)
{
if (l >= r) return;
int mid = l + r >> 1;
Merge_Sort(a, l, mid);
Merge_Sort(a, mid + 1, r);
int i = l, j = mid + 1;
int k = 0;
int *t = (int*)malloc((r - l + 4) * sizeof(int));
while (i <= mid && j <= r)
{
if (a[i] < a[j]) t[k++] = a[i++];
else
{
t[k++] = a[j++];
}
}
while (i <= mid) t[k++] = a[i++];
while (j <= r) t[k++] = a[j++];
for (i = l, j = 0; i <= r;)
a[i++] = t[j++];
return;
}
void Radix_Sort(int a[])
{
int count[10];//记录当前位置上0-9每个个数-》通过迭代得到每个不同数的结束位置
int *temp;
temp = (int*)malloc(Len * sizeof(int));
int radix = 1;
for (int k = 0; k < 5; k++)
{
for (int i = 0; i < 10; i++)
count[i] = 0;
for (int i = 0; i < Len; i++)
{
int m = (a[i] / radix) % 10;
count[m]++;
}
for (int i = 1; i < 10; i++)
count[i] = count[i - 1] + count[i];
for (int i = Len-1; i >= 0; i--)
{
int m = (a[i] / radix) % 10;
temp[count[m]-1] = a[i];
count[m]--;
}
for (int i = 0; i < Len; i++)
a[i] = temp[i];
radix = radix * 10;
}
return;
}
void Show(int a[])
{
for (int i = 1; i <= Len; i++)
{
cout << a[i] << " ";
if (i % 50 == 0) cout << endl;
}
return;
}
int main()
{
int *a = (int*)malloc((Len+4) * sizeof(int));
DWORD start_time = GetTickCount();
for (int k = 0; k < times; k++)
{
for (int i = 1; i <= Len; i++)
a[i] = rand() % 100000;
Direct_Insert_Sort(a);
}
DWORD end_time = GetTickCount();
cout << "Direct_Insert_Sort run time is:" << (end_time - start_time) << "ms!" << endl;
fstream fp;
fp.open("1.txt", ios::out);
if (fp.fail())
{
cout << "fail" << endl;
system("PAUSE");
}
for (int i = 1; i <= Len; i++)
{
fp << a[i] << " ";
if (i % 20 == 0)
fp << "\n";
}
fp.close();
//
start_time = GetTickCount();
for (int k = 0; k < times; k++)
{
int d[10] = { 97,73,59,47,41,31,23,11,3,1 };
for (int i = 1; i <= Len; i++)
a[i] = rand() % 100000;
Shell_Insert_Sort(a,d,10);
}
end_time = GetTickCount();
cout << "Shell_Insert_Sort run time is:" << (end_time - start_time) << "ms!" << endl;
fp.open("2.txt", ios::out);
if (fp.fail())
{
cout << "fail" << endl;
system("PAUSE");
}
for (int i = 1; i <= Len; i++)
{
fp << a[i] << " ";
if (i % 20 == 0)
fp << "\n";
}
fp.close();
//
for (int k = 0; k < 1; k++)
{
for (int i = 1; i <= Len; i++)
a[i] = rand() % 100000;
}
start_time = GetTickCount();
Bubble_Sort(a);
end_time = GetTickCount();
cout << "Bubble_Sort run time is:" << (end_time - start_time)*10 << "ms!" << endl;
fp.open("3.txt", ios::out);
if (fp.fail())
{
cout << "fail" << endl;
system("PAUSE");
}
for (int i = 1; i <= Len; i++)
{
fp << a[i] << " ";
if (i % 20 == 0)
fp << "\n";
}
fp.close();
//
start_time = GetTickCount();
for (int k = 0; k < times; k++)
{
for (int i = 1; i <= Len; i++)
a[i] = rand() % 100000;
/*fp.open("0.txt", ios::out);
if (fp.fail())
{
cout << "fail" << endl;
system("PAUSE");
}
for (int i = 1; i <= Len; i++)
{
fp << a[i] << " ";
if (i % 20 == 0)
fp << "\n";
}
fp.close();
*/
Quick_Sort(a,1,Len);
}
end_time = GetTickCount();
cout << "Quick_Sort run time is:" << (end_time - start_time) << "ms!" << endl;
fp.open("4.txt", ios::out);
if (fp.fail())
{
cout << "fail" << endl;
system("PAUSE");
}
for (int i = 1; i <= Len; i++)
{
fp << a[i] << " ";
if (i % 20 == 0)
fp << "\n";
}
fp.close();
//
start_time = GetTickCount();
for (int k = 0; k < times; k++)
{
for (int i = 1; i <= Len; i++)
a[i] = rand() % 100000;
Select_Sort(a);
}
end_time = GetTickCount();
cout << "Select_Sort run time is:" << (end_time - start_time) << "ms!" << endl;
fp.open("5.txt", ios::out);
if (fp.fail())
{
cout << "fail" << endl;
system("PAUSE");
}
for (int i = 1; i <= Len; i++)
{
fp << a[i] << " ";
if (i % 20 == 0)
fp << "\n";
}
fp.close();
//
start_time = GetTickCount();
for (int k = 0; k < times; k++)
{
for (int i = 1; i <= Len; i++)
a[i] = rand() % 100000;
Merge_Sort(a,1,Len);
}
end_time = GetTickCount();
cout << "Merge_Sort run time is:" << (end_time - start_time) << "ms!" << endl;
fp.open("6.txt", ios::out);
if (fp.fail())
{
cout << "fail" << endl;
system("PAUSE");
}
for (int i = 1; i <= Len; i++)
{
fp << a[i] << " ";
if (i % 20 == 0)
fp << "\n";
}
fp.close();
//
start_time = GetTickCount();
for (int k = 0; k < times; k++)
{
for (int i = 1; i <= Len; i++)
a[i] = rand() % 100000;
Heap_Sort(a);
}
end_time = GetTickCount();
cout << "Heap_Sort run time is:" << (end_time - start_time) << "ms!" << endl;
fp.open("7.txt", ios::out);
if (fp.fail())
{
cout << "fail" << endl;
system("PAUSE");
}
for (int i = 1; i <= Len; i++)
{
fp << a[i] << " ";
if (i % 20 == 0)
fp << "\n";
}
fp.close();
//
start_time = GetTickCount();
for (int k = 0; k < times; k++)
{
for (int i = 0; i < Len; i++)
a[i] = rand() % 100000;
Radix_Sort(a);
}
end_time = GetTickCount();
cout << "Radix_Sort run time is:" << (end_time - start_time) << "ms!" << endl;
fp.open("8.txt", ios::out);
if (fp.fail())
{
cout << "fail" << endl;
system("PAUSE");
}
for (int i = 1; i <= Len; i++)
{
fp << a[i] << " ";
if (i % 20 == 0)
fp << "\n";
}
fp.close();
//
system("PAUSE");
}
八大排序..
最新推荐文章于 2024-06-23 02:26:36 发布