Demo entry 6623328

ABC

   

Submitted by anonymous on Jun 07, 2017 at 12:55
Language: Swift. Code size: 716 Bytes.

func quicksort( arr: inout [Int], left: Int, right: Int) {
	if left < right {
		let p = partion(arr: &arr, left: left, right: right)
		quicksort(arr: &arr, left: left, right: p - 1)
		quicksort(arr: &arr, left: p + 1, right: right)
	}
}

func swap( arr: inout [Int], a: Int, b: Int) {
	let temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp
}

func partion( arr: inout [Int], left: Int, right: Int) -> Int {
	let pivotValue = arr[right]
	var storeIndex = left
	for i in left..<right {
		if arr[i] < pivotValue {
			swap(arr: &arr, a: i, b: storeIndex)
			storeIndex += 1
		}
	}
	swap(arr: &arr, a: storeIndex, b: right)
	return storeIndex
}

var test = [1, 3, 2, 5, 4]
quicksort(arr: &test, left: 0, right: 4)
dump(test)

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).