From fc025a97c4454cde374ab75a535e1733cb00e729 Mon Sep 17 00:00:00 2001 From: PriyanshuGupta18 <65297674+PriyanshuGupta18@users.noreply.github.com> Date: Sun, 2 Oct 2022 17:48:43 +0530 Subject: [PATCH 1/4] Create radix_sort.java --- java/radix_sort.java | 57 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 java/radix_sort.java diff --git a/java/radix_sort.java b/java/radix_sort.java new file mode 100644 index 0000000..69492dc --- /dev/null +++ b/java/radix_sort.java @@ -0,0 +1,57 @@ +import java.io.*; +import java.util.*; + +class Radix { + static int getMax(int arr[], int n) + { + int mx = arr[0]; + for (int i = 1; i < n; i++) + if (arr[i] > mx) + mx = arr[i]; + return mx; + } + static void countSort(int arr[], int n, int exp) + { + int output[] = new int[n]; // output array + int i; + int count[] = new int[10]; + Arrays.fill(count, 0); + for (i = 0; i < n; i++) + count[(arr[i] / exp) % 10]++; + for (i = 1; i < 10; i++) + count[i] += count[i - 1]; + + for (i = n - 1; i >= 0; i--) { + output[count[(arr[i] / exp) % 10] - 1] = arr[i]; + count[(arr[i] / exp) % 10]--; + } + for (i = 0; i < n; i++) + arr[i] = output[i]; + } + + static void radixsort(int arr[], int n) + { + + int m = getMax(arr, n); + + for (int exp = 1; m / exp > 0; exp *= 10) + countSort(arr, n, exp); + } + + static void print(int arr[], int n) + { + for (int i = 0; i < n; i++) + System.out.print(arr[i] + " "); + } + + + public static void main(String[] args) + { + int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 }; + int n = arr.length; + + + radixsort(arr, n); + print(arr, n); + } +} From 7865010c82a9087132778bcd674ba088e5d55ff1 Mon Sep 17 00:00:00 2001 From: PriyanshuGupta18 <65297674+PriyanshuGupta18@users.noreply.github.com> Date: Sun, 2 Oct 2022 17:54:39 +0530 Subject: [PATCH 2/4] Create insertion_sort.py --- python/insertion_sort.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 python/insertion_sort.py diff --git a/python/insertion_sort.py b/python/insertion_sort.py new file mode 100644 index 0000000..e634ca6 --- /dev/null +++ b/python/insertion_sort.py @@ -0,0 +1,16 @@ +def insertionSort(arr): + for i in range(1, len(arr)): + + key = arr[i] + j = i-1 + while j >=0 and key < arr[j] : + arr[j+1] = arr[j] + j -= 1 + arr[j+1] = key +arr = [12, 11, 13, 5, 6] +insertionSort(arr) +lst = [] +print("Sorted array is : ") +for i in range(len(arr)): + lst.append(arr[i]) +print(lst) From 162e76f726c6e97871bababdcd90506ff0090c7d Mon Sep 17 00:00:00 2001 From: PriyanshuGupta18 <65297674+PriyanshuGupta18@users.noreply.github.com> Date: Sun, 2 Oct 2022 17:57:12 +0530 Subject: [PATCH 3/4] Create bubble_sort.py --- python/bubble_sort.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 python/bubble_sort.py diff --git a/python/bubble_sort.py b/python/bubble_sort.py new file mode 100644 index 0000000..c440101 --- /dev/null +++ b/python/bubble_sort.py @@ -0,0 +1,15 @@ +def bubblesort(elements): + swapped = False + for n in range(len(elements)-1, 0, -1): + for i in range(n): + if elements[i] > elements[i + 1]: + swapped = True + elements[i], elements[i + 1] = elements[i + 1], elements[i] + if not swapped: + return +elements = [39, 12, 18, 85, 72, 10, 2, 18] +print("Unsorted list is,") +print(elements) +bubblesort(elements) +print("Sorted Array is, ") +print(elements) From d2a7775139b3769a012f25234e76c952046e76ac Mon Sep 17 00:00:00 2001 From: PriyanshuGupta18 <65297674+PriyanshuGupta18@users.noreply.github.com> Date: Sun, 2 Oct 2022 17:59:10 +0530 Subject: [PATCH 4/4] Create selection_sort.py --- python/selection_sort.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 python/selection_sort.py diff --git a/python/selection_sort.py b/python/selection_sort.py new file mode 100644 index 0000000..7b892a5 --- /dev/null +++ b/python/selection_sort.py @@ -0,0 +1,12 @@ +def selectionSort(array, size): + for ind in range(size): + min_index = ind + for j in range(ind + 1, size): + if array[j] < array[min_index]: + min_index = j + (array[ind], array[min_index]) = (array[min_index], array[ind]) +arr = [-2, 45, 0, 11, -9,88,-97,-202,747] +size = len(arr) +selectionSort(arr, size) +print('The array after sorting in Ascending Order by selection sort is:') +print(arr)