-
Notifications
You must be signed in to change notification settings - Fork 368
/
counting_sort.java
52 lines (42 loc) · 1.34 KB
/
counting_sort.java
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
import java.util.*;
public class counting_sort {
// We will be using counting sort.
// Efficient for smaller range.
// Time Complexity - O(n+k) | Space Complexity - O(n+k)
public static void Sort(int[] nums) {
//STEP 1 : largest element in the array.
int largest = Integer.MIN_VALUE;
for(int i=0; i<nums.length; i++){
largest = Math.max(largest, nums[i]);
}
//STEP 2 : making our count array
int count[] = new int[largest+1]; //this array keeps track of the coutn frequency of numbers equal to its index
for(int i=0;i<nums.length;i++){
count[nums[i]]++; //writing the frequncy in coutn array
}
//STEP 3 : Sorting
for(int i=0,j=0;i<count.length;i++){
while(count[i]>0){
nums[j]=i;
j++;
count[i]--;
}
}
}
static void Printarr(int arr[])
{
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + ", ");
}
}
public static void main(String args[]) {
int nums[] = { 1, 4, 3, 2, 4, 3, 7 };
System.out.println("Original Array : ");
Printarr(nums);
System.out.println();
System.out.println("Sorted Array : ");
Sort(nums);
Printarr(nums);
}
}