Skip to content

Commit

Permalink
Merge branch 'main' into GovindaRohith-dsa-worksheet
Browse files Browse the repository at this point in the history
  • Loading branch information
lilmistake authored Oct 26, 2023
2 parents 85c0fcf + 0beb059 commit ad5d207
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 9 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};
}
}
30 changes: 30 additions & 0 deletions DSA_Codesheet/Python/countingSort.py
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)))
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ Distributed under the MIT License. See `LICENSE` for more information.
<sub><b>Null</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Raj-sharma01">
<img src="https://avatars.githubusercontent.com/u/114892666?v=4" width="100;" alt="Raj-sharma01"/>
<br />
<sub><b>Raj Sharma</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/AdityaDKale">
<img src="https://avatars.githubusercontent.com/u/115162556?v=4" width="100;" alt="AdityaDKale"/>
Expand Down Expand Up @@ -137,15 +144,15 @@ Distributed under the MIT License. See `LICENSE` for more information.
<br />
<sub><b>DEEPAK KUMAR DAS</b></sub>
</a>
</td>
</td></tr>
<tr>
<td align="center">
<a href="https://github.com/BEAST-PRINCE">
<img src="https://avatars.githubusercontent.com/u/98230743?v=4" width="100;" alt="BEAST-PRINCE"/>
<br />
<sub><b>DIVYANSHU PRINCE</b></sub>
</a>
</td></tr>
<tr>
</td>
<td align="center">
<a href="https://github.com/Faraaz22">
<img src="https://avatars.githubusercontent.com/u/130041060?v=4" width="100;" alt="Faraaz22"/>
Expand Down Expand Up @@ -175,12 +182,10 @@ Distributed under the MIT License. See `LICENSE` for more information.
</a>
</td>
<td align="center">
<a href="https://github.com/GovindaRohith">
<img src="https://avatars.githubusercontent.com/u/102508383?v=4" width="100px;" alt="GovindaRohith"/>
<a href="https://github.com/DebidYadav">
<img src="https://avatars.githubusercontent.com/u/51717897?v=4" width="100;" alt="DebidYadav"/>
<br />
<sub><b>GovindaRohith</b></sub>
<sub><b>Debid Yadav</b></sub>
</a>
</td>
</tr>
</td></tr>
</table>
<!-- readme: collaborators,contributors -end -->

0 comments on commit ad5d207

Please sign in to comment.