数据结构–排序 冒泡排序
#include<iostream>
using namespace std;
void main()
{
int size = 6;
int x[6] = { 5,8,56,24,43,4 };
int k;
int flag;
for (int m = 0; m < size - 1; m++)
{
flag = 1;
for (int n = 0; n < size - 1; n++)
{
if (x[n] > x[n + 1])
{
k = x[n];
x[n] = x[n + 1];
x[n + 1] = k;
flag = 0;
}
}
if (flag == 1)
break;
}
for (int i = 0; i < size; i++)
cout << x[i]<<" ";
}
插入排序
#include<iostream>
using namespace std;
void main()
{
int size = 6;
int x[6] = { 5,8,56,24,43,4 };
int k;
int m;
for (int i = 1; i < size; i++)
{
k = x[i];
for (m = i; m >= 0; m--)
{
if (x[m-1] > k)
{
x[m] = x[m-1];
}
else
break;
}
x[m] = k;
}
for (int i = 0; i < size; i++)
cout << x[i]<< " ";
}
选择排序
#include<iostream>
using namespace std;
void main()
{
int size = 6;
int x[6] = { 5,8,56,24,43,4 };
int k;
int min_x;
for (int i = 0; i < size; i++)
{
min_x = i;
for (int m = i; m < size; m++)
{
if (x[m] < x[min_x])
min_x = m;
}
k = x[i];
x[i] = x[min_x];
x[min_x] = k;
}
for (int i = 0; i < size; i++)
cout << x[i] << " ";
}
归并排序
#include<iostream>
using namespace std;
void combin(int* A,int a, int* B,int b)
{
int m = 0;
int n = 0;
int i = 0;
int* temp = new int[a + b];
while (m<a||n<b)
{
if ((m != a&&A[m] <= B[n])||(n==b))
{
temp[i] = A[m];
i++;
m++;
}
else
{
temp[i] = B[n];
i++;
n++;
}
}
for (i = 0; i < a + b; i++)
A[i] = temp[i];
delete temp;
}
void merger(int *data,int q,int r)
{
if (q >= r)
return;
else
{
int p = (q + r) / 2;
merger(data, q, p);
merger(data, p+1, r);
combin(data + q, p-q + 1, data + p+1, r-p);
}
}
void main()
{
int size_x = 1;
int x[1] = {1};
merger(x, 0, 0);
for (int i = 0; i < size_x; i++)
cout << x[i] << endl;
cout << "\n";
int size_y = 6;
int y[6] = { 5,8,56,24,43,4 };
merger(x, 0, 0);
for (int i = 0; i < size_y; i++)
cout << y[i] << " ";
}
快速排序
#include<iostream>
using namespace std;
int partition(int* A, int p, int r)
{
int pivort = A[r];
int i = p;
int item;
for (int j = p; j <= r - 1; j++)
{
if (A[j] < pivort)
{
item = A[j];
A[j] = A[i];
A[i] = item;
i++;
}
}
item = A[r];
A[r] = A[i];
A[i] = item;
return i;
}
void func(int* A, int p, int r)
{
if (p >= r)
return;
else
{
int q = partition(A, p, r);
func(A, p, q - 1);
func(A, q + 1, r);
}
}
void quick_sort(int *A,int n)
{
func(A, 0, n - 1);
}
void main()
{
int size_x = 1;
int x[1] = { 1 };
quick_sort(x, 1);
for (int i = 0; i < size_x; i++)
cout << x[i] << endl;
cout << "\n";
int size_y = 6;
int y[6] = { 5,8,56,24,43,4 };
quick_sort(y, 6);
for (int i = 0; i < size_y; i++)
cout << y[i] << " ";
}
快速排序(改进)
#include<iostream>
using namespace std;
int get_pivort(int* A, int p, int r)
{
int mid = (r + p) / 2;
if ((A[r] >= A[p] && A[r] <= A[mid]) || (A[r] >= A[mid] && A[r] <= A[p]))
return r;
else if ((A[p] >= A[r] && A[p] <= A[mid]) || (A[p] >= A[mid] && A[p] <= A[r]))
return p;
else
return mid;
}
int partition(int* A, int p, int r)
{
int a = get_pivort(A, p, r);
if (A[a] != A[r])
{
int item = A[r];
A[r] = A[a];
A[a] = item;
}
int pivort = A[r];
int i = p;
int item;
for (int j = p; j <= r - 1; j++)
{
if (A[j] < pivort)
{
item = A[j];
A[j] = A[i];
A[i] = item;
i++;
}
}
item = A[r];
A[r] = A[i];
A[i] = item;
return i;
}
void func(int* A, int p, int r)
{
if (p >= r)
return;
else
{
int q = partition(A, p, r);
func(A, p, q - 1);
func(A, q + 1, r);
}
}
void quick_sort(int* A, int n)
{
func(A, 0, n - 1);
}
void main()
{
int t1 = time(0);
int size_x = 1;
int x[1] = { 1 };
quick_sort(x, 1);
for (int i = 0; i < size_x; i++)
cout << x[i] << endl;
cout << "\n";
int size_y = 6;
int yyy[6] = { 5,8,56,24,43,4 };
int y[6] = { 0 };
for (int i = 0; i < 50000000; i++)
{
for (int m = 0; m < 6; m++)
y[m] = yyy[m];
quick_sort(y, 6);
}
cout << endl;
int t2 = time(0);
cout << t2 - t1;
}
总结:冒泡排序、插入排序、选择排序的时间复杂度都是O(n^2),且都是原地排序,适用于小规模数据的排序,不过比较来说还是插入排序性能更优,因为冒泡排序主要是交换操作,相比插入排序主要是比较和移动的操作,稍复杂一些。而选择排序虽然也都是比较的操作,但是它是不稳定的排序,排序前后相同数据的相对位置可能发生改变,而插入排序是稳定排序,不会改变相同数据的相对位置。归并排序和快速排序的平均时间复杂度都是O(nlogn),比前三种的复杂度低,适用于大规模数据的排序,其中归并排序是稳定排序,但不是原地排序,排序过程中需要额外的内存空间,快速排序不是稳定排序,但是是原地排序,不需要额外的内存空间。这两者相比较还是快速排序更优,因为归并排序的空间复杂度是O(n),其消耗的内存空间随着数据规模的增长而增长,当数据规模过于庞大时,内存消耗就会很大,所以虽然快速排序不是稳定排序,但它的应用跟广泛。
|