Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. /**
  4.  * Quicksort() quickly sorts an array of integers.
  5.  * @author Richard Nysäter
  6.  * @version 2011-02-21
  7.  */
  8. public class Quicksort {
  9.     /**
  10.      * Sorts the array.
  11.      * @param array the array to be sorted.
  12.      */
  13.     public void QuickSort(int[] array){
  14.         qsort(array, 0, array.length-1);
  15.     }
  16.    
  17.    
  18.     private int partition(int[] arrayz, int first, int last) {
  19.         int low = first, high = last;;
  20.         int tmp;
  21.  
  22.         int pivot = arrayz[first];
  23.  
  24.         while (low <= high) {
  25.             while(arrayz[low] < pivot){
  26.                 low++;
  27.             }
  28.             while(arrayz[high] > pivot){
  29.                 high--;
  30.             }
  31.             if (low < high) {
  32.                 tmp = arrayz[low];
  33.                 arrayz[low] = arrayz[high];
  34.                 arrayz[high] = tmp;
  35.                 low++;
  36.                 high--;
  37.             }
  38.         }
  39.         return low;
  40.     }
  41.  
  42.     private void qsort(int[] array, int first, int last) {
  43.        
  44.             int index = partition(array, first, last);
  45.  
  46.             if(first < index-1){
  47.                 qsort(array, first, index-1);
  48.             }
  49.             if(index < last){
  50.                 qsort(array, index+1, last);
  51.             }
  52.         }
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement