Void Pivot(Int a , Int Left, Int Right)

Void Pivot(Int a , Int Left, Int Right)

<p>Quicksort</p><p> void Swap(int &a, int &b){ int temp = a; a = b; b = temp; }</p><p> void Pivot(int a[], int left, int right){ if (a[left] > a[right]) Swap(a[left], a[right]); }</p><p> int Partition(int a[], int left, int right){ int pivot = a[left]; int i = left; int j = right+1; do{ do{ ++i; }while(a[i] < pivot); do{ --j; }while(a[j] > pivot); if (i < j) Swap(a[i], a[j]); }while (i < j); Swap(a[j], a[left]); return j; }</p><p> void quicksort(int array[], int left, int right){ if (left < right){ Pivot(array, left, right); int k = Partition(array, left, right); quicksort (array, left, k-1); quicksort (array, k+1, right); } }</p><p>C++ Quicksort © 2001 B. Tjaden 1 void printarray(int a[], int n){ for (int i = 0; i < n; i++) cout << a[i] << " "; cout << endl << endl; }</p><p> quicksort has best-case, average-case and arbitrary-case performances of O(nlogn)</p><p> worst case performance o O(n2) o occurs when array is in reverse or nearly reverse sorted order</p><p> quicksort o begins by choosing a pivot point o the list is then rearranged into 3 sublists or partitions . left partition contains elements smaller than array[pivot] . middle partition is array[pivot] . right partition contains elements larger than array[pivot] o left and right sublists are sorted independently . with recursive calls to quicksort o left is the leftmost element in the list or sublist o right is the rightmost element in the list or sublist</p><p>C++ Quicksort © 2001 B. Tjaden 2</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    2 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us