1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | #include <QCoreApplication> #include <QDebug> #include <iostream> #include "kh_01.hpp" using namespace std; int a[100]; void swap(int* a , int* b){ const int temp = *a; *a = *b; *b = temp; } //반은 (스타트 + last ) / 2 void con_ai6(int *sus){ for(int i = 0 ; i < 10 ; i++){ cout << sus[i] <<" "; } cout << endl; } // 하나의 문제를 순서대로 분류하고 for if while 로 쪼개는 능력이 중요! //1.피벗인뎃스 설정. //2.피벗인덱스 부터 라스트 까지 피벗보다 큰값을 찾기. //3.피벗보다 큰 값을 찾았으면, //4.라스트에서부터 피벗보다 작은 값을 찾고. //5.두 값의 자리를 바꾼다. //6.그때 작은 값이 큰값보다 왼쪽에 있게 되면. //7.작은 값의 위치와 피벗을 바꾼다. //8.같은 배열을 스타트 부터 피벗 전까지와 피벗 이후부터 라스트까지 반복한다. //9.라스트와 스타트가 같아지면 종료. //int sus[10] = {10, 11, 9, 8 , 7 , 6 ,5, 4, 3, 2}; void quickSort(int* sus, const int start, const int last){ cout << "start : " << start << " last : " << last << endl;; if(start >= last ){ return; } const int pivotVal = sus[start]; int i = start + 1; int j = last ; while(true){ while(i <= last && sus[i] < pivotVal ) { i++; } while(j >= start && sus[j] > pivotVal ) { j--; } if(i < j){ swap(&sus[i], &sus[j]); con_ai6(sus); }else{ swap(&sus[start], &sus[j]); con_ai6(sus); break; } } quickSort(sus,start, j - 1); quickSort(sus, j + 1, last); } int main(int argc, char *argv[]) { int sus[10] = {10, 11, 9, 8 , 7 , 6 ,5, 4, 2, 3}; quickSort(sus, 0 , 9); cout << "end main " << endl; return 0; } | cs |
'Data Structure And Algorithm' 카테고리의 다른 글
Counting Sort 예제 (0) | 2019.03.31 |
---|---|
연결 리스트 (0) | 2019.03.30 |