Skip to content

Instantly share code, notes, and snippets.

@gokhanaliccii
Last active January 3, 2016 07:19
Show Gist options
  • Save gokhanaliccii/8428183 to your computer and use it in GitHub Desktop.
Save gokhanaliccii/8428183 to your computer and use it in GitHub Desktop.
countingsort1 hackerrank solution
package com.algorithm.sorthing;
import java.util.HashMap;
import java.util.Scanner;
//read file https://www.hackerrank.com/challenges/countingsort1
public class CountingSort {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int tempBigValue = 0;
int arraySize=in.nextInt();
int[] array = new int[arraySize];
for (int i = 0; i < arraySize; i++) {
array[i]=in.nextInt();
}
HashMap<Integer, Integer> tempList = new HashMap<>();
for (int i = 0; i < array.length; i++) {
if (tempList.containsKey(array[i])) {
// increase exist key
tempList.put(array[i], ((tempList.get(array[i])) + 1));
} else {
tempList.put(array[i], 1);
}
if (array[i] > tempBigValue)
tempBigValue = array[i];
}
for (int i = 0; i <= tempBigValue; i++) {
if(tempList.containsKey(i))
System.out.print(tempList.get(i)+" ");
else
System.out.print(0+" ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment