# 練習問題の解答例 3日目 ## マージソートの例 public static void main(String[] args) { int n[] = {23, 5, 69, 87, 4, 2, 62, 1}; for(int i=0;i1){ int n1 = a.length/2; int n2 = a.length - n1; int[] a1 = new int[n1]; int[] a2 = new int[n2]; for(int i=0; i ## クイックソートの例 public static void quickSort(int[] a, int l, int r){ if(l>=r){ return; } int base = a[l]; int left=l; int right=r; while(true){ if(left>right){ break; } while(true){ if(a[left]>=base){ break; } left++; } while(true){ if(a[right]<=base){ break; } right--; } int temp=a[left]; a[left]=a[right]; a[right]=temp; left++; right--; } quickSort(a, l, right); quickSort(a, left, r); } public static void main(String[] args) { int n[] = {23, 5, 69, 87, 4, 2, 62, 1}; quickSort(n, 0, n.length - 1); }