Skip to content

Commit

Permalink
Merge pull request #93 from AiyushKumar07/main
Browse files Browse the repository at this point in the history
Updated CountSort.java
  • Loading branch information
lilmistake authored Oct 26, 2023
2 parents 3146168 + fa7101b commit 3751010
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions DSA_Codesheet/Java/countSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import java.util.*;

public class countSort {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int arr[] = new int[n];
for(int i=0; i<n; i++){
arr[i] = input.nextInt();
}
System.out.println("Array Before Sorting :-");
System.out.println();
for(int i = 0; i<n; i++){
System.out.print(arr[i]+" ");
}
System.out.println();

int[] sortedArr = countingSort(arr);


System.out.println("Array After Sorting :-");
for(int i = 0; i<n; i++){
System.out.print(sortedArr[i]+" ");
}

}

public static int[] countingSort(int[] nums) {
int[] minMax = getMinMax(nums);
int offset = 0;
if(minMax[0] < 0) {
offset = minMax[0] * (-1);
}
for(int i = 0; i < nums.length; i++) {
nums[i]+=offset;
}
int[] count = new int[minMax[1] + offset + 1];
for(int i = 0; i < nums.length; i++) {
count[nums[i]]++;
}

int j = 0;
for(int i = 0; i < count.length; i++) {
while(count[i] != 0) {
nums[j] = i - offset;
count[i]--;
j++;
}
}
return nums;
}

public static int[] getMinMax(int[] nums) {
int max = nums[0];
int min = nums[0];
for(Integer num : nums) {
if(num > max) {
max = num;
}
if(num < min) {
min = num;
}
}
return new int[]{min, max};
}
}

0 comments on commit 3751010

Please sign in to comment.