Quick Sort is based on divide and conquer algorithmic technique, similar to Merge Sort. In this tutorial, we will be looking at Quick Sort Kotlin Implementation.
How Quick Sort Works?
In Quick Sort, we will consider a random element of an array as pivot and partition the array into two – left part and right part. The main essence of Quick sort is as follows: If an element is considered to be sorted, then all the other elements which are left to it should be less than that element and all the right elements must be greater than it.
Related Links
To understand the above, let us take an example of [1, 2, 3, 4, 5]. Here, if we consider 3 as pivot, which is in sorted position, the elements left to it ([1, 2]) are less than 3 and the elements right to it (4, 5) are greater than 3. Hence we know that 3 is in sorted position.Now let us discuss on how Quick sort algorithm is written.
We will select a random element as pivot and partition the array such that elements less than pivot position will be on left and elements greater will be on right. Any greater element on left will be swapped with any less element on right. In this manner we will keep on partitioning the array into sub arrays and rearranging them.
Sometimes Theory can be confusing than Practice – Vamsi Tallapudi 🙂
Let us quickly dive into the algorithm for Quick Sort Kotlin Implementation
Algorithm For Quick Sort Kotlin Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main.dataStructures.sorting | |
fun main(args: Array<String>) { | |
val array = readLine()!!.split(" ").map { it.toInt() }.toIntArray() // 1) Read the input and split into array | |
quickSort(array, 0, array.size-1) | |
for(i in array) println(i) | |
} | |
fun quickSort(array: IntArray, left: Int, right: Int) { | |
val index = partition (array, left, right) | |
if(left < index-1) { // 2) Sorting left half | |
quickSort(array, left, index-1) | |
} | |
if(index < right) { // 3) Sorting right half | |
quickSort(array,index, right) | |
} | |
} | |
fun partition(array: IntArray, l: Int, r: Int): Int { | |
var left = l | |
var right = r | |
val pivot = array[(left + right)/2] // 4) Pivot Point | |
while (left <= right) { | |
while (array[left] < pivot) left++ // 5) Find the elements on left that should be on right | |
while (array[right] > pivot) right– // 6) Find the elements on right that should be on left | |
// 7) Swap elements, and move left and right indices | |
if (left <= right) { | |
swapArray(array, left,right) | |
left++ | |
right– | |
} | |
} | |
return left | |
} | |
fun swapArray(a: IntArray, b: Int, c: Int) { | |
val temp = a[b] | |
a[b] = a[c] | |
a[c] = temp | |
} | |
As usual, Let us briefly discuss the points which are commented and are worth mentioning from the above Quick Sort Kotlin algorithm.
- Read the Input and Split into array:Â Here we are trying to read the input whose values are separated by spaces (for eg., 5 4 3 2 1) and later split them into an array of integers.
- Sorting left half:Â Here we will be calling the quickSort()Â recursively on the left half so that it will be sorted.
- Sorting right half:Â Here we will be calling the quickSort()Â recursively on the right half so that it will be sorted.
- Pivot Point:Â Here we are calculating the pivot point. For simplicity and efficient time complexity, we are taking the average of low and high as pivot. In general, we can have any element of the array / sub array as pivot.
- Find the elements on left that should be on right:Â Here we are iterating through the left array till we find the element greater than pivot position.
- Find the elements on right that should be on left:Â Here we are iterating through the right array until we find the element less than the pivot position.
- Swap elements, and move left and right indices:Â At this point of code, if left < right, then we have found left and right positions that needs to be swapped. Then we will increment left and decrement right positions respectively so that we will be ready for next iteration.
Performance
Performance of Quick sort depends upon the factors like choice of pivot selection point etc. On an average case, Quick sort’s time complexity is O(nlogn) and in worst case it is O(n2).
In the upcoming articles we will be discussing about Searching Algorithms.