빠 른 정렬 실현 (c 언어)

1595 단어 빠 른 정렬
코드
 
//

//  main.c

//  cTest

//

//  Created by Zizhu on 13-10-8.

//  Copyright (c) 2013  Zizhu. All rights reserved.

//



#include <stdio.h>



#define N (10)



void quickSor(int *nums, int indexStart, int indexEnd);

int  partition(int *nums, int indexStart, int indexEnd);

void exchange(int *nums, int p1, int p2);

void printNums(int *nums, int count);



int main()

{

	printf(">>>strat
"); int nums[N] = {0, 4, 2, 5, 1, 3, -1, 19, 99, -100}; printNums(nums, N); int mid = partition(nums, 0, N-1); printNums(nums, N); printf("mid = %d
", mid); quickSor(nums, 0, N-1); printNums(nums, N); return 0; } void quickSor(int *nums, int indexStart, int indexEnd) { if (indexEnd > indexStart) { int indexMid = partition(nums, indexStart, indexEnd); quickSor(nums, indexStart, indexMid-1); quickSor(nums, indexMid+1, indexEnd); } } int partition(int *nums, int indexStart, int indexEnd) { int p2 = indexStart-1; int p1; int target = nums[indexEnd]; for (p1 = indexStart; p1 < indexEnd; p1++) { if (nums[p1] <= target) { p2++; exchange(nums, p1, p2); } } exchange(nums, p2+1, indexEnd); return p2+1; } void exchange(int *nums, int p1, int p2) { int tmp = nums[p1]; nums[p1] = nums[p2]; nums[p2] = tmp; } void printNums(int *nums, int count) { printf("numbers : "); int i; for (i = 0; i < count; ++i) { printf("%4d\t", nums[i]); } printf("
"); }

좋은 웹페이지 즐겨찾기