交换排序
本帖最后由 pwnmelife 于 2019-1-19 20:48 编辑#include <stdio.h>
#include <stdlib.h>
void Bubble_Sort_1(int k[], int n);
void Bubble_Sort_2(int k[], int n);
void Bubble_Sort_3(int k[], int n);
void QuickSort(int k[], int low, int high);
int Partition(int k[], int low, int high);
int main()
{
int k[] = { 50,26,38,80,70,90,8,30,40,20 };
Bubble_Sort_3(k, 10);
printf("The sorted result: \n");
for (int i = 0; i < 10; i++) {
printf("%d ", k);
}
printf("\n\n");
}
void Bubble_Sort_1(int k[], int n) {
int temp;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (k > k) {
temp = k;
k = k;
k = temp;
}
}
}
}
void Bubble_Sort_2(int k[], int n) {
int temp;
for (int i = 0; i < n; i++) {
for (int j = n-1; j>i; j--) {
if (k < k) {
temp = k;
k = k;
k = temp;
}
}
}
}
void Bubble_Sort_3(int k[], int n) {
int temp;
bool flag = true;
for (int i = 0; i < n && flag; i++) {
for (int j = n - 1; j > i; j--) {
flag = 0;
if (k < k) {
flag = 1;
temp = k;
k = k;
k = temp;
}
}
}
}
void QuickSort(int k[], int low, int high) {
if (low < high) {
int pivotpos = Partition(k, low, high);
QuickSort(k, low, pivotpos - 1);
QuickSort(k, pivotpos + 1, high);
}
}
int Partition(int k[], int low, int high) {
int pivot = k;
while (low < high) {
while (low < high && k >= pivot) --high;
k = k;
while (low < high && k <= pivot) ++low;
k = k;
}
k = pivot;
return low;
}
页:
[1]