Algorithm 병합정렬 asdf18 2016. 10. 8. 20:10 #include<cstdio> #define SIZE 10 void merge(int arr[], int l, int m, int r) { int i=l, j=m+1, k=l; int tmp[SIZE]; while (i <= m&&j <= r) tmp[k++] = arr[i] < arr[j] ? arr[i++] : arr[j++]; while (i <= m) tmp[k++] = arr[i++]; while (j <= r) tmp[k++] = arr[j++]; for (k = l; k <= r; k++) arr[k] = tmp[k]; } void m_sort(int arr[], int l, int r) { if (l < r) { int m = (l + r) / 2; m_sort(arr, l, m); m_sort(arr, m + 1, r); merge(arr, l, m, r); } } int main() { int arr[SIZE] = { 10, 2, 3, 5, 1, 8, 6, 7, 9, 4 }; m_sort(arr, 0, 9); return 0; } 공유하기 게시글 관리 공부중