Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

|BucketSort|HeapSort|InsertionSort|radixSort| #446

Open
wants to merge 1 commit into
base: main
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
42 changes: 42 additions & 0 deletions SortingAlgorithmJava/BucketSort.java
Original file line number Diff line number Diff line change
@@ -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<Float>[] buckets = new ArrayList[n];

for (int i = 0; i < n; i++) {
buckets[i] = new ArrayList<Float>();
}

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 + " ");
}
}
}
58 changes: 58 additions & 0 deletions SortingAlgorithmJava/HeapSort.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
26 changes: 26 additions & 0 deletions SortingAlgorithmJava/InsertionSort.java
Original file line number Diff line number Diff line change
@@ -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 + " ");
}
}
}
63 changes: 63 additions & 0 deletions SortingAlgorithmJava/radixSort.java
Original file line number Diff line number Diff line change
@@ -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);
}
}