`
standalone
  • 浏览: 613197 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

sort n integers of range 0~n^2-1 in O(n) time

 
阅读更多

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

}

 

分享到:
评论

相关推荐

    l2tp.rar_range

    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).

    LeetCode最全代码

    | _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...

    计算机组成与结构体系英文课件:Chapter9 Arithmetic.pdf

    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-...

    Algebraic Graph Algorithms Lecture Notes (Stanford CS367)

    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:find-n-unique-integer

    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, ...

    计算机组成与结构体系英文课件:Chapter9 Arithmetic

    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. ...

    occam一维反演

    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 ...

    wolframalpha数学使用教程.pdf

    - **整数解**:使用"solve _ over the integers",如"solve x^2 - 4 = 0 over the integers"。 - **导数**:"derivative of_"或"second derivative of_",如"derivative of x^3"。 - **不定积分**:"indefinite ...

    C++课程辅导

    - **无符号短整型** (`Unsigned short`, `unsigned short int`): 存储空间为2字节,值域范围为0到2^16-1。 - **有符号整型** (`int`, `signed int`): 存储空间为4字节,值域范围为-2^31到2^31-1。 - **无符号整型** ...

    RSATool2.exe

    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 &gt; 240 Bits can take a LOT of time and...

    calculate SUM(n) = 1 + 2 + 3 + ... + n

    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 ...

    leetcode2sumc-leetcode-1304-Find-N-Unique-Integers-Sum-up-to-Zero:leetc

    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 &lt; n / 2 ; i ++ ) { arr [ j ++ ] = x ; arr...

    Palindrome Sub-Array

     There is two integers N, M (1&lt;=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 ...

    全球地形资料img ETOPO1

    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 ...

    gone fishing

    Next, there is a line of n integers specifying fi (1 &lt;= i &lt;=n), then a line of n integers di (1 &lt;=i &lt;=n), and finally, a line of n - 1 integers ti (1 &lt;=i &lt;=n - 1). Input is terminated by a case in ...

    ACM题目的C++/C代码程序

    Both s and t are integers, 1 &lt;= s &lt;= 90 and 1 &lt;= t &lt;= 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, ...

    POJ 1727 Advanced Causal Measurements (ACM)解题报告

    - 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 \). ##...

Global site tag (gtag.js) - Google Analytics