c语言快速排序的实现
以前写的一些老代码
留着没事可以看看
1 // 2 // main.c 3 // cTest 4 // 5 // Created by on 13-10-8. 6 // Copyright (c) 2013年. All rights reserved. 7 // 8 9 #include <stdio.h> 10 11 #define N (10) 12 13 void quickSor(int *nums, int indexStart, int indexEnd); 14 int partition(int *nums, int indexStart, int indexEnd); 15 void exchange(int *nums, int p1, int p2); 16 void printNums(int *nums, int count); 17 18 int main() 19 { 20 printf(">>>strat\n"); 21 int nums[N] = {0, 4, 2, 5, 1, 3, -1, 19, 99, -100}; 22 printNums(nums, N); 23 int mid = partition(nums, 0, N-1); 24 printNums(nums, N); 25 printf("mid = %d\n", mid); 26 quickSor(nums, 0, N-1); 27 printNums(nums, N); 28 return 0; 29 } 30 31 void quickSor(int *nums, int indexStart, int indexEnd) 32 { 33 if (indexEnd > indexStart) 34 { 35 int indexMid = partition(nums, indexStart, indexEnd); 36 quickSor(nums, indexStart, indexMid-1); 37 quickSor(nums, indexMid+1, indexEnd); 38 } 39 40 } 41 42 int partition(int *nums, int indexStart, int indexEnd) 43 { 44 45 int p2 = indexStart-1; 46 int p1; 47 48 int target = nums[indexEnd]; 49 50 for (p1 = indexStart; p1 < indexEnd; p1++) 51 { 52 if (nums[p1] <= target) 53 { 54 p2++; 55 exchange(nums, p1, p2); 56 57 } 58 } 59 exchange(nums, p2+1, indexEnd); 60 return p2+1; 61 } 62 63 void exchange(int *nums, int p1, int p2) 64 { 65 int tmp = nums[p1]; 66 nums[p1] = nums[p2]; 67 nums[p2] = tmp; 68 } 69 70 void printNums(int *nums, int count) 71 { 72 printf("numbers : "); 73 int i; 74 for (i = 0; i < count; ++i) 75 { 76 printf("%4d\t", nums[i]); 77 } 78 printf("\n"); 79 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。