Presentation is loading. Please wait.

Presentation is loading. Please wait.

선택 정렬 #define SWAP(x, y, t) {(t) = (x); (x) = (y); (y) = (t);}

Similar presentations


Presentation on theme: "선택 정렬 #define SWAP(x, y, t) {(t) = (x); (x) = (y); (y) = (t);}"— Presentation transcript:

1 선택 정렬 #define SWAP(x, y, t) {(t) = (x); (x) = (y); (y) = (t);}
void sort(int list[], int n) { int i, j, min, temp; for(i=0; i<n-1; i++) { min = i; for(j=i + 1; j<n; j++){ if(list[j] < list[min]) min = j; } SWAP(list[i], list[min], temp);

2 이진 탐색 (binary search) int compare(int x, int y) {
/* x와 y를 비교하여 적으면 -1, 같으면 0, 크면 1을 반환한다. */ if(x<y) return -1; else if (x==y) return 0; else return 1; } int binsearch(int list[], int searchnum, int left, int right) /* searchnum에 대해 list[0]<=···<=list[n-1]을 탐색. 찾으면 그 위치를 반환하고 못 찾으면 -1을 반환한다. */ int middle; while(left <= right) { middle = (left + right)/2; switch (compare(list[middle], searchnum)) { case -1: left = middle + 1; break; case 0 : return middle; case 1 : right = middle – 1; return -1;


Download ppt "선택 정렬 #define SWAP(x, y, t) {(t) = (x); (x) = (y); (y) = (t);}"

Similar presentations


Ads by Google