Quick Sort


1. Python

arr = [22,9,3,5,8,7,6,18,4,100,12]

def quickSort(arr,low,high):
   if low < high:
     pi = partition(arr,low,high)
     quickSort(arr,low, pi-1)
     quickSort(arr,pi+1, high)

def partition(arr,low,high):
    i = (low - 1)
    pivot = arr[high]
    for j in range(low, high):
        if arr[j] <= pivot:
           i = i+1
           arr[i], arr[j] = arr[j], arr[i]

    arr[i+1], arr[high] = arr[high], arr[i+1]
    return i+1

def callQuickSort():
    quickSort(arr,0,len(arr)-1)
    print(arr)

callQuickSort()

2.Swift

var inputArray = [22,9,3,5,8,7,6,18,4]
func callQuickSort() {
quickSort(arr:&inputArray,low: 0, high:inputArray.count-1)
print(inputArray)
}

func quickSort( arr:inout [Int],low:Int,high:Int){
if low Int{
var i = low – 1
let pivot = tempArr[high]

for j in low ..< high{
if tempArr[j] <= pivot{
i = i + 1
let temp = tempArr[i]
tempArr[i] = tempArr[j]
tempArr[j] = temp
}
}

let temp = tempArr[i+1]
tempArr[i+1] = tempArr[high]
tempArr[high] = temp
print(tempArr)
return i + 1
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: