Skip to content
Closed
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
86 changes: 86 additions & 0 deletions src/com/jwetherell/algorithms/sorts/BitonicSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* Java program for Bitonic Sort. Note that this program
works only when size of input is a power of 2. */
public class BitonicSort
{
/* The parameter dir indicates the sorting direction,
ASCENDING or DESCENDING; if (a[i] > a[j]) agrees
with the direction, then a[i] and a[j] are
interchanged. */
void compAndSwap(int a[], int i, int j, int dir)
{
if ( (a[i] > a[j] && dir == 1) ||
(a[i] < a[j] && dir == 0))
{
// Swapping elements
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}

/* It recursively sorts a bitonic sequence in ascending
order, if dir = 1, and in descending order otherwise
(means dir=0). The sequence to be sorted starts at
index position low, the parameter cnt is the number
of elements to be sorted.*/
void bitonicMerge(int a[], int low, int cnt, int dir)
{
if (cnt>1)
{
int k = cnt/2;
for (int i=low; i<low+k; i++)
compAndSwap(a,i, i+k, dir);
bitonicMerge(a,low, k, dir);
bitonicMerge(a,low+k, k, dir);
}
}

/* This funcion first produces a bitonic sequence by
recursively sorting its two halves in opposite sorting
orders, and then calls bitonicMerge to make them in
the same order */
void bitonicSort(int a[], int low, int cnt, int dir)
{
if (cnt>1)
{
int k = cnt/2;

// sort in ascending order since dir here is 1
bitonicSort(a, low, k, 1);

// sort in descending order since dir here is 0
bitonicSort(a,low+k, k, 0);

// Will merge wole sequence in ascending order
// since dir=1.
bitonicMerge(a, low, cnt, dir);
}
}

/*Caller of bitonicSort for sorting the entire array
of length N in ASCENDING order */
void sort(int a[], int N, int up)
{
bitonicSort(a, 0, N, up);
}

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

// Driver method
public static void main(String args[])
{
int a[] = {3, 7, 4, 8, 6, 2, 1, 5};
int up = 1;
BitonicSort ob = new BitonicSort();
ob.sort(a, a.length,up);
System.out.println("\nSorted array");
printArray(a);
}
}
59 changes: 59 additions & 0 deletions src/com/jwetherell/algorithms/sorts/BogoSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Java Program to implement BogoSort
public class BogoSort
{
// Sorts array a[0..n-1] using Bogo sort
void bogoSort(int[] a)
{
// if array is not sorted then shuffle the
// array again
while (isSorted(a) == false)
shuffle(a);
}

// To generate permuatation of the array
void shuffle(int[] a)
{
// Math.random() returns a double positive
// value, greater than or equal to 0.0 and
// less than 1.0.
for (int i=1; i <= n; i++)
swap(a, i, (int)(Math.random()*i));
}

// Swapping 2 elements
void swap(int[] a, int i, int j)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}

// To check if array is sorted or not
boolean isSorted(int[] a)
{
for (int i=1; i<a.length; i++)
if (a[i] < a[i-1])
return false;
return true;
}

// Prints the array
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)
{
//Enter array to be sorted here
int[] a = {3, 2, 5, 1, 0, 4};
BogoSort ob = new BogoSort();

ob.bogoSort(a);

System.out.print("Sorted array: ");
ob.printArray(a);
}
}
50 changes: 50 additions & 0 deletions src/com/jwetherell/algorithms/sorts/BucketSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/*
* Java Program sort an integer array using radix sort algorithm.
* input: [80, 50, 30, 10, 90, 60, 0, 70, 40, 20, 50]
* output: [0, 10, 20, 30, 40, 50, 50, 60, 70, 80, 90]
* Time Complexity of Solution:
* Best Case O(n); Average Case O(n); Worst Case O(n^2).
*/
public class BucketSort {
public static void main(String[] args) {
System.out.println("Bucket sort in Java");
int[] input = { 80, 50, 30, 10, 90, 60, 0, 70, 40, 20, 50 };
System.out.println("integer array before sorting");
System.out.println(Arrays.toString(input)); // sorting array using radix Sort Algorithm
bucketSort(input);
System.out.println("integer array after sorting using bucket sort algorithm");
System.out.println(Arrays.toString(input));
}
/** * * @param input */
public static void bucketSort(int[] input) { // get hash codes
final int[] code = hash(input); // create and initialize buckets to ArrayList: O(n)
List[] buckets = new List[code[1]];
for (int i = 0; i < code[1]; i++) {
buckets[i] = new ArrayList();
} // distribute data into buckets: O(n)
for (int i : input) {
buckets[hash(i, code)].add(i); } // sort each bucket O(n)
for (List bucket : buckets) {
Collections.sort(bucket);
}
int ndx = 0; // merge the buckets: O(n)
for (int b = 0; b < buckets.length; b++) {
for (int v : buckets[b]) {
input[ndx++] = v; }
}
} /** * * @param input * @return an array containing hash of input */
private static int[] hash(int[] input) {
int m = input[0];
for (int i = 1; i < input.length; i++) {
if (m < input[i]) {
m = input[i]; }
} return new int[] { m, (int) Math.sqrt(input.length) };
} /** * * @param i * @param code * @return */
private static int hash(int i, int[] code) {
return (int) ((double) i / code[0] * (code[1] - 1));
}
}
74 changes: 74 additions & 0 deletions src/com/jwetherell/algorithms/sorts/CocktailSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Java program for implementation of Cocktail Sort
public class CocktailSort {
void cocktailSort(int a[])
{
boolean swapped = true;
int start = 0;
int end = a.length;

while (swapped == true) {
// reset the swapped flag on entering the
// loop, because it might be true from a
// previous iteration.
swapped = false;

// loop from bottom to top same as
// the bubble sort
for (int i = start; i < end - 1; ++i) {
if (a[i] > a[i + 1]) {
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
swapped = true;
}
}

// if nothing moved, then array is sorted.
if (swapped == false)
break;

// otherwise, reset the swapped flag so that it
// can be used in the next stage
swapped = false;

// move the end point back by one, because
// item at the end is in its rightful spot
end = end - 1;

// from top to bottom, doing the
// same comparison as in the previous stage
for (int i = end - 1; i >= start; i--) {
if (a[i] > a[i + 1]) {
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
swapped = true;
}
}

// increase the starting point, because
// the last stage would have moved the next
// smallest number to its rightful spot.
start = start + 1;
}
}

/* Prints the array */
void printArray(int a[])
{
int n = a.length;
for (int i = 0; i < n; i++)
System.out.print(a[i] + " ");
System.out.println();
}

// Driver method
public static void main(String[] args)
{
CocktailSort ob = new CocktailSort();
int a[] = { 5, 1, 4, 2, 8, 0, 2 };
ob.cocktailSort(a);
System.out.println("Sorted array");
ob.printArray(a);
}
}
67 changes: 67 additions & 0 deletions src/com/jwetherell/algorithms/sorts/CombSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Java program for implementation of Comb Sort
class CombSort
{
// To find gap between elements
int getNextGap(int gap)
{
// Shrink gap by Shrink factor
gap = (gap*10)/13;
if (gap < 1)
return 1;
return gap;
}

// Function to sort arr[] using Comb Sort
void sort(int arr[])
{
int n = arr.length;

// initialize gap
int gap = n;

// Initialize swapped as true to make sure that
// loop runs
boolean swapped = true;

// Keep running while gap is more than 1 and last
// iteration caused a swap
while (gap != 1 || swapped == true)
{
// Find next gap
gap = getNextGap(gap);

// Initialize swapped as false so that we can
// check if swap happened or not
swapped = false;

// Compare all elements with current gap
for (int i=0; i<n-gap; i++)
{
if (arr[i] > arr[i+gap])
{
// Swap arr[i] and arr[i+gap]
int temp = arr[i];
arr[i] = arr[i+gap];
arr[i+gap] = temp;

// Set swapped
swapped = true;
}
}
}
}

// Driver method
public static void main(String args[])
{
CombSort ob = new CombSort();
int arr[] = {8, 4, 1, 56, 3, -44, 23, -6, 28, 0};
ob.sort(arr);

System.out.println("sorted array");
for (int i=0; i<arr.length; ++i)
System.out.print(arr[i] + " ");

}
}
/* This code is contributed by Rajat Mishra */
Loading