PuzzlersWorld.com

  • Hacker Puzzle
  • Interview Puzzles
  • Number Puzzles
  • Maths Puzzles

Sort the array in O(N)

(1 votes, average: 1.00 out of 5)

April 11, 2013 by puzzler 1 Comment

Problem Statement
Given an integer array of length N, containing values in the range 1,2,3…N^2. Sort the array in O(N) time.

Solution


We can sort the array in O(N) using radix sort, by using a list of lists of size 10, first position denotes the buckets for 0, 10th pos for 9.

We need passes equal to the double of digits in N, as the number of digits in N^2 can be maximum double of the digits in N. To get a digit we can first divide number with 10^i and then get the remainder after dividing by 10, where i = 0 to 2*Number of digits in N. You can understand this better by this radix sort java code snippet below.

Java Code for Radix Sort

import java.util.ArrayList;

public class RadixSort {

	public void radixSort(int arr[], int maxDigits){
		int exp = 1;//10^0;
		for(int i =0; i < maxDigits; i++){
			ArrayList bucketList[] = new ArrayList[10];
			for(int k=0; k < 10; k++){
				bucketList[k] = new ArrayList();
			}
			for(int j =0; j < arr.length; j++){
				int number = (arr[j]/exp)%10;
				bucketList[number].add(arr[j]);
			}
			exp *= 10;
			int index =0;
			for(int k=0; k < 10; k++){
				for(int num: bucketList[k]){
					arr[index] = num;
					index++;
				}
			}
		}

		System.out.println("Sorted numbers");
		for(int i =0; i < arr.length; i++){
			System.out.print(arr[i] +", ");
		}
	}

	public static void main(String[] argv){
		int n = 5;
		int arr[] = {1,4,2,3,5,10,8};
		new RadixSort().radixSort(arr, 2);
	}
}
  • Share on Whatsapp
  • Share on Facebook
  • Share on Twitter
Facebook Comments
Facebook
Next Puzzle
Tic-Tac-Toe-Tomek Solution: Google codejam 2013 Qual Round

Checkout more Interview Questions Tags: Algorithm, Array, Difficult, Interview, Java, Solved Puzzles

Comments

  1. Alexander says

    June 25, 2014 at 1:32 pm

    The shit ain’t workin’

    Reply

Leave a Comment Cancel reply

Submit your Puzzle

You may also like

  • DS with insert, delete and getRandomElement in O(1)
  • Find middle element of linked list
  • Find all non duplicate pairs that sum to S
  • Objective Questions set C/C++ – Part 6
  • Objective Questions set C/C++ – Part 3
  • Reverse a link list without using another
  • Minesweeper Master Solution – Google Code jam 2014
  • Cake Cutting solution: Facebook hacker cup 2013 Round 2
  • Objective Questions set C/C++ – Part 4
  • Falling Diamonds Solution: Google codejam 2013 Round 1B

Categories

  • Aive hi Puzzles
  • Akbar and Birbal
  • Alphabetical Puzzles
  • Bollywood Puzzles
  • Google Code jam
  • Hindi Riddles
  • Interview Puzzles
  • Interview Questions
  • Logical Puzzles
  • Malayalam Puzzles
  • Maths Puzzles
  • Miscellaneous
  • Number Puzzles
  • Picture Puzzles
  • Riddles
  • Tamil Puzzles
  • Technical

Social

  • View puzzlersworld’s profile on Twitter
privacy policy

Copyright © 2025 · eleven40 Pro Theme on Genesis Framework · WordPress · Log in

  • Hacker Puzzle
  • Logo Puzzles
  • Optical Illusions
  • WhatsApp Puzzles
  • Picture Puzzles
  • Riddles
    ▼
    • Hindi Riddles
  • Bollywood Puzzles
  • Alphabetical Puzzles
  • Aive hi Puzzles
  • Interview Puzzles
  • Logical Puzzles
  • Interview Questions
    ▲
    • Data Structures
    • Binary Tree
    • Algorithms
    • Recursion Questions
    • Amazon Interview Questions
    • Snapdeal Interview Questions
    • Google Code jam
  • Technical
  • Akbar and Birbal
  • Number Puzzles
  • Maths Puzzles
  • Miscellaneous