Skip to content
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
63 changes: 63 additions & 0 deletions Java/BucketSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import java.util.*;

/*
1) Create n empty buckets (Or lists).
2) Do following for every array element arr[i].
.......a) Insert arr[i] into bucket[n*array[i]]
3) Sort individual buckets using insertion sort.
4) Concatenate all sorted buckets.
*/

class BucketSort {
public static int[] bucketSort(int[] array, int bucketCount) {

if (bucketCount <= 0) throw new IllegalArgumentException("Invalid bucket count");
if (array.length <= 1) return array; //trivially sorted
int high = array[0];
int low = array[0];
for (int i = 1; i < array.length; i++) { //find the range of input elements
if (array[i] > high) high = array[i];
if (array[i] < low) low = array[i];
}
double interval = ((double)(high - low + 1))/bucketCount; //range of one bucket
ArrayList<Integer> buckets[] = new ArrayList[bucketCount];
for (int i = 0; i < bucketCount; i++) { //initialize buckets
buckets[i] = new ArrayList();
}
for(int i = 0; i < array.length; i++)
{ //partition the input array
buckets[(int)((array[i] - low)/interval)].add(array[i]);
}
int pointer = 0;
for (int i = 0; i < buckets.length; i++)
{
Collections.sort(buckets[i]); //mergeSort
for (int j = 0; j < buckets[i].size(); j++)
{ //merge the buckets
array[pointer] = buckets[i].get(j);
pointer++;
}

}
return array;

}


public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
int[] a=new int[t];
for(int i=0;i<t;i++)
{
a[i]=sc.nextInt();
}
bucketSort(a,t);
for(int i=0;i<t;i++)
{
System.out.println(a[i]);
}

}
}
81 changes: 81 additions & 0 deletions Java/CountingSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import java.util.Scanner;

/** Class CountingSort **/
public class CountingSort
{
private static final int MAX_RANGE = 1000000;
/** Counting Sort function **/
public static void sort( int[] arr )
{
int N = arr.length;
if (N == 0)
return;
/** find max and min values **/
int max = arr[0], min = arr[0];
for (int i = 1; i < N; i++)
{
if (arr[i] > max)
max = arr[i];
if (arr[i] < min)
min = arr[i];
}
int range = max - min + 1;

/** check if range is small enough for count array **/
/** else it might give out of memory exception while allocating memory for array **/
if (range > MAX_RANGE)
{
System.out.println("\nError : Range too large for sort");
return;
}

int[] count = new int[range];
/** make count/frequency array for each element **/
for (int i = 0; i < N; i++)
count[arr[i] - min]++;
/** modify count so that positions in final array is obtained **/
for (int i = 1; i < range; i++)
count[i] += count[i - 1];
/** modify original array **/
int j = 0;
for (int i = 0; i < range; i++)
while (j < count[i])
arr[j++] = i + min;
}
/** Main method **/
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
System.out.println("Counting Sort Test\n");
int n, i;
/** Accept number of elements **/
System.out.println("Enter number of integer elements");
n = scan.nextInt();
/** Create integer array on n elements **/
int arr[] = new int[ n ];
/** Accept elements **/
System.out.println("\nEnter "+ n +" integer elements");
for (i = 0; i < n; i++)
arr[i] = scan.nextInt();
/** Call method sort **/
sort(arr);
/** Print sorted Array **/
System.out.println("\nElements after sorting ");
for (i = 0; i < n; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
}



// Counting Sort Test

// Enter number of integer elements
// 20

// Enter 20 integer elements
// 54 67 13 24 76 37 97 10 67 24 6 28 5 19 63 1 71 83 97 24

// Elements after sorting
// 1 5 6 10 13 19 24 24 24 28 37 54 63 67 67 71 76 83 97 97
58 changes: 58 additions & 0 deletions Java/CycleSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.util.*;
public class CycleSort
{
static void sort(int a[], int n)
{
int writes = 0,start,element,pos,temp,i;

for (start = 0; start <= n - 2; start++) {
element = a[start];
pos = start;
for (i = start + 1; i < n; i++)
if (a[i] < element)
pos++;
if (pos == start)
continue;
while (element == a[pos])
pos += 1;
if (pos != start) {
//swap(element, a[pos]);
temp = element;
element = a[pos];
a[pos] = temp;
writes++;
}
while (pos != start) {
pos = start;
for (i = start + 1; i < n; i++)
if (a[i] < element)
pos += 1;
while (element == a[pos])
pos += 1;
if (element != a[pos]) {
temp = element;
element = a[pos];
a[pos] = temp;
writes++;
}
}
}
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of elements: ");
int n1=sc.nextInt();
int[] a=new int[n1];
for(int i=0;i<n1;i++)
{
a[i]=sc.nextInt();
}
int n = a.length,i;
sort(a, n);
System.out.println("After sort, array : ");
for (i = 0; i < n; i++)
System.out.println(a[i]);

}
}