forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCounting_Sort.kt
78 lines (67 loc) · 1.66 KB
/
Counting_Sort.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Kotlin Code for Counting Sort
public class Counting_Sort {
// Function that sort the given input
fun sort(int:input[]):void
{
int n = input.length;
int output[] = new int[n];
int max = input[0];
int min = input[0];
for(i in 1 until n)
{
if(input[i] > max)
{
max = input[i];
}
else if(input[i] < min)
{
min = input[i];
}
}
// Size of count array
int k = max - min + 1;
int count_array[] = new int[k];
for(i in 0 until n)
{
count_array[input[i] - min]++;
}
for(i in 1 until k)
{
count_array[i] += count_array[i - 1];
}
for(i in 0 until n)
{
output[count_array[input[i] - min] - 1] = input[i];
count_array[input[i] - min]--;
}
// Copy the output array to input, so that input now contains sorted values
for(i in 0 until n)
{
input[i] = output[i];
}
}
// Main Function
fun main()
{
var read = Scanner(System.`in`)
println("Enter the size of Array:")
val arrSize = read.nextLine().toInt()
var input = IntArray(arrSize)
println("Enter elements")
for(i in 0 until arrSize)
{
input[i] = read.nextLine().toInt()
}
sort(input);
for(i in 0 until arrSize)
{
println(input[i]);
}
}
}
/*
Input
2 1 4 1 3 5 7 5 4
Output
1 1 2 3 4 4 5 5 7
*/