GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java/Sorts/QuickSort.java at master · feinoah/Java Expand file tree/ Copy pathQuickSort.javaMore file actions 97 lines (78 loc) · 2.3 KB / Copy pathQuickSort.javaFile metadata and controls97 lines (78 loc) · 2.3 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 package Sorts; import static Sorts.SortUtils.*; /** * * @author Varun Upadhyay (https://github.com/varunu28) * @author Podshivalov Nikita (https://github.com/nikitap492) * * * @see SortAlgorithm * */ class QuickSort implements SortAlgorithm { /** * This method implements the Generic Quick Sort * * @param array The array to be sorted * Sorts the array in increasing order **/ @Override public <T extends Comparable<T>> T[] sort(T[] array) { doSort(array, 0, array.length - 1); return array; } /** * The sorting process * * @param left The first index of an array * @param right The last index of an array * @param array The array to be sorted * **/ private static <T extends Comparable<T>> void doSort(T[] array, int left, int right) { if (left < right) { int pivot = partition(array, left, right); doSort(array, left, pivot - 1); doSort(array, pivot , right); } } /** * This method finds the partition index for an array * * @param array The array to be sorted * @param left The first index of an array * @param right The last index of an array * Finds the partition index of an array **/ private static <T extends Comparable<T>> int partition(T[] array, int left, int right) { int mid = (left + right) / 2; T pivot = array[mid]; while(left <= right) { while(less(array[left], pivot)){ ++left; } while(less(pivot, array[right])) { --right; } if(left <= right) { swap(array, left, right); ++left; --right; } } return left; } // Driver Program public static void main(String[] args) { // For integer input Integer[] array = {3, 4, 1, 32, 0, 1, 5, 12 ,2, 5 ,7 ,8 ,9, 2, 44, 111, 5}; QuickSort quickSort = new QuickSort(); // quickSort.sort(array); //Output => 0 1 1 2 2 3 4 5 5 5 7 8 9 12 32 44 111 print(array); String[] stringArray = {"c", "a", "e", "b", "d"}; quickSort.sort(stringArray); //Output => a b c d e print(stringArray); } } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |