Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

All the sorting algorithm added #971

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions Java/Algorithm/SortingAlgorithm/BucketSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import java.util.ArrayList;
import java.util.Collections;

public class BucketSort {

/*
* Bucket Sort
*
* Bucket sort is a distribution sort algorithm that sorts elements by first
* dividing the range of the input into a number of buckets. Each bucket is then
* sorted individually, either using a different sorting algorithm or by
* recursively applying the bucket sorting algorithm.
*/

// Function to perform Bucket Sort
public void bucketSort(int arr[], int n) {
int maxVal = getMaxValue(arr, n);
int numBuckets = maxVal / 10 + 1;

// Create buckets
ArrayList<Integer>[] buckets = new ArrayList[numBuckets];
for (int i = 0; i < numBuckets; i++) {
buckets[i] = new ArrayList<>();
}

// Distribute elements into buckets
for (int i = 0; i < n; i++) {
int bucketIndex = arr[i] / 10;
buckets[bucketIndex].add(arr[i]);
}

// Sort individual buckets
for (int i = 0; i < numBuckets; i++) {
Collections.sort(buckets[i]);
}

// Concatenate the sorted buckets
int index = 0;
for (int i = 0; i < numBuckets; i++) {
for (int j = 0; j < buckets[i].size(); j++) {
arr[index++] = buckets[i].get(j);
}
}
}

// Utility function to get the maximum value in the array
int getMaxValue(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}

// A utility function to print the array
static void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i) {
System.out.print(arr[i] + " ");
}
System.out.println();
}

public static void main(String args[]) {
int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 };
int n = arr.length;

System.out.println("Unsorted array: ");
printArray(arr, n);

BucketSort bucketSort = new BucketSort();
bucketSort.bucketSort(arr, n);

System.out.println("Sorted array: ");
printArray(arr, n);
}
}
77 changes: 77 additions & 0 deletions Java/Algorithm/SortingAlgorithm/HeapSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import java.util.Arrays;

public class HeapSort {

/*
* Heap Sort
*
* Heap sort is a comparison-based sorting algorithm that builds a binary heap
* from the input array and repeatedly extracts the maximum element from the
* heap and rebuilds the heap after each extraction.
*/

// Function to perform Heap Sort
public void heapSort(int arr[]) {
int n = arr.length;

// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);

// One by one extract an element from heap
for (int i = n - 1; i > 0; i--) {
// Move current root to end
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;

// call max heapify on the reduced heap
heapify(arr, i, 0);
}
}

// To heapify a subtree rooted with node i which is an index in arr[].
// n is size of heap
void heapify(int arr[], int n, int i) {
int largest = i; // Initialize largest as root
int l = 2 * i + 1; // left = 2*i + 1
int r = 2 * i + 2; // right = 2*i + 2

// If left child is larger than root
if (l < n && arr[l] > arr[largest])
largest = l;

// If right child is larger than largest so far
if (r < n && arr[r] > arr[largest])
largest = r;

// If largest is not root
if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;

// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}

// A utility function to print the array
static void printArray(int arr[]) {
System.out.println(Arrays.toString(arr));
}

public static void main(String args[]) {
int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 };
int n = arr.length;

System.out.println("Unsorted array: ");
printArray(arr);

HeapSort heapSort = new HeapSort();
heapSort.heapSort(arr);

System.out.println("Sorted array: ");
printArray(arr);
}
}
49 changes: 49 additions & 0 deletions Java/Algorithm/SortingAlgorithm/InsertionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
public class InsertionSort {

/*
* Insertion Sort
*
* Insertion sort is a simple sorting algorithm that builds the final sorted
* array one item at a time. It repeatedly takes the next element from the
* unsorted part and inserts it into its correct position in the sorted part.
*/

// Function to perform Insertion Sort
void insertionSort(int arr[], int n) {
// Iterate through the array
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;
}
}

// A utility function to print the array
static void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i) {
System.out.print(arr[i] + " ");
}
System.out.println();
}

public static void main(String[] args) {
int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 };
int n = arr.length;

System.out.println("Unsorted array: ");
printArray(arr, n);

InsertionSort insertionSort = new InsertionSort();
insertionSort.insertionSort(arr, n);

System.out.println("Sorted array: ");
printArray(arr, n);
}
}
79 changes: 79 additions & 0 deletions Java/Algorithm/SortingAlgorithm/RadixSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Radix Sort
*
* Radix sort is a non-comparative sorting algorithm that sorts numbers by
* grouping individual digits. It sorts by distributing elements into buckets
* according to their radix (base).
*/

public class RadixSort {

// Function to get the maximum element from the array
int getMax(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}

// Function to perform Counting Sort based on the digit at a specific place value
void countingSort(int arr[], int n, int exp) {
int output[] = new int[n + 1]; // output array
int count[] = new int[10]; // count array for storing occurrences of digits

// Calculate count of occurrences of each digit (0 to 9)
for (int i = 0; i < n; i++) {
count[(arr[i] / exp) % 10]++;
}

// Calculate cumulative frequency (stable for equal digit elements)
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}

// Place elements in sorted order using the count array
for (int i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}

// Copy the sorted elements back to the original array
System.arraycopy(output, 0, arr, 0, n);
}

// The main function to implement Radix Sort
void radixSort(int arr[], int n) {
// Find the maximum element to determine number of passes
int max = getMax(arr, n);

// Do counting sort for every digit in base 10
for (int exp = 1; max / exp > 0; exp *= 10) {
countingSort(arr, n, exp);
}
}

// A utility function to print the array
static void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i) {
System.out.print(arr[i] + " ");
}
System.out.println();
}

public static void main(String[] args) {
int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 };
int n = arr.length;

System.out.println("Unsorted array: ");
printArray(arr, n);

RadixSort radixSort = new RadixSort();
radixSort.radixSort(arr, n);

System.out.println("Sorted array: ");
printArray(arr, n);
}
}
50 changes: 50 additions & 0 deletions Java/Algorithm/SortingAlgorithm/SelectionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
public class SelectionSort {

/*
* Selection Sort
*
* Selection sort is a simple sorting algorithm that repeatedly selects the
* minimum element from the unsorted portion of the array and places it at the
* beginning.
*/

// Function to perform Selection Sort
void selectionSort(int arr[], int n) {
// Iterate through the array
for (int i = 0; i < n - 1; i++) {
// Find the minimum element in the unsorted portion
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}

// A utility function to print the array
static void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i) {
System.out.print(arr[i] + " ");
}
System.out.println();
}

public static void main(String[] args) {
int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 };
int n = arr.length;

System.out.println("Unsorted array: ");
printArray(arr, n);

SelectionSort selectionSort = new SelectionSort();
selectionSort.selectionSort(arr, n);

System.out.println("Sorted array: ");
printArray(arr, n);
}
}