Professional Documents
Culture Documents
Goldman Sachs Counting Sort Coding PlacementClasses
Goldman Sachs Counting Sort Coding PlacementClasses
Goldman Sachs Counting Sort Coding PlacementClasses
s
Telegram - https://t.me/placementclasses
Counting Sort | Goldman Sachs
la s
Write a program to input an array of integers from the user and print the
C
sorted array using counting sort.
t
Sample input
Sample input-1
e n
Enter the length of array : 3
m
Enter the element : 9
e
Enter the element : 0
c
Enter the element : 3
1
Enter the length of array : 6
Enter the element : 7
Enter the element : 3
Enter the element : 8
Enter the element : 1
Enter the element : 0
Enter the element : 2
s
Sample output-2
e
Array sorted by counting sort is :
s
[0, 1, 2, 3, 7, 8]
la s
C
Algorithm
t
⦁ Define a function counting_sort( ) to sort the array of integers.
n
⦁
e
Initialize two arrays ‘result’ and ‘a’.
m
⦁ Iterate another loop to store the cumulative count and increment a[i] by
e
a[i+1].
c
⦁ To find the index of each element of the input array in ‘a’ , append the
elements to ‘result’.
la
⦁ Initialise an array ‘arr’ and input the length and elements of the array
P
from the user.
2
Code
e s
ss
Cla
nt
me
c e
Pla
3