java radix sort_Java RadixSort

Java RadixSort

/**

*

*

*

Copyright 1994-2018 JasonInternational

*

All rights reserved.

*

Created on 2018年4月10日

*

Created by Jason

*

*

*/

package cn.ucaner.algorithm.sorts;

import java.util.Arrays;

/**

* Radix sort is a non-comparative integer sorting algorithm that sorts data

* with integer keys by grouping keys by the individual digits which share the

* same significant position and value. A positional notation is required, but

* because integers can represent strings of characters (e.g., names or dates)

* and specially formatted floating point numbers, radix sort is not limited to

* integers.

*

* Family: Bucket.

* Space: 10 Buckets with at most n integers per bucket.

* Stable: True.

*

* Average case = O(n*k)

* Worst case = O(n*k)

* Best case = O(n*k)

*

* NOTE: n is the number of digits and k is the average bucket size

*

* @see Radix Sort (Wikipedia)

*

* @author Justin Wetherell

*/

public class RadixSort {

private static final int NUMBER_OF_BUCKETS = 10; // 10 for base 10 numbers

private RadixSort() { }

public static Integer[] sort(Integer[] unsorted) {

int[][] buckets = new int[NUMBER_OF_BUCKETS][10];

for (int i = 0; i < NUMBER_OF_BUCKETS; i++)

buckets[i][0] = 1; // Size is one since the size is stored in first element

int numberOfDigits = getMaxNumberOfDigits(unsorted); // Max number of digits

int divisor = 1;

for (int n = 0; n < numberOfDigits; n++) {

int digit = 0;

for (int d : unsorted) {

digit = getDigit(d, divisor);

buckets[digit] = add(d, buckets[digit]);

}

int index = 0;

for (int i = 0; i < NUMBER_OF_BUCKETS; i++) {

int[] bucket = buckets[i];

int size = bucket[0];

for (int j = 1; j < size; j++) {

unsorted[index++] = bucket[j];

}

buckets[i][0] = 1; // reset the size

}

divisor *= 10;

}

return unsorted;

}

private static int getMaxNumberOfDigits(Integer[] unsorted) {

int max = Integer.MIN_VALUE;

int temp = 0;

for (int i : unsorted) {

temp = (int) Math.log10(i) + 1;

if (temp > max)

max = temp;

}

return max;

}

private static int getDigit(int integer, int divisor) {

return (integer / divisor) % 10;

}

private static int[] add(int integer, int[] bucket) {

int size = bucket[0]; // size is stored in first element

int length = bucket.length;

int[] result = bucket;

if (size >= length) {

result = Arrays.copyOf(result, ((length * 3) / 2) + 1);

}

result[size] = integer;

result[0] = ++size;

return result;

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值