[[TableOfContents]] = 계획 = * Priority_Queue * Heap * Heap 구현 * STL에서 Priority_Queue 사용 * Heap Sort = 참여자 = ||강사 || [권영기] || ||<|10> 참여자|| [권준혁] || || [이지수] || || [김정민] || || [유재범] || || [성훈] || || [] || || [] || = 내용 = == Priority Queue == * In computer science/data structures, a priority queue is an abstract data type which is like a regular queue or stack data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to their order in the queue. * While priority queues are often implemented with heaps, they are conceptually distinct from heaps. A priority queue is an abstract concept like "a list" or "a map"; just as a list can be implemented with a linked list or an array, a priority queue can be implemented with a heap or a variety of other methods such as an unordered array. * 출처 : [http://en.wikipedia.org/wiki/Priority_queue] == Priority Queue in STL == {{{ #include #include #include #include using namespace std; priority_queue pq; int v[100]; int main(void) { int n = 10; srand(time(NULL)); printf("Vector \n"); for(int i = 0; i // min이 가장 위에 가도록! int heap[32]; //동적할당, 포인터로 바꿔보자 ㅠㅠ //실제 data가 들어가는 index는 0부터가 아니라 1부터임! int push(int num){ int i; for (i = 1; i < 32; i++){ if (heap[i] == -1){ //이거 때문에 일단 양의 정수만 넣을수 있음 (count를 쓰는 방법도 있지만 이걸로 해봄) heap[i] = num; break; } if (i == 31){ printf("the heap is full\n"); return 1; } } while(heap[i] < heap[i / 2]){ int temp_for_change; temp_for_change = heap[i]; heap[i] = heap[i / 2]; heap[i / 2] = temp_for_change; i = i / 2; } } int pop(){ int last_num_index; //숫자가 있는 마지막 칸의 index int j=1, small_num_index; //for 'while' for (int i = 1; i < 32; i++){ if (heap[i] == -1){ if (i == 1){ printf("heap is empty\n"); return 1; } last_num_index = i - 1; break; } if (i == 31){ last_num_index = 31; } } heap[1] = heap[last_num_index]; heap[last_num_index] = -1; while (1){ if ((heap[j] > heap[j * 2] && heap[j * 2] != -1) || (heap[j] > heap[j * 2 + 1] && heap[j*2+1] != -1)){ //children 중에 parent보다 작은놈이 있다! int temp_for_change; if (heap[j * 2] <= heap[j * 2 + 1] || heap[j*2+1] == -1){ //왼쪽을 끌어올린다 small_num_index = j * 2; } else{ //오른쪽을 끌어올린다 small_num_index = j * 2 + 1; } temp_for_change = heap[small_num_index]; heap[small_num_index] = heap[j]; heap[j] = temp_for_change; j = small_num_index; } else{ return 0; } } } void view_top(){ printf("top : %d\n", heap[1]); } /* void view_all(){ //just for debug for (int i = 1; i < 32; i++){ printf("%d ", heap[i]); } printf("\n"); } */ void main(){ int temp; int num; for (int i = 0; i < 32; i++){ heap[i] = -1; } while (1){ printf("push : 1, pop : 2\tinput : "); scanf_s("%d", &temp); switch (temp){ case 1: printf("num : "); scanf_s("%d", &num); push(num); view_all(); break; case 2: view_top(); pop(); view_all(); break; default: break; } if (temp != 1 && temp != 2) break; } } }}} ---- [새싹교실/2014], [새싹교실/2014/다빈치인재반]