-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into GovindaRohith-dsa-worksheet
- Loading branch information
Showing
3 changed files
with
110 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import List | ||
|
||
def sortArray(N: List[int]) -> List[int]: | ||
min_val, max_val = min(N), max(N) | ||
count = [0] * (max_val - min_val + 1) | ||
|
||
for num in N: | ||
count[num - min_val] += 1 | ||
|
||
sorted_array = [] | ||
for i in range(len(count)): | ||
sorted_array.extend([i + min_val] * count[i]) | ||
|
||
return sorted_array | ||
|
||
if __name__ == "__main__": | ||
# Input | ||
n = int(input("Enter the number of elements: ")) | ||
user_input = list(map(int, input("Enter the elements separated by spaces: ").split())) | ||
|
||
# Output the array before sorting | ||
print("Array Before Sorting :-") | ||
print(" ".join(map(str, user_input))) | ||
|
||
# Sort the array | ||
sorted_list = sortArray(user_input) | ||
|
||
# Output the array after sorting | ||
print("Array After Sorting :-") | ||
print(" ".join(map(str, sorted_list))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters