From c1860de935f5b8502f1b1854e47a5019e7bdb9e6 Mon Sep 17 00:00:00 2001 From: sattya19 Date: Thu, 12 Oct 2023 16:48:34 +0530 Subject: [PATCH 1/2] added counting sort --- Python/Sorting.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Python/Sorting.py b/Python/Sorting.py index 08dae4f2..8416b68c 100644 --- a/Python/Sorting.py +++ b/Python/Sorting.py @@ -138,6 +138,30 @@ def radix_sort(self): buckets = [[] for _ in range(10)] + + +def counting_sort(self): + + max_num = max(self.arr) + min_num = min(self.arr) + range_of_values = max_num - min_num + 1 + + count = [0] * range_of_values + output = [0] * len(self.arr) + + for num in self.arr: + count[num - min_num] += 1 + + for i in range(1, range_of_values): + count[i] += count[i - 1] + + for num in reversed(self.arr): + output[count[num - min_num] - 1] = num + count[num - min_num] -= 1 + + self.arr = output + + # Base example array arr = [64, 34, 25, 12, 22, 11, 90] @@ -186,4 +210,10 @@ def radix_sort(self): print("\nRadix Sort -") sorting_obj = SortingAlgorithms(arr) # Reset the array sorting_obj.radix_sort() +sorting_obj.display() + +# Counting Sort +print("\nCounting Sort -") +sorting_obj = SortingAlgorithms(arr) # Reset the array +sorting_obj.counting_sort() sorting_obj.display() \ No newline at end of file From 34750d89f5527f79f87fb7dea4ef14535aea3e95 Mon Sep 17 00:00:00 2001 From: sattya19 Date: Thu, 12 Oct 2023 20:02:32 +0530 Subject: [PATCH 2/2] added SortingJava --- CONTRIBUTORS.md | 4 ++- SortingJava/QuickSort.java | 51 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 SortingJava/QuickSort.java diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 4d87438d..ca6b6c1c 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -1,8 +1,9 @@

✨Happy Hacktoberfest 2023✨

Hacktoberfest is a yearly event whiich encourages not only programmers and coders but also non-technical background to contribute in open source and start their journey with open source.

-

Star Repo is Mandatory for Hactober Accepting!

+

Star Repo is Mandatory for Hactober Accepting!

# Message +

Enter your Name, Github Link in the given format. Don't try to change anything else!!!

| Name | Github Link | @@ -21,3 +22,4 @@ | Shreeraj Bhamare | shreerajbhamare| | Moh Faizan | Moh Faizan| | Praneesh Sharma | Praneesh Sharma | +| Satya Sharma | Satya Sharma | diff --git a/SortingJava/QuickSort.java b/SortingJava/QuickSort.java new file mode 100644 index 00000000..b29c7a95 --- /dev/null +++ b/SortingJava/QuickSort.java @@ -0,0 +1,51 @@ +package SortingJava; + +public class QuickSort { + public static void quickSort(int[] arr) { + quickSort(arr, 0, arr.length - 1); + } + + private static void quickSort(int[] arr, int low, int high) { + if (low < high) { + int pivotIndex = partition(arr, low, high); + quickSort(arr, low, pivotIndex - 1); + quickSort(arr, pivotIndex + 1, high); + } + } + + private static int partition(int[] arr, int low, int high) { + int pivot = arr[high]; + int i = low - 1; + + for (int j = low; j < high; j++) { + if (arr[j] < pivot) { + i++; + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + + int temp = arr[i + 1]; + arr[i + 1] = arr[high]; + arr[high] = temp; + + return i + 1; + } + + public static void main(String[] args) { + int[] arr = { 24, 9, 29, 14, 19, 27 }; + System.out.println("Before quick sort:"); + printArray(arr); + quickSort(arr); + System.out.println("After quick sort:"); + printArray(arr); + } + + public static void printArray(int[] arr) { + for (int num : arr) { + System.out.print(num + " "); + } + System.out.println(); + } +}