realdev22 / quick-sort

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

quick-sort

Challenge Summary

Quick Sorts use a partition value similar to a merge sort in that it divides and conquers.

Challenge Description

Follow the Pseudo code to create and test a working quickSort algorithm

Approach & Efficiency

  • Partition the array by defining a Pivot value.
  • Sort everything less than the Pivot to the left, and everything greater to the right.
  • Recursively Sort the array using Partition with a Pivot point one value to the left.

Solution

ALGORITHM QuickSort(arr, left, right) if left < right // Partition the array by setting the position of the pivot value DEFINE position <-- Partition(arr, left, right) // Sort the left QuickSort(arr, left, position - 1) // Sort the right QuickSort(arr, position + 1, right)

ALGORITHM Partition(arr, left, right) // set a pivot value as a point of reference DEFINE pivot <-- arr[right] // create a variable to track the largest index of numbers lower than the defined pivot DEFINE low <-- left - 1 for i <- left to right do if arr[i] <= pivot low++ Swap(arr, i, low)

 // place the value of the pivot location in the middle.
 // all numbers smaller than the pivot are on the left, larger on the right. 
 Swap(arr, right, low + 1)
// return the pivot index point
 return low + 1

ALGORITHM Swap(arr, i, low) DEFINE temp; temp <-- arr[i] arr[i] <-- arr[low] arr[low] <-- temp

About


Languages

Language:JavaScript 100.0%