package hw5; import java.util.Arrays; public class BubbleSort implements Sorter { public static void main(String[] args) { Comparable[] a = {5, 1, 7, 8, 2, 3, 10, 12, 3, 4, 5, 7, 9, 8}; BubbleSort b = new BubbleSort(); b.sort(a); System.out.println("final"); System.out.println(Arrays.deepToString(a)); } @Override public void sort(Comparable[] a) { for (int i = 0; i < a.length - 1; i++) { for (int j = 0; j < a.length - 1 - i; j++) { if (larger(a[j], a[j+1])) { exch(a, j, j+1); } } System.out.println(Arrays.deepToString(a)); } } private static boolean larger(Comparable v, Comparable w) { return v.compareTo(w) > 0; } } // Modified from Algorithms, 4th edition Sedgewick private static void exch(Comparable[] a, int i, int j) { Comparable t = a[i]; a[i] = a[j]; a[j] = t; } package hw5; public interface Sorter<Item extends Comparable<Item>> { void sort(Item[] a); } package hw5; import java.util.Arrays; public class BubbleSort implements Sorter { public static void main(String[] args) { Comparable[] a = {5, 1, 7, 8, 2, 3, 10, 12, 3, 4, 5, 7, 9, 8}; BubbleSort b = new BubbleSort(); b.sort(a); System.out.println("final"); System.out.println(Arrays.deepToString(a)); } @Override public void sort(Comparable[] a) { for (int i = 0; i < a.length - 1; i++) { for (int j = 0; j < a.length - 1 - i; j++) { if (larger(a[j], a[j+1])) { exch(a, j, j+1); } } System.out.println(Arrays.deepToString(a)); } } private static boolean larger(Comparable v, Comparable w) { return v.compareTo(w) > 0; } // Modified from Algorithms, 4th edition Sedgewick private static void exch(Comparable[] a, int i, int j) { Comparable t = a[i]; a[i] = a[j]; a[j] = t; } } package hw5; public class LinkedList<Item extends Comparable<Item>> { public Node head; public String printLinkedList() { StringBuilder sb = new StringBuilder(); sb.append("["); Node current = head; while (current.next != null) { sb.append(current.item + ", "); current = current.next; } sb.append(current.item); sb.append("]"); } return sb.toString(); } package hw5; public class Node<Item extends Comparable<Item>> { Item item; Node next; public Node() { } public Node(Item item) { this.item = item; } @Override public String toString() { return "Node{" + "item=" + item + '}'; } public int compareTo(Node<Item> o) { return this.item.compareTo(o.item); } } // TODO: Need to complete (still a WIP) public class Quick3WaySorter<Item extends Comparable<Item>> implements Sorter<Item> { Item[] a; private void exch(int i, int j) { Item temp = a[i]; a[i] = a[j]; a[j] = temp; } private void sort(int lo, int hi) { // TODO: finish the method } public void sort(Item[] a) { this.a = a; sort(0, a.length-1); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a string of characters to sort: "); String userInput = scanner.nextLine(); scanner.close(); Character[] array = new Character[userInput.length()]; for (int i = 0; i < array.length; i++) { array[i] = userInput.charAt(i); } Quick3WaySorter<Character> quick3WaySorter = new Quick3WaySorter<Character>(); quick3WaySorter.sort(array); } } // TODO: Need to complete (still a WIP) public class QuickSorterWithFewerRecursiveCalls<Item extends Comparable<Item>> { Item[] a; private boolean less(Item v, Item w) { return (v.compareTo(w) < 0); } private void exch(int i, int j) { Item temp = a[i]; a[i] = a[j]; a[j] = temp; } private int partition(int lo, int hi) { int i = lo; // note, this will get incremented to (lo+1) before it is used int j = hi+1; // similarly, this will get decremented to hi before it is used while (true) { while (less(a[++i], a[lo])) item on left to swap if (i == hi) break; //find position of while (less(a[lo], a[--j])) item on right to swap if (j == lo) break; //find position of if (i >= j) break; (positions i and j) cross and we are //ready for final exchange //check if arrows exch(i, j); positions i and j } //swap the elements at in place } exch(lo, j); // final exchange (with pivot) return j; // return index of item now known to be public void sort1(Item[] a) { this.a = a; sort1(0,a.length-1); } private void sort1(int lo, int hi) { } // TODO: Finish the sort1 function public void sort2(Item[] a) { this.a = a; sort2(0,a.length-1); } private void sort2(int lo, int hi) { Stack<Integer> s = new Stack<Integer>(); // TODO: finish the sorting function } public static void main(String[] args) { Character[] a = {'K','R','A','T','E','L','E','P','U','I','M','Q','C','X','O','S' }; Character[] b = {'K','R','A','T','E','L','E','P','U','I','M','Q','C','X','O','S' }; QuickSorterWithFewerRecursiveCalls<Character> sorter = new QuickSorterWithFewerRecursiveCalls<Character>(); sorter.sort1(a); System.out.println("Sorted (w/ 1 recursive call): " + Arrays.toString(a)); sorter.sort2(b); System.out.println("Sorted (iterative quicksort): " + Arrays.toString(b)); }