1、冒泡排序
void bubble_sort(int* num, int num_count)
{
// 冒泡排序,最好情况O(N),最坏情况O(N*N)
for (int i = num_count - 1; i >= 0; i--)
{
bool ok_flag = true;
for (int j = 0; j < i; j++)
{
if (num[j] > num[j + 1])
{
ok_flag = false;
int temp = num[j];
num[j] = num[j + 1];
num[j + 1] = temp;
}
}
if (ok_flag) return;
}
}
2、插入排序
void insertion_sort(int* num, int num_count)
{
// 插入排序,最好情况O(N),最坏情况O(N*N)
for (int i = 1; i < num_count; i++)
{
int temp = num[i];
int j = i - 1;
for (; j > 0 && num[i] < num[j]; j--)
{
num[j + 1] = num[j];
}
num[j + 1] = temp;
}
}
3、堆排序
void build_heap(int* num, int num_count, int cur_father)
{
int max_son = cur_father * 2 + 1, right_son = cur_father * 2 + 2;
if (max_son >= num_count) return;
if (right_son < num_count && num[right_son] > num[max_son]) max_son = right_son;
if (num[max_son] > num[cur_father])
{
int temp = num[max_son];
num[max_son] = num[cur_father];
num[cur_father] = temp;
build_heap(num, num_count, max_son);
}
}
void heap_sort(int* num, int num_count)
{
// 堆排序
// 建立最大堆
int final_father = (num_count - 2) / 2;
for (int i = final_father; i >= 0; i--)
{
build_heap(num, num_count, i);
// 下方注释为不使用递归建堆的过程
//int cur_father = i;
//while (1)
//{
// int max_son = cur_father * 2 + 1, right_son = cur_father * 2 + 2;
// if (max_son >= num_count) break; // 没有左儿子,直接break
// if (right_son < num_count && num[right_son] > num[max_son]) max_son = right_son;
// if (num[max_son] > num[cur_father])
// {
// int temp = num[max_son];
// num[max_son] = num[cur_father];
// num[cur_father] = temp;
// cur_father = max_son;
// }
// else
// {
// // 没有交换,直接break
// break;
// }
//}
}
// 开始排序
for (int i = num_count - 1; i > 0 ; i--)
{
int temp = num[i];
num[i] = num[0];
num[0] = temp;
build_heap(num, i, 0);
}
}
4、归并排序
void merge(int* num, int left, int right)
{
int center = (right - left) / 2 + left;
if (left < right)
{
merge(num, left, center);
merge(num, center + 1, right);
int temp_count = right + 1 - left;
int* temp = new int[temp_count];
int temp_idx = 0, right_idx = center + 1, left_idx = left, temp_num = 0;
while (left_idx <= center && right_idx <= right)
{
if (num[left_idx] <= num[right_idx])
{
temp_num = num[left_idx];
left_idx++;
}
else
{
temp_num = num[right_idx];
right_idx++;
}
temp[temp_idx] = temp_num;
temp_idx++;
}
while (left_idx <= center)
{
temp[temp_idx] = num[left_idx];
temp_idx++;
left_idx++;
}
while (right_idx <= right)
{
temp[temp_idx] = num[right_idx];
temp_idx++;
right_idx++;
}
for (int i = 0; i < temp_count; i++)
{
num[left] = temp[i];
left++;
}
delete[]temp;
}
}
void merge_sort(int* num, int num_count)
{
merge(num, 0, num_count - 1);
}
5、快速排序
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
int get_pivot(int* num, int left, int right)
{
int mid = (right + left) / 2;
int max = num[mid];
if (num[left] > num[mid]) swap(num[left], num[mid]);
if (num[left] > num[right]) swap(num[left], num[right]);
if (num[mid] > num[right]) swap(num[mid], num[right]);
swap(num[mid], num[right - 1]);
return num[right - 1];
}
void quick_sort1(int* num, int left, int right)
{
if (left == right) return;
int pivot = get_pivot(num, left, right);
int i = left, j = right - 1;
for (;;)
{
while(num[i] < pivot)
{
i++;
}
while(num[j] > pivot)
{
j--;
}
if (i < j) swap(num[i], num[j]);
else
{
break;
}
}
swap(num[i], num[right - 1]);
quick_sort1(num, left, i - 1);
quick_sort1(num, i + 1, right);
}
void quick_sort(int* num, int num_count)
{
quick_sort1(num, 0, num_count - 1);
}