diff --git a/SortingAlgorithmJava/BucketSort.java b/SortingAlgorithmJava/BucketSort.java new file mode 100644 index 00000000..a70f6fdb --- /dev/null +++ b/SortingAlgorithmJava/BucketSort.java @@ -0,0 +1,42 @@ +import java.util.ArrayList; +import java.util.Collections; + +public class BucketSort { + + public static void bucketSort(float[] arr, int n) { + if (n <= 0) + return; + + ArrayList[] buckets = new ArrayList[n]; + + for (int i = 0; i < n; i++) { + buckets[i] = new ArrayList(); + } + + for (int i = 0; i < n; i++) { + int bucketIndex = (int) arr[i] * n; + buckets[bucketIndex].add(arr[i]); + } + + for (int i = 0; i < n; i++) { + Collections.sort(buckets[i]); + } + + int index = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < buckets[i].size(); j++) { + arr[index++] = buckets[i].get(j); + } + } + } + + public static void main(String[] args) { + float[] arr = { (float) 0.897, (float) 0.565, (float) 0.656, (float) 0.1234, (float) 0.665, (float) 0.3434 }; + int n = arr.length; + bucketSort(arr, n); + + for (float el : arr) { + System.out.print(el + " "); + } + } +} diff --git a/SortingAlgorithmJava/HeapSort.java b/SortingAlgorithmJava/HeapSort.java new file mode 100644 index 00000000..8fff10eb --- /dev/null +++ b/SortingAlgorithmJava/HeapSort.java @@ -0,0 +1,58 @@ +import java.util.Arrays; + +class HeapSort { + + static void heapify(int arr[], int n, int i) { + + int largest = i; + + int l = 2 * i + 1; + + int r = 2 * i + 2; + + if (l < n && arr[l] > arr[largest]) { + largest = l; + } + + if (r < n && arr[r] > arr[largest]) { + largest = r; + } + if (largest != i) { + int temp = arr[i]; + arr[i] = arr[largest]; + arr[largest] = temp; + + heapify(arr, n, largest); + } + } + + static void heapSort(int arr[]) { + int n = arr.length; + + for (int i = n / 2 - 1; i >= 0; i--) { + heapify(arr, n, i); + } + + for (int i = n - 1; i > 0; i--) { + + int temp = arr[0]; + arr[0] = arr[i]; + arr[i] = temp; + heapify(arr, i, 0); + } + } + + static void printArray(int arr[]) { + for (int i = 0; i < arr.length; i++) { + System.out.print(arr[i] + " "); + } + System.out.println(); + } + + public static void main(String args[]) { + int arr[] = { 9, 4, 3, 8, 10, 2, 5 }; + heapSort(arr); + System.out.println("Sorted array is "); + printArray(arr); + } +} \ No newline at end of file diff --git a/SortingAlgorithmJava/InsertionSort.java b/SortingAlgorithmJava/InsertionSort.java new file mode 100644 index 00000000..59613008 --- /dev/null +++ b/SortingAlgorithmJava/InsertionSort.java @@ -0,0 +1,26 @@ +public class InsertionSort { + public static void insertionSort(int[] arr) { + int n = arr.length; + for (int i = 1; i < n; i++) { + int key = arr[i]; + int j = i - 1; + + // Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position + while (j >= 0 && arr[j] > key) { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = key; + } + } + + public static void main(String[] args) { + int[] arr = {12, 11, 13, 5, 6}; + insertionSort(arr); + + System.out.println("Sorted array:"); + for (int i : arr) { + System.out.print(i + " "); + } + } +} diff --git a/SortingAlgorithmJava/radixSort.java b/SortingAlgorithmJava/radixSort.java new file mode 100644 index 00000000..1e6a28e4 --- /dev/null +++ b/SortingAlgorithmJava/radixSort.java @@ -0,0 +1,63 @@ +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]; + 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 printSortedElemets(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 }; + radixSort(arr, arr.length); + printSortedElemets(arr, arr.length); + } +} \ No newline at end of file