diff --git a/SortingAlgorithms/QuickSort.js b/SortingAlgorithms/QuickSort.js new file mode 100644 index 0000000..69db83b --- /dev/null +++ b/SortingAlgorithms/QuickSort.js @@ -0,0 +1,39 @@ +function swap(items, leftIndex, rightIndex){ + var temp = items[leftIndex]; + items[leftIndex] = items[rightIndex]; + items[rightIndex] = temp; +} +function partition(items, left, right) { + var pivot = items[Math.floor((right + left) / 2)], //middle element + i = left, //left pointer + j = right; //right pointer + while (i <= j) { + while (items[i] < pivot) { + i++; + } + while (items[j] > pivot) { + j--; + } + if (i <= j) { + swap(items, i, j); //sawpping two elements + i++; + j--; + } + } + return i; +} + +function quickSort(items, left, right) { + var index; + if (items.length > 1) { + index = partition(items, left, right); //index returned from partition + if (left < index - 1) { //more elements on the left side of the pivot + quickSort(items, left, index - 1); + } + if (index < right) { //more elements on the right side of the pivot + quickSort(items, index, right); + } + } + return items; +} +
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: