Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions java/sorting/Bucket_Sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Java program to sort an array using bucket sort
import java.util.*;
import java.util.Collections;

class GFG {

// Function to sort arr[] of size n using bucket sort
static void bucketSort(float arr[], int n)
{
if (n <= 0)
return;

@SuppressWarnings("unchecked")
Vector<Float>[] buckets = new Vector[n];

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


for (int i = 0; i < n; i++) {
float idx = arr[i] * n;
buckets[(int)idx].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);
}
}
}

// Driver code
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);

System.out.println("Sorted array is ");
for (float el : arr) {
System.out.print(el + " ");
}
}
}

35 changes: 35 additions & 0 deletions java/sorting/Gnome_sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Java Program to implement Gnome Sort

import java.util.Arrays;
public class GFG {
static void gnomeSort(int arr[], int n)
{
int index = 0;

while (index < n) {
if (index == 0)
index++;
if (arr[index] >= arr[index - 1])
index++;
else {
int temp = 0;
temp = arr[index];
arr[index] = arr[index - 1];
arr[index - 1] = temp;
index--;
}
}
return;
}

// Driver program to test above functions.
public static void main(String[] args)
{
int arr[] = { 34, 2, 10, -9 };

gnomeSort(arr, arr.length);

System.out.print("Sorted sequence after applying Gnome sort: ");
System.out.println(Arrays.toString(arr));
}
}