define SWAPx y ttx xy yt void selectionsortint





![선택 정렬 함수 #define SWAP(x, y, t)((t)=(x), (x)=(y), (y)=(t)) void selection_sort(int list[], int n) 선택 정렬 함수 #define SWAP(x, y, t)((t)=(x), (x)=(y), (y)=(t)) void selection_sort(int list[], int n)](https://slidetodoc.com/presentation_image_h/81a13dbcd42ccf5002d4a079bad44a17/image-6.jpg)







![버블정렬 C코드 #define SWAP(x, y, t) ( (t)=(x), (x)=(y), (y)=(t) ) void bubble_sort(int list[], 버블정렬 C코드 #define SWAP(x, y, t) ( (t)=(x), (x)=(y), (y)=(t) ) void bubble_sort(int list[],](https://slidetodoc.com/presentation_image_h/81a13dbcd42ccf5002d4a079bad44a17/image-14.jpg)








![합병 알고리즘 merge(list, left, mid, last): // 2개의 인접한 배열 list[left. . mid]와 list[mid+1. 합병 알고리즘 merge(list, left, mid, last): // 2개의 인접한 배열 list[left. . mid]와 list[mid+1.](https://slidetodoc.com/presentation_image_h/81a13dbcd42ccf5002d4a079bad44a17/image-23.jpg)



![Heap. Sort n program ADJUST n T(n)= O(log n) void ADJUST(element A[], int root, Heap. Sort n program ADJUST n T(n)= O(log n) void ADJUST(element A[], int root,](https://slidetodoc.com/presentation_image_h/81a13dbcd42ccf5002d4a079bad44a17/image-27.jpg)

![Heap. Sort n program Heap. Sort void Heap. Sort(element A[], int s, int e) Heap. Sort n program Heap. Sort void Heap. Sort(element A[], int s, int e)](https://slidetodoc.com/presentation_image_h/81a13dbcd42ccf5002d4a079bad44a17/image-29.jpg)

![Heap. Sort n program Make_heap void Make_heap(element A[], int e) { int i; for Heap. Sort n program Make_heap void Make_heap(element A[], int e) { int i; for](https://slidetodoc.com/presentation_image_h/81a13dbcd42ccf5002d4a079bad44a17/image-31.jpg)



![partition 함수 구현 int partition(int list[], int left, int right) { int pivot, temp; partition 함수 구현 int partition(int list[], int left, int right) { int pivot, temp;](https://slidetodoc.com/presentation_image_h/81a13dbcd42ccf5002d4a079bad44a17/image-35.jpg)







- Slides: 42
선택 정렬 함수 #define SWAP(x, y, t)((t)=(x), (x)=(y), (y)=(t)) void selection_sort(int list[], int n) { int i , j, least, temp; for(i=0, i<n-1; i++){ least=I; for(j=i+1; j<n ; j++) if(list[j]<list[least]) least=j; SWAP(list[i], list[least], temp); } } C로 쉽게 풀어쓴 자료구조 © 생능출판사 2005
버블정렬 C코드 #define SWAP(x, y, t) ( (t)=(x), (x)=(y), (y)=(t) ) void bubble_sort(int list[], int n) { int i, j, temp; for(i=n-1; i>0; i--){ for(j=0; j<i; j++) /* 앞뒤의 레코드를 비교한 후 교체 */ if(list[j]>list[j+1]) SWAP(list[j], list[j+1], temp); } } C로 쉽게 풀어쓴 자료구조 © 생능출판사 2005
쉘정렬 프로그램 // gap 만큼 떨어진 요소들을 삽입 정렬 // 정렬의 범위는 first에서 last inc_insertion_sort(int list[], int first, int last, int gap) { int i, j, key; for(i=first+gap; i<=last; i=i+gap){ key = list[i]; for(j=i-gap; j>=first && key<list[j]; j=j-gap) list[j+gap]=list[j]; list[j+gap]=key; } } // void shell_sort( int list[], int n ) // n = size { int i, gap; for( gap=n/2; gap>0; gap = gap/2 ) { if( (gap%2) == 0 ) gap++; for(i=0; i<gap; i++) // 부분 리스트의 개수는 gap inc_insertion_sort(list, i, n-1, gap); } } C로 쉽게 풀어쓴 자료구조 © 생능출판사 2005
합병 알고리즘 merge(list, left, mid, last): // 2개의 인접한 배열 list[left. . mid]와 list[mid+1. . right]를 합병 b 1←left; e 1←mid; b 2←mid+1; e 2←right; sorted 배열을 생성; index← 0; while b 1≤e 1 and b 2≤e 2 do if(list[b 1]<list[b 2]) then sorted[index]←list[b 1]; b 1++; index++; else sorted[index]←list[b 2]; b 2++; index++; 요소가 남아있는 부분배열을 sorted로 복사한다; sorted를 list로 복사한다; C로 쉽게 풀어쓴 자료구조 © 생능출판사 2005
Heap. Sort n program ADJUST n T(n)= O(log n) void ADJUST(element A[], int root, int end) { //root와 end는 각각 시작 index와 끝 index int child; child= 2*root+1; //A[0]부터 while(child <= end){ if(child+1 <= end&& A[child] < A[child+1]) child++; if(A[root] >= A[child]) break; swap(A, child, root); root= child; child= 2*root+1; } } C로 쉽게 풀어쓴 자료구조 © 생능출판사 2005
Heap Sort n n n개의 데이타에 대하여 Max Heap Tree를 구성: Make Heap n번 delete. PQ를 함 99 61 77 76 1 59 48 19 77 26 61 76 1 59 48 77 99 19 61 26 99 1 77 77 C로 쉽게 풀어쓴 자료구조 26 48 19 48 26 19 61 26 99 1 77 59 48 19 19 48 76 99 © 생능출판사 2005 1 77 26 99 76 99 59 19 59 26 99 59 61 1 59 48 26 59 76 76 48 76 19 1 61 77 48 59 61 1 76 77 26 99 19 61 76
Heap. Sort n program Heap. Sort void Heap. Sort(element A[], int s, int e) { Make_heap(A, e); for( i=e; i>=s; i--){ swap(A, s, i); ADJUST(A, s, i-1); } } C로 쉽게 풀어쓴 자료구조 © 생능출판사 2005
Heap. Sort n Make Heap n n Unsorted list로 부터 하나의 Heap Tree 생성 n/2번의 연속적인 ADJUST를 이용하여 Heap Tree 생성 19 1 26 48 76 99 59 77 61 19 1 48 77 19 26 76 99 1 59 77 61 48 19 77 61 48 48 76 26 59 61 Adjust 59 19 1 77 Adjust 48 99 76 26 61 59 26 19 1 99 C로 쉽게 풀어쓴 자료구조 99 99 76 99 61 76 1 77 26 Adjust 77 59 © 생능출판사 2005 61 76 26 19 48 1 59
Heap. Sort n program Make_heap void Make_heap(element A[], int e) { int i; for ( i=(e-1)/2; i>= 1; i--) ADJUST(A, i, e); } n Heap. Sort의 특징 n n 항상 O(nlogn)의 시간 복잡도를 보장한다. Memory의 사용이 n이다. C로 쉽게 풀어쓴 자료구조 © 생능출판사 2005
partition 함수 구현 int partition(int list[], int left, int right) { int pivot, temp; int low, high; low = left; high = right+1; pivot = list[left]; do { do low++; while(low<=right &&list[low]<pivot); do high--; while(high>=left && list[high]>pivot); if(low<high) SWAP(list[low], list[high], temp); } while(low<high); SWAP(list[left], list[high], temp); return high; } C로 쉽게 풀어쓴 자료구조 © 생능출판사 2005