Skip to content

Instantly share code, notes, and snippets.

@rogerwschmidt
Last active September 1, 2017 14:26
Show Gist options
  • Select an option

  • Save rogerwschmidt/3973a97ea66775b4635adb8d32304ae8 to your computer and use it in GitHub Desktop.

Select an option

Save rogerwschmidt/3973a97ea66775b4635adb8d32304ae8 to your computer and use it in GitHub Desktop.

Advanced sorting instructor notes

  • Implement a merge sort algorithm in Javascript
  • Implement a quick sort algorithm in Javascript

Implement a merge sort algorithm in Javascript

  • Manually work through the merge sort algorithm.
  • Implement merge sort.

Implement a quick sort algorithm in Javascript

  • Manually work through the quick sort algorithm.
  • Implement quick sort.
function partition(arr, left, right) {
  let pivotValue = arr[left];
  let partitionIndex = left;

  for (let i = left + 1; i <= right; i++) {
    if (arr[i] < pivotValue) {
      partitionIndex++;
      swap(arr, i, partitionIndex);
    }
  }

  swap(arr, left, partitionIndex);
  return partitionIndex;
}

function quickSort(arr, left=0, right=arr.length - 1) {

  if (left < right) {
    let partitionIndex = partition(arr, left, right);
    quickSort(arr, left, partitionIndex - 1);
    quickSort(arr, partitionIndex + 1, right);
  }

  return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment