HackerRank Counting Sort 2 Problem Solution
In this post, we will solve HackerRank Counting Sort 2 Problem Solution.
Often, when a list is sorted, the elements being sorted are just keys to other values. For example, if you are sorting files by their size, the sizes need to stay connected to their respective files. You cannot just take the size numbers and output them in order, you need to output all the required file information.
The counting sort is used if you just need to sort a list of integers. Rather than using a comparison, you create an integer array whose index range covers the entire range of values in your array to sort. Each time a value occurs in the original array, you increment the counter at that index. At the end, run through your counting array, printing the value of each non-zero valued index that number of times.
For example, consider an array arr = [1, 1, 3, 2, 1]. All of the values are in the range [0…3]
, so create an array of zeroes, result = [0, 0, 0, 0]. The results of each iteration follow:
i arr[i] result 0 1 [0, 1, 0, 0] 1 1 [0, 2, 0, 0] 2 3 [0, 2, 0, 1] 3 2 [0, 2, 1, 1] 4 1 [0, 3, 1, 1]
Now we can print the sorted array: sorted = [1, 1, 1, 2, 3].
Function Description
Complete the countingSort function in the editor below. It should return the original array, sorted ascending, as an array of integers.
countingSort has the following parameter(s):
- arr: an array of integers
Input Format
The first line contains an integer n, the length of arr. The next line contains space-separated
integers arr[i] where 0 < i < n.
Output Format
Print the sorted list as a single line of space-separated integers.
Sample Input
100 63 25 73 1 98 73 56 84 86 57 16 83 8 25 81 56 9 53 98 67 99 12 83 89 80 91 39 86 76 85 74 39 25 90 59 10 94 32 44 3 89 30 27 79 46 96 27 32 18 21 92 69 81 40 40 34 68 78 24 87 42 69 23 41 78 22 6 90 99 89 50 30 20 1 43 3 70 95 33 46 44 9 69 48 33 60 65 16 82 67 61 32 21 79 75 75 13 87 70 33
Sample Output
1 1 3 3 6 8 9 9 10 12 13 16 16 18 20 21 21 22 23 24 25 25 25 27 27 30 30 32 32 32 33 33 33 34 39 39 40 40 41 42 43 44 44 46 46 48 50 53 56 56 57 59 60 61 63 65 67 67 68 69 69 69 70 70 73 73 74 75 75 76 78 78 79 79 80 81 81 82 83 83 84 85 86 86 87 87 89 89 89 90 90 91 92 94 95 96 98 98 99 99
Explanation
Once our counting array has been filled, loop from index 0 to the end, printing each i value arr[i] times.
Counting Sort 2 C Solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
unsigned int long *count;
int tmp;
scanf("%i\n", &n);
count = calloc (1, sizeof (int long) * 100);
for (int i = 0; i < n; i++)
{
scanf("%i ", &tmp);
if (tmp < 0 || tmp > 99)
continue;
count[tmp]++;
}
for (int i = 0; i < 100; i++)
for (int j = 0; j < count[i]; j++)
printf("%i ", i);
printf("\n");
return 0;
}
Counting Sort 2 C++ Solution
#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define INF 111111
#define N 5001
#define pi 3.14159265359
using namespace std;
int a[111];
int main() {
int n;
cin >> n;
for(int i = 0; i < n; i ++) {
int x;
cin >> x;
a[x] ++;
}
for(int i = 0; i < 100; i++) {
for(int j = 0; j < a[i]; j ++) cout<<i<<" ";
}
}
Counting Sort 2 C Sharp Solution
using System;
class Program
{
public static void Main()
{
string elmCountStr = Console.ReadLine();
int count = int.Parse(elmCountStr);
int [] ar = new int[count];
string [] elements = Console.ReadLine().Split(' ');
for(int i = 0; i < elements.Length; i++)
{
ar[i] = int.Parse(elements[i]);
}
int [] counts = new int[100];
for( int i = 0; i < counts.Length; i++)
{
counts[i] = 0;
}
foreach( int val in ar)
{
counts[val]++;
}
for( int i = 0; i < counts.Length; i++)
{
for (int j = 0; j < counts[i]; j++)
{
Console.Write(i + " ");
}
}
Console.WriteLine("");
}
}
Counting Sort 2 Java Solution
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] frequencies = new int[100];
for(int i = 0; i < n; i++)
{
int num = input.nextInt();
frequencies[num] = frequencies[num] + 1;
}
for(int i = 0; i < frequencies.length; i++)
{
for(int j = 0; j < frequencies[i]; j++)
{
System.out.print(i+" ");
}
}
}
}
Counting Sort 2 JavaScript Solution
function countSort(ar){
for(var i = 0; i < ar.length; i++){
newArray[ar[i]]++;
}
var resultArray = [];
for(var i = 0; i < newArray.length; i++){
if(newArray[i] !== 0){
for(var x = 0; x < newArray[i]; x++){
resultArray.push(i);
}
}
}
printArray(resultArray);
}
var newArray = [];
function processData(input) {
var words = input.split("\n");
var array = words[1].split(" ");
for(var i = 0; i < 100; i++){
newArray.push(0);
}
var result = countSort(array);
}
function printArray(array){
var string = "";
for(var i = 0; i < array.length; i++){
string += array[i] + " ";
}
process.stdout.write("" + string + "\n");
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
Counting Sort 2 Python Solution
count = input()
nums = list(map(int, input().split()))
#Quick sort
def sort(arr):
less = []
equal = []
greater = []
if(len(arr) > 1):
pivot = arr[0]
for i in range(len(arr)):
if arr[i] > pivot:
greater.append(arr[i])
elif arr[i] < pivot:
less.append(arr[i])
else:
equal.append(arr[i])
return sort(less) + equal + sort(greater)
else:
return arr
s = sort(nums)
for i in range(len(s)):
print(s[i], end=' ')
Other Solutions