#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define count 30
void sort_quick_right(int arr[], int min, int max,int tmp, int left, int right);
void sort_quick_left(int arr[], int min, int max,int tmp, int left, int right);
void print(int arr[]) {
for (int i = 0; i < count; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
//冒泡排序
void sort_bubble( int arr[]) {
int tmp;
for (int i = 0; i < count; i++){
for (int j = count -1; j > i; j--) {
if (arr[j] < arr[j - 1]) {
tmp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = tmp;
}
}
}
}
//简单选择排序
void sort_choose_simple(int arr[]) {
int tmp;
for (int i =0; i < count -1; i++) {
int min = i;
for (int j = i+1; j < count; j++) {
if (arr[j]<arr[i])
min = j;
}
if (arr[i] != arr[min]) {
tmp = arr[min];
arr[min] = arr[i];
arr[i] = min;
}
}
}
//插入排序
void sort_insert(int arr[]) {
for (int i = 1; i < count ; i++) {
int j=i-1;
while (j>=0 && arr[j] > arr[i]) {
arr[j+1] = arr[j];
j--;
}
arr[++j] = arr[i];
}
}
void swap(int* a, int* b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
void max_heapify(int arr[], int super, int len) {
int sub;
while ((sub = 2 * super + 1) < len) {
if (sub + 1 < len && arr[sub] < arr[sub + 1])
sub++;
if (arr[super] >= arr[sub])
return;
swap(arr + super, arr + sub);
super = sub;
}
}
//堆排序
void sort_heap(int arr[]) {
//初始化
for (int super = count/ 2 - 1; super >= 0; super--)
max_heapify(arr, super, count);
//最大值换到后面,排除最大值重新排序,
for (int i = count; i > 0; i--) {
swap(arr, arr + i - 1);
max_heapify(arr,0,i-1);
}
}
//快速排序_从右到左
void sort_quick_right(int arr[],int min,int max,int tmp,int left,int right) {
//print(arr);
if (min >= max)return;
if (left == right) {
left--;
right++;
sort_quick_right(arr, min, left,arr[min], min, left);
sort_quick_right(arr, right, max, arr[right], right, max);
}else if (arr[right] >= tmp) {
sort_quick_right(arr,min,max, tmp,left, --right);
}else if (arr[right] < tmp) {
arr[left] = arr[right];
arr[right] = tmp;
sort_quick_left(arr, min, max,tmp, left, right);
}
}
//快速排序_从左到右
void sort_quick_left(int arr[], int min, int max,int tmp, int left, int right) {
//print(arr);
if (min >= max) return;
if (left == right) {
left--;
right++;
sort_quick_right(arr, min, left, arr[min], min, left);
sort_quick_right(arr, right, max, arr[right], right, max);
}
else if (arr[left] <= tmp) {
sort_quick_left(arr, min, max,tmp, ++left, right);
}
else if (arr[left] > tmp) {
arr[right] = arr[left];
arr[left] = tmp;
sort_quick_right(arr, min, max,tmp, left, right);
}
}
//快速排序
void sort_quick(int arr[], int min, int max, int tmp, int left, int right,int dest) {
if (min >= max) return;
if (left == right) {
left--;
right++;
sort_quick(arr, min, left, arr[min], min, left,1);
sort_quick(arr, right, max, arr[right], right, max,1);
}else if (dest == 1) {
if (arr[right] >= tmp)
sort_quick_right(arr, min, max, tmp, left, --right);
else {
arr[left] = arr[right];
arr[right] = tmp;
sort_quick_left(arr, min, max, tmp, left, right);
}
}else if (dest == -1) {
if (arr[left] <= tmp)
sort_quick(arr, min, max, tmp, ++left, right, -1);
else {
arr[right] = arr[left];
arr[left] = tmp;
sort_quick(arr, min, max, tmp, left, right, 1);
}
}
}
void sort() {
int arr[count];
srand((unsigned int)time);
for (int i = 0; i < count;i++)
arr[i] = rand()%100;
print(arr);
//sort_bubble(arr);
//sort_choose_simple(arr);
//sort_insert(arr);
sort_heap(arr);
//sort_quick(arr, 0, count - 1, arr[0], 0, count - 1, 1);
print(arr);
}
c语言-排序算法
最新推荐文章于 2024-08-20 20:01:22 发布