Thursday, June 1, 2023
HomeAlgorithmsQuick Sort Kotlin Implementation - Sorting Algorithms #4

# Quick Sort Kotlin Implementation – Sorting Algorithms #4

-

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.

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.

 package main.dataStructures.sorting fun main(args: Array) { 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 }

view raw

QuickSort.kt

hosted with ❤ by GitHub

As usual, Let us briefly discuss the points which are commented and are worth mentioning from the above Quick Sort Kotlin algorithm.

1. 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.
2. Sorting left half: Here we will be calling the quickSort() recursively on the left half so that it will be sorted.
3. Sorting right half: Here we will be calling the quickSort() recursively on the right half so that it will be sorted.
4. 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.
5. 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.
6. 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.
7. 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.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

### House Robber Problem | Maximum Sum of non-adjacent elements

In this blog post, we will discuss about how to find maximum sum of Non-Adjacent elements. Since House Robber Problem is a typical DP Problem...
00:11:41

### Check Array Formation Through Concatenation | Leetcode Problem 1640

In this video, we will explain Check Array Formation Through Concatenation, Leetcode's 1640th problem in 2 different approaches along with a coding solution. Problem statement You are given...

### Binary Tree Traversals in Kotlin – PreOrder, InOrder and PostOrder Traversals

Binary Tree Traversals are most common Tree Problems in an Interview as well as Competitive Programming. They can be efficient while searching, inserting and deleting...

### Next Greater node in a Linked List – Leetcode Problem #1019

In this article, we will discuss the solution for 1019th Problem from Leetcode - Next Greater Node In Linked List. Let us see the question in...

1,358Fans
10Followers
402Subscribers

00:11:41