Quick sort

Quick sort 

When solving a design problem, when should I use merge sort vs quick sort, for instance ? 

Idea : big class, class 1 

public class QuickSort{
  public int[] quickSort(int[] array){
    // sanity check 
    if(array == null) return array;
  
    quickSort(array, 0, array.length - 1);
    return array;
  }
  
  public void quickSort(int[] array, int left, int right){
    // base case 
    if(left >= right) return;
    
    // pivot 
    int pivotPos = partition(array, left, right);
    quickSort(array, left, pivotPos - 1);
    quickSort(array, pivotPos + 1, right);
  }
  
  private int partition(int[] array, int left, int right){
    int pivotIndex = pivotIndex(left, right);
    int pivot = array[pivotIndex];
    
    // swap the pivot element to the right position first
    swap(array, pivotIndex, right);
    
    int leftBound = left;
    int rightBound = right - 1;
    while(leftBound <= rightBound){
      if(array[leftBound] < pivot){
        leftBound++;
      }else if(array[rightBound] >= pivot){
        rightBound--;
      }else{
        swap(array, leftBound++, rightBound--);
      }
    }
    
    //swap back the pivot element  ??
    swap(array, leftBound, right);
    return leftBound;
  }
  
  private int pivotIndex(int left, int right){
    return left + (int)(Math.random() * (right - left + 1));
  }
  
  private void swap(int[] array. int left, int right){
    int tmp = array[left];
    array[left] = array[right];
    array[right] = tmp;
  }
}


public static void main(String[] args){
  QuickSort solution = new QuickSort();
  int[] array = new int[]{4, 3,2, 1};
  array = solution.quickSort(array);
  System.out.println(Arrays.toString(array));
}







Quicksort
Animated visualization of the quicksort algorithm. The horizontal lines are pivot values.    
Class    Sorting algorithm
Worst-case performance    O(n2)
Best-case performance    O(n log n)
Average performance    O(n log n)
Worst-case space complexity    O(n) auxiliary (naive)
O(log n) auxiliary (Sedgewick 1978)
Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order. Developed by Tony Hoare in 1959[1] and published in 1961,[2] it is still a commonly used algorithm for sorting. When implemented well, it can be about two or three times faster than its main competitors, merge sort and heapsort.[3][contradictory]
Quicksort is a comparison sort, meaning that it can sort items of any type for which a "less-than" relation (formally, a total order) is defined. In efficient implementations it is not a stable sort, meaning that the relative order of equal sort items is not preserved. Quicksort can operate in-place on an array, requiring small additional amounts of memory to perform the sorting. It is very similar to selection sort, except that it does not always choose worst-case partition.
Mathematical analysis of quicksort shows that, on average, the algorithm takes O(n log n) comparisons to sort n items. In the worst case, it makes O(n2) comparisons, though this behavior is rare.



Algorithm

Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays.
The steps are:
1. Pick an element, called a pivot, from the array.
2. Partitioning: reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
3. Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.
The base case of the recursion is arrays of size zero or one, which are in order by definition, so they never need to be sorted.
The pivot selection and partitioning steps can be done in several different ways; the choice of specific implementation schemes greatly affects the algorithm's performance.

 

posted on 2018-09-20 18:04  猪猪&#128055;  阅读(177)  评论(0)    收藏  举报

导航