//冒泡排序
void BubbleSort(int a[], int n) {
//第一个参数:a[] 需要排序的数组
//第二个参数:n 数组的长度
int temp = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
//选择排序
void SelectSort(int a[], int n) {
//第一个参数:a[] 需要排序的数组
//第二个参数:n 数组的长度
int min,index,temp;
for (int i = 0; i < n; i++) {
min = a[0];
for (int j = i+1; j < n; j++) {
if (a[j] < min) {
min = a[j];
index = j;
}
}
temp = a[index];
a[index] = a[i];
a[i] = temp;
}
}
//插入排序
void InsertSort(int a[], int n) {
//第一个参数:a[] 需要排序的数组
//第二个参数:n 数组的长度
int preIndex, curData;
for (int i = 1; i < n; i++) { //从1开始,是因为当数组只有一个数的时候,数组一定是有序的;
preIndex = i - 1;
curData = a[i];
while (preIndex >= 0 && curData < a[preIndex]){
a[preIndex + 1] = a[preIndex];
preIndex--;
}
a[preIndex + 1] = curData;
}
}
//归并排序
//第一步两个有序序列的归并;
void merge(int a[], int fBegin, int fEnd, int sBegin, int sEnd,int newArray[]) {
int index = fBegin;
int f = fBegin;
int s = sBegin;
while (f <= fEnd && s <= sEnd) {
if (a[f] <= a[s]) {
newArray[index++] = a[f++];
}
else{
newArray[index++] = a[s++];
}
}
while (f <= fEnd) {
newArray[index++] = a[f++];
}
while (s <= sEnd) {
newArray[index++] = a[s++];
}
memcpy(a + fBegin, newArray + fBegin, sizeof(int)*(sEnd - fBegin + 1));
}
//第二步递归
void Merge(int a[], int left, int right, int array[]) {
if (left >= right) {
return;
}
int mid = (left + right) / 2;
Merge(a, left, mid, array);
Merge(a, mid + 1, right, array);
merge(a, left, mid, mid + 1, right, array);
}
//希尔排序
void ShellSort(int a[], int n) {
int temp;
int d, i, j;
for (d = n / 2; d >= 1; d = d / 2) {
for (i = d ; i < n; i++) {
if (a[i] < a[i - d]) {
temp = a[i];
for (j = i - d; j >= 0 && a[j] > temp; j -= d) {
a[j + d] = a[j];
}
a[j + d] = temp;
}
}
}
}
//快速排序
void QuickSort(int a[], int low,int high) {
//int low = 0, high = n - 1;
int Partition(int a[], int low, int high);
if (low < high) {
int pivotpos = Partition(a, low, high);
QuickSort(a, low, pivotpos-1);
QuickSort(a, pivotpos + 1, high);
}
}
//找到基准元素应该存放的位置;
int Partition(int a[], int low, int high) {
int pivot = a[low];
while (low < high) {
while (low < high && pivot < a[high]) {
--high;
}
a[low] = a[high];
while (low<high && pivot>a[low]) {
++low;
}
a[high] = a[low];
}
a[low] = pivot;
return low;
}
完整代码
#include
#include
#include
#define MaxSize 100
//冒泡排序
void BubbleSort(int a[], int n) {
//第一个参数:a[] 需要排序的数组
//第二个参数:n 数组的长度
int temp = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
//选择排序
void SelectSort(int a[], int n) {
//第一个参数:a[] 需要排序的数组
//第二个参数:n 数组的长度
int min,index,temp;
for (int i = 0; i < n; i++) {
min = a[0];
for (int j = i+1; j < n; j++) {
if (a[j] < min) {
min = a[j];
index = j;
}
}
temp = a[index];
a[index] = a[i];
a[i] = temp;
}
}
//插入排序
void InsertSort(int a[], int n) {
//第一个参数:a[] 需要排序的数组
//第二个参数:n 数组的长度
int preIndex, curData;
for (int i = 1; i < n; i++) { //从1开始,是因为当数组只有一个数的时候,数组一定是有序的;
preIndex = i - 1;
curData = a[i];
while (preIndex >= 0 && curData < a[preIndex]){
a[preIndex + 1] = a[preIndex];
preIndex--;
}
a[preIndex + 1] = curData;
}
}
//归并排序
//第一步两个有序序列的归并;
void merge(int a[], int fBegin, int fEnd, int sBegin, int sEnd,int newArray[]) {
int index = fBegin;
int f = fBegin;
int s = sBegin;
while (f <= fEnd && s <= sEnd) {
if (a[f] <= a[s]) {
newArray[index++] = a[f++];
}
else{
newArray[index++] = a[s++];
}
}
while (f <= fEnd) {
newArray[index++] = a[f++];
}
while (s <= sEnd) {
newArray[index++] = a[s++];
}
memcpy(a + fBegin, newArray + fBegin, sizeof(int)*(sEnd - fBegin + 1));
}
//第二步递归
void Merge(int a[], int left, int right, int array[]) {
if (left >= right) {
return;
}
int mid = (left + right) / 2;
Merge(a, left, mid, array);
Merge(a, mid + 1, right, array);
merge(a, left, mid, mid + 1, right, array);
}
//希尔排序
void ShellSort(int a[], int n) {
int temp;
int d, i, j;
for (d = n / 2; d >= 1; d = d / 2) {
for (i = d ; i < n; i++) {
if (a[i] < a[i - d]) {
temp = a[i];
for (j = i - d; j >= 0 && a[j] > temp; j -= d) {
a[j + d] = a[j];
}
a[j + d] = temp;
}
}
}
}
//快速排序
void QuickSort(int a[], int low,int high) {
//int low = 0, high = n - 1;
int Partition(int a[], int low, int high);
if (low < high) {
int pivotpos = Partition(a, low, high);
QuickSort(a, low, pivotpos-1);
QuickSort(a, pivotpos + 1, high);
}
}
//找到基准元素应该存放的位置;
int Partition(int a[], int low, int high) {
int pivot = a[low];
while (low < high) {
while (low < high && pivot < a[high]) {
--high;
}
a[low] = a[high];
while (low<high && pivot>a[low]) {
++low;
}
a[high] = a[low];
}
a[low] = pivot;
return low;
}
int main() {
int n; //用来记录输入数组的大小
//数组的输入工作;
printf("请输入数组的长度\n");
scanf_s("%d", &n);
int a[MaxSize]={0};
printf("请输出数组各元素的值\n");
for (int i = 0; i < n; i++) {
printf("第%d个数的值为:\n",i+1);
scanf_s("%d", &a[i]);
}
int array[MaxSize];
//排序
//BubbleSort(a, n); //冒泡排序;
//SelectSort(a, n); //插入排序;
//InsertSort(a, n); //插入排序;
//Merge(a, 0, n-1, array); //归并排序;
//ShellSort(a, n); //希尔排序
QuickSort(a, 0,n-1); //快速排序;
//输入打印
printf("排序后的结果为:\n");
for (int i = 0; i < n; i++) {
printf("%5d", a[i]);
}
return 0;
}