• 基本排序


    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);
        }
    }
    

      

  • 相关阅读:
    Office文档在线编辑的实现之二
    Office文档在线编辑的实现之一
    WebIM(5)----将WebIM嵌入到页面中
    WebIM(4)----Comet的特殊之处
    WebIM(3)----性能测试
    WebIM(2)---消息缓存
    WebIM(1)
    微信的通讯录首字母滑动的实现
    新浪微博认证新API调用
    Android小感悟-重写textview组件感悟
  • 原文地址:https://www.cnblogs.com/wqkant/p/9813704.html
Copyright © 2020-2023  润新知