赞
踩
#include <stdio.h> #include <stdlib.h> #include <time.h> #define MAXSIZE 10 void initArr(int arr[] , int length); void showArr(int arr[] , int length); void quickSort(int arr[] , int left , int right); int main() { srand((unsigned int)time(NULL)); int arr[MAXSIZE]; printf("==========排序前的序列=============\n"); initArr(arr,MAXSIZE); showArr(arr,MAXSIZE); printf("==========快速排序后的序列=============\n"); quickSort(arr,0,MAXSIZE-1); showArr(arr,MAXSIZE); system("pause"); return 0; } void initArr(int arr[] , int length) { for(int i = 0 ; i < length ; i++){ arr[i] = rand()%20; } } void showArr(int arr[] , int length) { for(int i = 0 ; i < length ; i++){ printf("%4d",arr[i]); } printf("\n"); } void quickSort(int arr[] , int left , int right) { //快速排序 if(left>right){ return; } int i = left; int j = right; int pivot = arr[i]; while(i<j){ while(i<j && arr[j] >= pivot){ j--; } arr[i] = arr[j]; while(i<j && arr[i] <= pivot){ i++; } arr[j] = arr[i]; } arr[i] = pivot; quickSort(arr,left,i-1); quickSort(arr,i+1,right); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。