sort

  • クイックソート(昇順)
    a[]を基準にしてb[]も入れ替える
    l=0,r=a.length-1で始める
    void quickSort_ascend(int[] a, int[] b, int l, int r) {
    	if (l < r) {
    		int w = a[(l+r)/2];
    		int i = l, j = r, temp;
    		while (i < j) {
    			while (a[i] < w) i++;
    			while (a[j] > w) j--;
    			if (i <= j) {
    				temp = a[i];
    				a[i] = a[j]; a[j] = temp;
    				temp = b[i];
    				b[i] = b[j]; b[j] = temp;
    				i++; j--;
    			}
    		}
    		quickSort_ascend(a, b, l, j);
    		quickSort_ascend(a, b, i, r);
    	}
    }
  • クイックソート(降順)
    a[]を基準にしてb[]も入れ替える
    l=0,r=a.length-1で始める
    void quickSort_descend(int[] a, int[] b, int l, int r) {
    	if (l < r) {
    		int w = a[(l+r)/2];
    		int i = l, j = r, temp;
    		while (i < j) {
    			while (a[i] > w) i++;
    			while (a[j] < w) j--;
    			if (i <= j) {
    				temp = a[i];
    				a[i] = a[j]; a[j] = temp;
    				temp = b[i];
    				b[i] = b[j]; b[j] = temp;
    				i++; j--;
    			}
    		}
    		quickSort_descend(a, b, l, j);
    		quickSort_descend(a, b, i, r);
    	}
    }
最終更新:2013年10月04日 11:33