From 429f85b273c86d933a1ce888f08fdc32e1a1b56a Mon Sep 17 00:00:00 2001 From: AashifAhamed Date: Sat, 5 Oct 2019 23:58:20 +0530 Subject: [PATCH 1/2] Create quick_sort.py Also added a test code --- 9_quick_sort/quick_sort.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 9_quick_sort/quick_sort.py diff --git a/9_quick_sort/quick_sort.py b/9_quick_sort/quick_sort.py new file mode 100644 index 0000000..9f91d9e --- /dev/null +++ b/9_quick_sort/quick_sort.py @@ -0,0 +1,38 @@ +# Python program for implementation of Quicksort Sort +def partition(arr,low,high): + i = ( low-1 ) # index of smaller element + pivot = arr[high] # pivot + + for j in range(low , high): + + # If current element is smaller than the pivot + if arr[j] < pivot: + + # increment index of smaller element + i = i+1 + arr[i],arr[j] = arr[j],arr[i] + + arr[i+1],arr[high] = arr[high],arr[i+1] + return ( i+1 ) + +# Function to do Quick sort +def quickSort(arr,low,high): + if low < high: + + # pi is partitioning index, arr[p] is now + # at right place + pi = partition(arr,low,high) + + # Separately sort elements before + # partition and after partition + quickSort(arr, low, pi-1) + quickSort(arr, pi+1, high) + +# Driver code to test above +# Uncomment lines below this +arr = [10, 7, 8, 9, 1, 5] +n = len(arr) +quickSort(arr,0,n-1) +print ("Sorted array is:") +for i in range(n): + print ("%d" %arr[i]), From 99b44bbae07abcfe49f146a7ba895c913594313d Mon Sep 17 00:00:00 2001 From: AashifAhamed Date: Sat, 19 Oct 2019 18:46:50 +0530 Subject: [PATCH 2/2] JavaScript quickSort added JavaScript quickSort added and also tested --- 9_quick_sort/quickSort.js | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 9_quick_sort/quickSort.js diff --git a/9_quick_sort/quickSort.js b/9_quick_sort/quickSort.js new file mode 100644 index 0000000..f2c61b1 --- /dev/null +++ b/9_quick_sort/quickSort.js @@ -0,0 +1,41 @@ +const swap = (items, leftIndex, rightIndex) => { + const temp = items[leftIndex] + items[leftIndex] = items[rightIndex] + items[rightIndex] = temp +} + +const partition = (items, left, right) => { + let pivot = items[Math.floor((right + left) / 2)] + let i = left + let j = right + while (i <= j) { + while (items[i] < pivot) { + i++ + } + while (items[j] > pivot) { + j-- + } + if (i <= j) { + swap(items, i, j) + i++ + j-- + } + } + return i +} + +const quickSort = (items, left, right) => { + let index + if (items.length > 1) { + index = partition(items, left, right) + if (left < index - 1) { + quickSort(items, left, index - 1) + } + if (index < right) { + quickSort(items, index, right) + } + } + return items +} + +export default quickSort