演算法導論示例 MergeSort

2021-04-07 08:55:33 字數 844 閱讀 5416

/**

* introduction to algorithms, second edition

* 2.3 mergesort

* @author 土豆爸爸

* */

public class mergesort

}

public static void merge(int array, int p, int q, int r)

l[i] = integer.max_value;

for (j = 0; j < n2; j++)

r[j] = integer.max_value;

i = 0;

j = 0;

for (int k = p; k <= r; k++) else

}

}

}import junit.framework.testcase;

public class mergesorttest extends testcase ;

mergesort.sort(array, 0 , array.length -1);

asserttrue(verifyordered(array));

}

public void testrandomarray()

mergesort.sort(array, 0 , array.length -1);

asserttrue(verifyordered(array));

}

private boolean verifyordered(int array)

}

return true;

}

}

演算法導論示例 InsertSort

introduction to algorithms,second edition 2.1 insertsort author 土豆爸爸 public class insertsort array i 1 key import junit.framework.testcase public clas...

演算法導論示例 PermuteBySorting

introduction to algorithms,second edition 5.3 permute by sorting author 土豆爸爸 public class permutebysorting 根據隨機陣列對目標資料進行排序 sort array,p 根據p對array重排 pa...

演算法導論示例 HeapSort

introduction to algorithms,second edition 6.4 heapsort author 土豆爸爸 public class heapsort 構建max heap private void buildmaxheap 使陣列的第i個元素按max heap規則重排 p...

演算法導論示例 QuickSort

introduction to algorithms,second edition 7.1 quicksort author 土豆爸爸 public class quicksort 找到一個位置q,使q左邊的元素都比q小,q右邊的元素都比q大 param array 待排序陣列 param p 開始...

演算法導論示例 Queue

introduction to algorithms,second edition 10.1 queues author 土豆爸爸 public class queue 將元素x新增到佇列。佇列的尾索引加1。param x 待新增的元素 public void enqueue int x array...