Advertisement
Guest User

quicksortxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

a guest
Oct 17th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. package ad.lab2.problem1;
  2.  
  3. public class Quicksort {
  4.  
  5.     public static void sort(Comparable[] array) {
  6.         sort(array, 0, array.length - 1);
  7.     }
  8.    
  9.     static void sort(Comparable[] array, int low, int high) {
  10.         if(low < high) {
  11.             int pivot = partition(array, low, high);
  12.            
  13.             sort(array, low, pivot - 1);
  14.             sort(array, pivot + 1, high);
  15.         }
  16.     }
  17.    
  18.     static int partition(Comparable[] array, int low, int high) {
  19.         Comparable pivot = array[high];
  20.         int i = low - 1;
  21.         for (int j = low; j < high; j++) {
  22.             if (lessEqual(array[j], pivot)) {
  23.                 i++;
  24.                 exchange(array, i, j);
  25.             }
  26.         }
  27.         exchange(array, i + 1, high);
  28.         return i + 1;
  29.     }
  30.    
  31.     private static boolean lessEqual(Comparable v, Comparable w) {
  32.         return (v.compareTo(w) <+ 0);
  33.     }
  34.    
  35.     private static void exchange(Comparable[] array, int i, int j) {
  36.         Comparable t = array[i];
  37.         array[i] = array[j];
  38.         array[j] = t;
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement