diff --git a/src/com/jwetherell/algorithms/sorts/BitonicSort.java b/src/com/jwetherell/algorithms/sorts/BitonicSort.java new file mode 100644 index 00000000..650aa13a --- /dev/null +++ b/src/com/jwetherell/algorithms/sorts/BitonicSort.java @@ -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; i1) + { + 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 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); + } +} diff --git a/src/com/jwetherell/algorithms/sorts/CombSort.java b/src/com/jwetherell/algorithms/sorts/CombSort.java new file mode 100644 index 00000000..b99ddb85 --- /dev/null +++ b/src/com/jwetherell/algorithms/sorts/CombSort.java @@ -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 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 - * Family: Counting.
- * Space: An Array of length r.
- * Stable: True.
- *

- * Average case = O(n+r)
- * Worst case = O(n+r)
- * Best case = O(n+r)
- *

- * NOTE: r is the range of numbers (0 to r) to be sorted. - *

- * @see Counting Sort (Wikipedia) - *
- * @author Justin Wetherell - */ -public class CountingSort { - - private CountingSort() { } - - public static Integer[] sort(Integer[] unsorted) { - int maxValue = findMax(unsorted); - int[] counts = new int[maxValue + 1]; - updateCounts(unsorted, counts); - populateCounts(unsorted, counts); - return unsorted; - } - - private static int findMax(Integer[] unsorted) { - int max = Integer.MIN_VALUE; - for (int i : unsorted) { - if (i > max) - max = i; - } - return max; - } - - private static void updateCounts(Integer[] unsorted, int[] counts) { - for (int e : unsorted) - counts[e]++; - } - - private static void populateCounts(Integer[] unsorted, int[] counts) { - int index = 0; - for (int i = 0; i < counts.length; i++) { - int e = counts[i]; - while (e > 0) { - unsorted[index++] = i; - e--; - } - } - } -} +// Java implementation of Counting Sort +class CountingSort +{ + void sort(char arr[]) + { + int n = arr.length; + + // The output character array that will have sorted arr + char output[] = new char[n]; + + // Create a count array to store count of inidividul + // characters and initialize count array as 0 + int count[] = new int[256]; + for (int i=0; i<256; ++i) + count[i] = 0; + + // store count of each character + for (int i=0; i=0; i--) + { + output[count[arr[i]]-1] = arr[i]; + --count[arr[i]]; + } + + // Copy the output array to arr, so that arr now + // contains sorted characters + for (int i = 0; i adj[]; // Adjacency List + + //Constructor + TopologicalSort(int v) { + V = v; + adj = new LinkedList[v]; + for (int i=0; i it = adj[v].iterator(); + while (it.hasNext()) + { + i = it.next(); + if (!visited[i]) + topologicalSortUtil(i, visited, stack); + } + + // Push current vertex to stack which stores result + stack.push(new Integer(v)); + } + + // The function to do Topological Sort. It uses + // recursive topologicalSortUtil() + void topologicalSort() + { + Stack stack = new Stack(); + + // Mark all the vertices as not visited + boolean visited[] = new boolean[V]; + for (int i = 0; i < V; i++) + visited[i] = false; + + // Call the recursive helper function to store + // Topological Sort starting from all vertices + // one by one + for (int i = 0; i < V; i++) + if (visited[i] == false) + topologicalSortUtil(i, visited, stack); + + // Print contents of stack + while (stack.empty()==false) + System.out.print(stack.pop() + " "); + } + + // Driver method + public static void main(String args[]) + { + // Create a graph given in the above diagram + TopologicalSort g = new TopologicalSort(6); + g.addEdge(5, 2); + g.addEdge(5, 0); + g.addEdge(4, 0); + g.addEdge(4, 1); + g.addEdge(2, 3); + g.addEdge(3, 1); + + System.out.println("Following is a Topological " + "sort of the given graph"); + g.topologicalSort(); + } +}