#include <stdio.h>
#include <stdlib.h>
void myswap(int array[], int k, int j)
{
int tmp = array[k];
array[k] = array[j];
array[j] = tmp;
}
//选择排序
void Selection_Sort(int array[], int len)// O(n*n)
{
int k = 0,i=0,j=0;
for (i = 0; i < len; i++)
{
for (j = i+1; j < len; j++)
{
if (array[i] > array[j])
{
k = j;
myswap(array, k, i);
}
}
}
}
//插入排序
void Insert_Sort(int array[], int len)// O(n*n)
{
int tmp = 0, i = 0, j = 0 ,k = 0;
for (i = 1; i < len; i++)
{
tmp = array[i];//拿出元素
for (j = i - 1; j >= 0 && tmp < array[j]; j--)//进行比较
{
array[j + 1] = array[j];//元素后移
k = j;
}
array[k] = tmp;
}
}
//冒泡排序
void Bubble_Sort(int array[], int len)// O(n*n)
{
int i = 0, j = 0;
int tag = 0;//tag为0表示没有排好序,1表示已经排好序
//for (i = 0; i <len; i++)//从后往前冒泡
for (i = len - 1; i >= 0 && (tag == 0); i--)//从前往后冒泡
{
tag = 1;
for (j = 1; j <= i; j++)
{
if (array[j] < array[j - 1])
{
myswap(array, j, j - 1);
tag = 0;
}
}
}
}
//快速排序
int Partition(int array[], int low, int high)
{
int pv = array[low];
while (low < high)
{
while (low<high&&array[high]>=pv)
{
high--;
}
myswap(array, low, high);
while (low < high&&array[low] <= pv)
{
low++;
}
myswap(array, low, high);
}
return low;
}
void Q_Sort(int array[], int low, int high)
{
if (low < high)
{
int pivot = Partition(array, low, high);
Q_Sort(array, low, pivot - 1);
Q_Sort(array, pivot + 1, high);
}
}
void Quick_Sort(int array[], int len)
{
Q_Sort(array, 0, len-1);
}
//希尔排序
void Shell_Sort(int array[], int len)
{
int i = 0, j = 0, k = 0;
int gap = len;
int tmp = 0;
do
{
gap = gap / 3 + 1;
for (i = gap; i < len; i+=gap)
{
tmp = array[i];
for (j = i - gap; j >= 0 && array[j] > tmp; j-=gap)
{
array[j+gap] = array[j];
k = j;
}
array[k] = tmp;
}
} while (gap > 1);
}
//归并算法
void Merge(int src[], int des[], int low, int mid, int high)
{
int i = low, j = mid + 1;
int k = low;
while (i <= mid&&j <= high)
{
if (src[i] < src[j])
{
des[k++] = src[i++];
}
else
{
des[k++] = src[j++];
}
}
while (i <= mid)
{
des[k++] = src[i++];
}
while (j <= high)
{
des[k++] = src[j++];
}
}
void M_Sort(int src[], int des[], int low, int high, int max)
{
if (low == high)
{
des[low] = src[low];
}
else
{
int mid = (low + high) / 2;
int *space = (int *)malloc(sizeof(int) * max);
if (space != NULL)
{
M_Sort(src, space, low, mid, max);
M_Sort(src, space, mid + 1, high, max);
Merge(space, des, low, mid, high);
}
free(space);
}
}
void Merge_Sort(int array[], int len)
{
M_Sort(array, array, 0, len - 1, len);
}
void main()
{
int a[50];
int len = 50;
for (int i = 0; i < len; i++)
{
a[i] = rand() % 100;
}
for (int i = 0; i < len; i++)
{
printf("%d ", a[i]);
}
//Selection_Sort(a, len);
//printf("\n");
//for (int i = 0; i < len; i++)
//{
// printf("%d ", a[i]);
//}
//Insert_Sort(a, len);
//printf("\n");
//for (int i = 0; i < len; i++)
//{
// printf("%d ", a[i]);
//}
//Bubble_Sort(a, len);
//printf("\n");
//for (int i = 0; i < len; i++)
//{
// printf("%d ", a[i]);
//}
//Quick_Sort(a, len);
//printf("\n");
//for (int i = 0; i < len; i++)
//{
// printf("%d ", a[i]);
//}
//Shell_Sort(a, len);
//printf("\n");
//for (int i = 0; i < len; i++)
//{
// printf("%d ", a[i]);
//}
Merge_Sort(a, len);
printf("\n");
for (int i = 0; i < len; i++)
{
printf("%d ", a[i]);
}
}
简单排序算法
最新推荐文章于 2022-11-17 14:48:01 发布