java 希尔排序 leetcode_leetcode-數組篇

importjava.util.ArrayList;importjava.util.List;/*** 俩个排序算法

**/

public classLc26 {public static int removeDuplicates(int[] nums) {int count = 0;for (int i = 0; i < nums.length - 1; i++) {if (nums[i] == nums[i + 1]) {

nums[i]=Integer.MAX_VALUE;

count++;

}

}//nums = insertSort(nums);//nums = shellSort(nums);//nums = selectSort(nums);//nums = bubbleSort(nums);//quickSort(nums, 0, nums.length - 1);

nums =radixSort(nums);return nums.length -count;

}/*** 插入排序,遍历所有的元素和以前排好的元素,如果选择的元素比以前的元素小就替换。*/

public static int[] insertSort(int[] nums) {for (int i = 1; i < nums.length; i++) {for (int j = 0; j < i; j++) {if (nums[i]

nums[i]=nums[j];

nums[j]=temp;break;

}

}

}returnnums;

}/*** 插入排序,遍历所有的元素和以前排好的元素,如果选择的元素比以前的元素小就替换。 希尔排序:在插入排序的基础上增加控制增量,逻辑分组数据,每次比较俩端数据*/

public static int[] shellSort(int[] nums) {for (int gap = nums.length / 2; gap > 0; gap /= 2) {for (int i = gap; i < nums.length; i++) {for (int j = i; j >= gap; j -=gap) {if (nums[j] < nums[j -gap]) {int temp =nums[j];

nums[j]= nums[j -gap];

nums[j- gap] =temp;

}

}

}

}returnnums;

}/*** 选择排序:再要排序中的数组中,选出最小的数字和第一个数字替换,一次选出第二小的数组和第二个数字替换,一次类推*/

public static int[] selectSort(int[] nums) {for (int i = 0; i < nums.length; i++) {int index =nums[i];for (int j = i + 1; j < nums.length; j++) {if (nums[j]

nums[j]=nums[i];

nums[i]=temp;

}

}

}returnnums;

}/** 冒泡排序:对当前还未排好顺序的元素进行自顶向下排序,交换相邻的俩个数字,小数上浮,大数下沉*/

public static int[] bubbleSort(int[] nums) {for (int i = 0; i < nums.length; i++) {for (int j = i + 1; j < nums.length; j++) {if (nums[j]

nums[j]=nums[i];

nums[i]=temp;

}

}

}returnnums;

}/*** 快速排序:选取一个基准值,利用二分法对其排序*/

public static void quickSort(int[] nums, int low, inthign) {if (low

quickSort(nums,0, index - 1);

quickSort(nums, index+ 1, hign);

}

}private static int getIndex(int[] nums, int low, inthign) {int temp =nums[low];while (low =temp) {

hign--;

}

nums[low]=nums[hign];while (low < hign && nums[low] <=temp) {

low++;

}

nums[hign]=nums[low];

}

nums[low]=temp;returnlow;

}/*** 基数排序:低位优先,比较每个数字的低位,依次到高位,注意是正整数*/

public static int[] radixSort(int[] nums) {if (nums == null || nums.length == 0) {returnnums;

}int max = nums[0];for (int i = 0; i < nums.length; i++) {if (max

max=nums[i];

}

}int digit = 0;while (max != 0) {

max/= 10;

digit++;

}//init list

List> buckets = new ArrayList>();for (int i = 0; i < 10; i++) {

buckets.add(new ArrayList<>());

}for (int i = 0; i < digit; i++) {for (int j = 0; j < nums.length; j++) {int key = (int) (nums[j] % Math.pow(10, i + 1) / Math.pow(10, i));

buckets.get(key).add(nums[j]);

}int count = 0;for (int j = 0; j < nums.length; j++) {while (buckets.get(j).size() > 0) {

nums[count++] = buckets.get(j).remove(0);

}

}//分配完之后,将桶中的元素依次复制回数组

}returnnums;

}public static voidmain(String[] args) {int[] nums = { -1, 0, 0, 0, 0, 3, 3};

System.out.println(removeDuplicates(nums));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值