대충 quicksort를 vc++ 9.0으로 만들어보았다.
다들 간단히 구현하는 것인데.. 왜 난 그 개고생을 했는지. -_-;;
01 | #include "stdafx.h" |
02 | #include <iostream> |
03 | int partition( int * int_array, int startIndex, int endIndex){ |
04 | int x=int_array[endIndex]; |
05 |
06 | int i=startIndex-1; |
07 | int j; |
08 | for (j=startIndex; j < endIndex; j++){ |
09 | if (int_array[j] <= x){ |
10 | i++; |
11 | int temp=int_array[i]; |
12 | int_array[i]=int_array[j]; |
13 | int_array[j]=temp; |
14 | } |
15 | } |
16 | int temp2; |
17 |
18 | temp2=int_array[i+1]; |
19 | int_array[i+1]=int_array[endIndex]; |
20 | int_array[endIndex]=temp2; |
21 |
22 | return i+1; |
23 | } |
24 |
25 | void quicksort( int * int_array, int startIndex, int endIndex){ |
26 | if (startIndex < endIndex){ |
27 | int pivotIndex=partition(int_array, startIndex, endIndex); |
28 | quicksort(int_array, startIndex, pivotIndex-1); |
29 | quicksort(int_array, pivotIndex+1, endIndex); |
30 | } |
31 | } |
32 |
33 | int _tmain( int argc, _TCHAR* argv[]) |
34 | { |
35 | int test_array[]={ 26, 27, 6, 21, 72, 9, 32, 74, 78, 38, 12, 34, 89 }; |
36 |
37 | quicksort(test_array, 0, 12); |
38 |
39 | for ( int i=0; i < 13; i++) |
40 | { |
41 | std::cout << test_array[i] << " " ; |
42 | } |
43 |
44 | std::cout << std::endl; |
45 | return 0; |
46 | } |