**The Full Counting Sort**

### Problem Statement :

Use the counting sort to order a list of strings associated with integers. If two strings are associated with the same integer, they must be printed in their original order, i.e. your sorting algorithm should be stable. There is one other twist: strings in the first half of the array are to be replaced with the character - (dash, ascii 45 decimal). Insertion Sort and the simple version of Quicksort are stable, but the faster in-place version of Quicksort is not since it scrambles around elements while sorting. Design your counting sort to be stable. Function Description Complete the countSort function in the editor below. It should construct and print the sorted strings. countSort has the following parameter(s): string arr[n][2]: each arr[i] is comprised of two strings, x and s Returns - Print the finished array with each element separated by a single space. Note: The first element of each , , must be cast as an integer to perform the sort. Input Format The first line contains , the number of integer/string pairs in the array . Each of the next contains and , the integers (as strings) with their associated strings. Output Format Print the strings in their correct order, space-separated on one line.

### Solution :

` ````
Solution in C :
In C++ :
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include<string>
#include<list>
using namespace std;
vector<string> a[100];
char str[100007];
int n,i,j,x;
int main() {
cin>>n;
for(i=0;i<n/2;i++)
{
cin>>x;
cin>>str;
a[x].push_back("-");
}
for(;i<n;i++)
{
cin>>x;
cin>>str;
a[x].push_back(str);
}
for(i=0;i<100;i++)
{
x=a[i].size();
for(j=0;j<x;j++)
cout<<a[i][j]<<" ";
}
return 0;
}
In Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
StringBuffer[] map = new StringBuffer[100];
for(int i = 0; i < 100; i++) {
map[i] = new StringBuffer();
}
for(int i = 0; i < n; i++) {
StringTokenizer tok = new StringTokenizer(in.readLine());
int v = Integer.parseInt(tok.nextToken());
String s = tok.nextToken();
map[v].append(i < n / 2 ? "-" : s).append(" ");
}
for(int i = 0; i < 100; i++) {
System.out.print(map[i]);
}
System.out.println();
}
}
In C :
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int* hist[100];
int size[100];
int pos[100];
int main() {
int BUF_SZ = 256;
int N, v, i, j;
scanf("%d", &N);
fprintf(stderr, "%d elements\n", N);
char *data[N];
for (i = 0; i < N; ++i) {
if (i < N/2) {
data[i] = (char*)malloc(BUF_SZ*sizeof(char));
}
scanf("%d %s", &v, data[i<N/2?i:i-N/2]);
if (hist[v] == NULL) {
size[v] = 2*N/100 + 1;
hist[v] = (int*)malloc(size[v]*sizeof(int));
} else if (pos[v] >= size[v]) {
size[v] *= 2;
hist[v] = (int*)realloc(hist[v], size[v]*sizeof(int));
}
(hist[v])[pos[v]++] = i;
}
for (i = 0; i < 100; ++i) {
fprintf(stderr, "Printing %d\n", i);
for (j = 0; j < pos[i]; ++j) {
if ((hist[i])[j] < N/2) {
printf("- ");
} else {
printf("%s ", data[(hist[i])[j]-N/2]);
free(data[(hist[i])[j]-N/2]);
}
}
if (hist[i] != NULL) {
free(hist[i]);
hist[i] = NULL;
}
}
return 0;
}
In Python3 :
n = int(input())
ar = []
for i in range(0,n) :
data = input().strip().split(' ')
ar.append((int(data[0]), data[1]))
c = [0]*100
for a in ar :
c[a[0]] += 1
c1 = [0]*100
for x in range(1,100) :
c1[x] = c1[x-1] + c[x-1]
s = ['-']*n
for i in range(0,n) :
if i >= n/2 :
s[c1[ar[i][0]]] = ar[i][1]
c1[ar[i][0]] += 1
print(' '.join(s))
```

## View More Similar Problems

## Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →## AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

View Solution →## Waiter

You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

View Solution →## Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →## Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

View Solution →## Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

View Solution →