This is one problem introduced in "Introduction to Algorithm".
We can use radix-sort to solve it.
package alg;
import java.util.ArrayList;
import java.util.Iterator;
/*
* This example shows how to use radix sort algorithm to sort N integers in the range of 0 ~ N^2-1.
*/
public class RadixSortExample {
void sort(int a[], int N){
int len = a.length;
ArrayList<ArrayList<Integer>> bucket= new ArrayList<ArrayList<Integer>>(N);
for(int i = 0; i<N;i++){
bucket.add(new ArrayList<Integer>(len));
}
ArrayList<Integer> temp = new ArrayList<Integer>(len);
for(int i = 0; i< len; i++){
temp.add(new Integer(a[i]));
}
for(int i = 0; i<len; i++){
int digit0 = temp.get(i).intValue() % N;
bucket.get(digit0).add(temp.get(i));
}
temp.clear();
for(int i = 0; i<N; i++){
Iterator<Integer> it = bucket.get(i).iterator();
while(it.hasNext()) temp.add(it.next());
}
for(int i = 0; i<N;i++){
bucket.get(i).clear();
}
for(int i = 0; i<len; i++){
int digit1 = temp.get(i).intValue() / N;
bucket.get(digit1).add(temp.get(i));
}
temp.clear();
for(int i = 0; i<N; i++){
Iterator<Integer> it = bucket.get(i).iterator();
while(it.hasNext()) temp.add(it.next());
}
Iterator<Integer> it = temp.iterator();
while(it.hasNext()) System.out.println(it.next());
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a []= {63,20,43,15,56,9,1,28};
RadixSortExample r = new RadixSortExample();
r.sort(a,8);
}
}
分享到:
相关推荐
The range of reals in PDF A is the same as SkFixed: + - 32,767 and + - 1 65,536 (though integers can range 2^31 - 1 to -2^31).
| _O(2^n)_ | _O(n)_ | Hard | 馃摉 | 421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an...
For an n-bit unsigned integer, the range of values extends from 0 to 2^n - 1. For instance, an 8-bit unsigned integer can represent numbers from 0 to 255 (11111111 in binary). 2. **Unsigned Non-...
It is assumed that operations on O(log n) integers take O(1) time in the word-RAM model of computation. Various attempts have been made to improve the runtime of matrix multiplication beyond the ...
leetcode伪代码find-n-unique-integers-sum-up-to-zero 题目解读: 题目来源: 原文: Given an integer n, return any array containing n unique integers such that they add up to 0. 解读: 给定一个正整数n, ...
An unsigned integer can range from 0 to (2^n) - 1, where 'n' is the number of bits used for the representation. For example, an 8-bit unsigned integer can represent values from 0 to 255. **2. ...
c iruf = roughness measure, 0 (size penalty), 1 (1st deriv.), 2 (2nd deriv.) c nit = current iteration number (0=start) c i = loop index c itmax = maximum iteration number c konv = convergence flag ...
Column 1: Programs for sorting integers bitsort.c -- Sort with bit vectors. sortints.cpp -- Sort using C++ STL sets. qsortints.c -- Sort with C library qsort. bitsortgen.c -- Generate random ...
- **整数解**:使用"solve _ over the integers",如"solve x^2 - 4 = 0 over the integers"。 - **导数**:"derivative of_"或"second derivative of_",如"derivative of x^3"。 - **不定积分**:"indefinite ...
- **无符号短整型** (`Unsigned short`, `unsigned short int`): 存储空间为2字节,值域范围为0到2^16-1。 - **有符号整型** (`int`, `signed int`): 存储空间为4字节,值域范围为-2^31到2^31-1。 - **无符号整型** ...
2) Type in or Paste the number in the Editbox for the Modulus (N). This enables the 'Factor' button. 3) Press the Factor N button. Note that factoring numbers > 240 Bits can take a LOT of time and...
The input will consist of a series of integers n, one integer per line. For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit ...
2 和 c leetcode 1304 - 找到 N 个唯一整数总和为零 C# public int [] SumZero ( int n ) { var arr = new int [ n ]; int j = 0 ; int x = 1 ; for ( int i = 0 ; i < n / 2 ; i ++ ) { arr [ j ++ ] = x ; arr...
There is two integers N, M (1<=N, M) separated by one white space in the first line of each block, representing the size of the 2-D array. Then N lines follow, each line contains M integers ...
ETOPO1 comes in two different binary formats: 2-byte/16-bit integer (i2) and 4-byte/32-bit float (f4); float refers to the file format, not the elevation values in the grids, which have been rounded ...
Next, there is a line of n integers specifying fi (1 <= i <=n), then a line of n integers di (1 <=i <=n), and finally, a line of n - 1 integers ti (1 <=i <=n - 1). Input is terminated by a case in ...
Both s and t are integers, 1 <= s <= 90 and 1 <= t <= 12. The values for t are always in strictly increasing order. A value of -1 for n signals the end of the input. Output For each input set, ...
- The coordinates of the observed events are integers in the range \([-1000000, 1000000]\). - The number of events \( n \) and the number of causes \( m \) satisfy \( 1 \leq n, m \leq 100000 \). ##...