#include <stdio.h> void swap(int *, int *); int Partition(int A[], int low, int high); //冒泡排序 void BubbleSort(int A[], int n) { for (int i = 0; i < n - 1; ++i) { int flag = 0; for (int j = 0; j < n - i - 1; ++j) { if (A[j] > A[j + 1]) { swap(&A[j], &A[j + 1]); flag++; } } if (flag == 0) { break; } } } void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; } void QuickSort(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&&A[high]>=pivot) high--; A[low]=A[high]; while (low<high&&A[low]<pivot) low++; A[high]=A[low]; } A[low]=pivot; return low; } int main(void) { int A[] = {49, 38, 65, 97, 76, 13, 27, 49}; BubbleSort(A, 8); printf("冒泡排序"); for (int i = 0; i < sizeof(A) / sizeof(A[0]); ++i) { printf("%d ", A[i]); } printf("\n"); int B[] = {49, 38, 65, 97, 76, 13, 27, 49}; QuickSort(B,0,7); printf("快速排序"); for (int i = 0; i < sizeof(B) / sizeof(B[0]); ++i){ printf("%d ",B[i]); } return 0; }
c语言数据结构-------------交换排序
最新推荐文章于 2025-04-27 23:29:59 发布