import org.junit.After;import org.junit.Before;import org.junit.Test;import scala.actors.threadpool.Arrays;import static org.junit.Assert.assertTrue;public class Sort { int[] nums = new int[10000]; public int[] bubbleSort(int[] nums) { for (int i = 0; i < nums.length; i++) { for (int j = i + 1; j < nums.length; j++) { if (nums[i] > nums[j]) { int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; } } } return nums; } public int[] insertSort(int[] nums) { for (int i = 1; i < nums.length; i++) { int tmp = nums[i]; int insertPos = i; for (int j = i - 1; j >= 0; j--) { if (nums[j] > tmp) { nums[j + 1] = nums[j]; insertPos = j; } } nums[insertPos] = tmp; } return nums; } public int[] quickSort(int[] nums, int left, int right) { if (left < right) { int p = getPartition(nums, left, right); quickSort(nums, left, p - 1); quickSort(nums, p + 1, right); } return nums; } private int getPartition(int[] nums, int low, int hi) { int pivot = nums[low]; while (low < hi) { while (hi > low && nums[hi] >= pivot) { --hi; } nums[low] = nums[hi]; while (low < hi && nums[low] < pivot) { ++low; } nums[hi] = nums[low]; } nums[low] = pivot; return low; } public int[] mergeSort(int[] nums, int low, int hi) { if (low < hi) { int mid = (low + hi) / 2; mergeSort(nums, low, mid); mergeSort(nums, mid + 1, hi); merge(nums, low, mid, hi); } return nums; } private void merge(int[] nums, int low, int mid, int hi) { int[] tmpNums = new int[hi - low + 1]; int pos = 0; for (int i = low, j = mid + 1; i <= mid || j <= hi; ) { if (i > mid) { tmpNums[pos++] = nums[j++]; } else if (j > hi) { tmpNums[pos++] = nums[i++]; } else if (nums[i] < nums[j]) { tmpNums[pos++] = nums[i++]; } else { tmpNums[pos++] = nums[j++]; } } for (int i = 0; i < hi - low + 1; i++) { nums[low + i] = tmpNums[i]; } tmpNums = null; } @Before public void init() { for (int i = 0; i < nums.length; i++) { nums[i] = (int) (Math.random() * 10000); } } @After public void check() { System.out.println(Arrays.toString(nums)); int pre = Integer.MIN_VALUE; for (int num : nums) { assertTrue(pre <= num); pre = num; } } @Test public void test() { //nums = bubbleSort(nums); //nums = quickSort(nums, 0, nums.length - 1); mergeSort(nums, 0, nums.length - 1); }}