直接插入排序:
#include<stdio.h>
#include<stdlib.h>
void insertSort(int *array, int n)
{
int i = 0, j = 0;
for (i = 1; i < n; i++)
{
int tmp = array[i];//tmp保存待排序的数
for (j = i - 1; j >= 0; j--)
{
if (tmp < array[j])
{
array[j + 1] = array[j];//大数后移
array[j] = tmp;
}
else
break;
}
}
}
int main()
{
int n = 0;
printf("直接插入排序:\n输入数组的大小:\n");
scanf_s("%d", &n);
int *array = (int*)malloc((n + 1) * sizeof(int));
int i = 0;
printf("依次输入这%d个数:\n", n);
for (i = 0; i < n; i++)
{
scanf_s("%d", &array[i]);
}
insertSort(array, n);
printf("排序结果:\n");
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
printf("\n");
}
改进版:
#include<stdio.h>
#include<stdlib.h>
//怎么感觉跟冒泡一样?
void insertSort(int *array, int low, int high)
{
int i = low;
while (i <= high) {
int j;
for (j = i; j >= low;j--)
{
if (array[j] < array[j - 1])
{
int tmp = array[j];
array[j] = array[j - 1];
array[j - 1] = tmp;
}
}
i++;
}
}
int main()
{
int i;
int n;
int *array;
printf("请输入数组的大小:");
scanf_s("%d", &n);
array = (int*)malloc(sizeof(int) * (n + 1));
printf("请输入数据(用空格分隔):");
for (i = 0; i < n; i++)
{
scanf_s("%d", &array[i]);
}
insertSort(array, 0, n - 1);
printf("排序后为:");
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
printf("\n");
}
冒泡排序:
#include<stdio.h>
#include<stdlib.h>
void bubbleSort(int *array, int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (array[j] > array[j + 1])
{
int tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
}
}
}
}
int main()
{
int n = 0;
printf("冒泡排序:\n输入数组的大小:\n");
scanf_s("%d", &n);
int *array = (int*)malloc((n + 1) * sizeof(int));
int i = 0;
printf("依次输入这%d个数:\n", n);
for (i = 0; i < n; i++)
{
scanf_s("%d", &array[i]);
}
bubbleSort(array, n);
printf("排序结果:\n");
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
printf("\n");
}
快排:
挖坑填数+分治法:
#include<stdio.h>
#include<stdlib.h>
void QuickSort(int *array, int low, int high)
{
if (low < high)
{
int L = low;
int R = high;
int pivotkey = array[low];
while (low < high && array[high] >= pivotkey)
{
high--;
}
array[low] = array[high];
while (low < high&&array[low] <= pivotkey)
{
low++;
}
array[high] = array[low];
array[low] = pivotkey;
QuickSort(array, L, low - 1);
QuickSort(array, low + 1, R);
}
}
int main()
{
int i;
int n;
int *array;
printf("请输入数组的大小:");
scanf_s("%d", &n);
array = (int*)malloc(sizeof(int) * (n + 1));
printf("请输入数据(用空格分隔):");
for (i = 0; i < n; i++)
{
scanf_s("%d", &array[i]);
}
QuickSort(array, 0, n - 1);
printf("排序后为:");
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
printf("\n");
}
Quick_Sort重写
单个 quickSort 函数版:
void quickSort(int arr[], int left, int right)
{
int i = left, j = right;
int tmp;
int pivot = (arr[(left + right) / 2]);
//partition
while (i <= j)
{
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j)
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
//recursion
if (left < j)
quickSort(arr, left, j);
if (i < right)
quickSort(arr, i, right);
}
partition + quicksort 函数版:
int partition(int arr[], int left, int right)
{
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
while (i <= j)
{
while (arr[i] < pivot)
i++;
while (pivot < arr[j])
j--;
if (i <= j)
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
return i;
}
void quickSort(int arr[], int left, int right)
{
int index = partition(arr, left, right);
if(left < index - 1)
quickSort(arr, left, index - 1);
if(index < right)
quickSort(arr, index, right);
}
选择排序:
- 简单选择排序:
#include<stdio.h>
#include<stdlib.h>
void selectSort(int *array, int low, int high)
{
int i = low;
while (i <= high)
{
int j;
for (j = i; j <= high; j++)
{
if (array[j] < array[i]) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
i++;
}
}
int main()
{
int i;
int n;
int *array;
printf("请输入数组的大小:");
scanf_s("%d", &n);
array = (int*)malloc(sizeof(int) * (n + 1));
printf("请输入数据(用空格分隔):");
for (i = 0; i < n; i++)
{
scanf_s("%d", &array[i]);
}
selectSort(array, 0, n - 1);
printf("排序后为:");
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
printf("\n");
}
- 堆排序:
//TODO
归并排序:
//Todo