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); + } +} 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) 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) 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)