#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
//判断次序↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
bool Is_sorted(int buffer[], int buffersize) {
for (int i = 0; i < buffersize-1; i++) {
if (buffer[i] > buffer[i + 1])return false;
}
return true;
}
//判断次序↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
//冒泡排序↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
void bubble_sort(int buffer[],int buffersize) {
int buf;
for (int i = 0; i < buffersize-1; i++) { //buffersize-1轮调整
for (int j = 0; j < buffersize - i - 1; j++) {
if (buffer[j] > buffer[j + 1]) {
buf = buffer[j];
buffer[j] = buffer[j + 1];
buffer[j + 1] = buf;
}
}
}
}
//冒泡排序↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
//选择排序↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
void selection_sort(int buffer[], int buffersize) {
int buf,Index;
int k;
for (int i = 0; i < buffersize - 1; i++) { //buffersize-1轮调整
buf = buffer[i];
Index = i;
for (int j = i+1; j < buffersize; j++) {
if (buf > buffer[j]) { //找出最小
buf = buffer[j];
Index = j;
}
}
k = buffer[i];
buffer[i] = buf;
buffer[Index] = k;
}
}
//选择排序↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
//快速排序↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
int quick_sort_partition(int buffer[], int LOW,int HIGH) {
int Index=LOW; //记录将被覆盖的值对应的下标
int left=LOW, right=HIGH; //左右指针
int pivot=buffer[LOW]; //记录基准值
while (left != right) { //当左右指针重合时,赋基准值给该下标
if (Index != right) {
if (buffer[right] >= pivot)right--; //如有指针对应的值大于等于pivot,右指针左移
else {
buffer[Index] = buffer[right]; //小于的话,值往左侧添加
left++; //左指针右移
Index = right; //变换Index
}
}
else {
if (buffer[left] <= pivot)left++; //如有指针对应的值小于等于pivot,左指针右移
else {
buffer[Index] = buffer[left]; //大于的话,值往右侧添加
right--; //右指针左移
Index = left; //变换Index
}
}
}
buffer[Index] = pivot;
return Index;
}
void quick_sort(int buffer[], int LOW,int HIGH) { //主要思想是不断进行分割排序,分治,迭代函数
if (LOW < HIGH) { //小的放左边,大的放右边,重复进行上述操作
int P= quick_sort_partition(buffer, LOW, HIGH); //进行一次排序,tip:只能在满足LOW<HIGH下使用,if中
quick_sort(buffer, P + 1, HIGH);
quick_sort(buffer, LOW, P - 1);
}
}
//快速排序↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
//归并排序↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
void merge_sort_partition(int buffer[], int begin1, int end1, int begin2, int end2, int* temp)
{
int index = begin1; //开始添加数值的下标,对应于temp
int i = begin1, j = begin2; //两小序列开始移动,移到末尾停止
while (i <= end1 && j <= end2) { //note:这里不应使用||,防止越位
if (buffer[i] <= buffer[j])
temp[index++] = buffer[i++];
else
temp[index++] = buffer[j++];
}
while (i <= end1) //与上面代码呼应
temp[index++] = buffer[i++];
while (j <= end2) //与上面代码呼应
temp[index++] = buffer[j++];
memcpy(buffer + begin1, temp + begin1, sizeof(int) * (end2 - begin1 + 1));//拷贝进入buffer
}
void merge_sort(int* buffer, int left, int right, int* temp)
{
if (left >= right)return;
assert(buffer); //断言函数
int mid = left + ((right - left) >> 1); //将数组分割
merge_sort(buffer, left, mid, temp);
merge_sort(buffer, mid + 1, right, temp);
merge_sort_partition(buffer, left, mid, mid + 1, right, temp);
}
//归并排序↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
//计数排序↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
void counting_sort(int buffer[], int buffersize){
int MIN=buffer[0], MAX=buffer[0];
int range;
int j=0;
for (int i = 0; i < buffersize; i++) { //求出该数组中的最大值与最小值
if (buffer[i] < MIN)MIN = buffer[i];
if (buffer[i] > MAX)MAX = buffer[i];
}
range = MAX - MIN + 1;
int* temp = (int*)malloc(sizeof(int) * range);//申请与之匹配的空间
if (temp == NULL) return; //未申请到空间返回
memset(temp, 0, sizeof(int)*range);
for (int i = 0; i < buffersize; i++) temp[buffer[i]-MIN]++; //对于buffer中出现的数++
for (int i = 0; i < range; i++) {
while (temp[i]--)buffer[j++] = i + MIN;
}
free(temp);
temp = NULL;
}
//计数排序↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
void main() {
int aa[6] = { 5,4,1,6,2,3 };
int* temp = (int*)malloc(sizeof(aa));
counting_sort(aa, 6);
for(int i=0;i<6;i++)
printf("%d\n", aa[i]);
if (Is_sorted(aa, 6))printf("YES");
}
五种简易排序(C语言)
最新推荐文章于 2022-09-18 14:31:55 发布