벡터가 아니라 배열을 받았을 때
소팅을 sort(a, a+n); 이런 식으로 하는게 모양이 이뻐서 한 번 구현해보려고
시도해봤는데 방향이 완전 잘못된 것 같습니다.
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 | // wip #include <iostream> using namespace std; //*/ // Make tree; // cmp의 반대로 넣어야 함에 유의할 것 template<class T, class Compare> void MakeHeap(T first, T last, Compare cmp) { T t; // swap 용 변수 // 범위 지정 방식 수정 필요 for (it = first; it != last; ++it) { // child = it; // 아이값 초기화 parent = (first + child - 1) / 2; // 부모값 초기화 while (child == first || cmp(*parent, *child) == *child) { // t = *parent; // swap(parent, child) *parent = *child; // *child = t; // child = parent; // 아이값 재설정 parent = (first + child) / 2; // 부모값 재설정 } } } // HeapSort(a, a + n, cmp); template<class T, class Compare> void HeapSort(T first, T last, Compare cmp) { MakeHeap(first, last, cmp); T t; for (it = first; it != last; ++it) { child = it; parent = (first + child - 1) / 2; while (child == first || cmp(*parent, *child) == *parent) { t = *parent; *parent = *child; *child = t; child = parent; parent = (first + child) / 2; } } } // HeapSort(a, a + n); // 기본형 : 오름차순 정렬 template<class T> void HeapSort(T first, T last) { HeapSort(first, last, less<>()); } int main() { int a[10] = { 3,14,15,92,65,35,89,79,32,38 }; HeapSort(a, a + 10); for (int i = 0; i < 10; ++i) { cout << a[i] << ' '; } return 0; } | cs |
방향을 어떻게 잡아야 할지 조언 좀 받고 싶습니다.
댓글 0