博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基本排序
阅读量:5248 次
发布时间:2019-06-14

本文共 3120 字,大约阅读时间需要 10 分钟。

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

  

转载于:https://www.cnblogs.com/wqkant/p/9813704.html

你可能感兴趣的文章
ERP渠道文档详细和修改(二十五)
查看>>
C#正则Groups高级使用方法
查看>>
对copy、mutableCopy理解
查看>>
java基础--接口
查看>>
9.依赖(Dependence)
查看>>
Spring3.0 Could not find acceptable representation 解决方案
查看>>
rpm简单使用
查看>>
1 . SQL Server 基本知识
查看>>
Master配置文件
查看>>
Search in Rotated Sorted Array II
查看>>
精确时间计算方法
查看>>
内存不能为Read
查看>>
conda使用以前安装的python环境
查看>>
xampp集成环境安装时出现 windows找不到文件“-n”
查看>>
c++运算符重载---20
查看>>
c++ 关键字extern(声明)和定义的区别
查看>>
Struts2(四)Struts2配置文件的配置
查看>>
java 异常处理try+catch
查看>>
老司机带你探知存储伸缩之道,赶紧上车,来不及了!
查看>>
ecshop安装常见问题及解决办法
查看>>