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?
Table of Contents
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.
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
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 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.