Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created Quick Sort Using python #113

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions 9_quick_sort/quickSort.js
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions 9_quick_sort/quick_sort.py
Original file line number Diff line number Diff line change
@@ -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]),