## Introduction to Haskell quicksort

Quicksort, in general, is a sorting algorithm that is used to sort the given values in the minimum amount of time, by taking performance into the consideration. If we talk about quicksort in Haskell, then it is not different only the syntax of writing the algorithm is different but the concept behind it is the same. We know that the quicksort is based on the divide and concur rule where we dived the array or list into two parts and try to create the two sub-array from the given one. In the end, after doing the steps involved in quicksort we get the resultant array which is sorted. In the coming section of the tutorial, we will see the internal working of quicksort in Haskell, and a sample to implement the quicksort in Haskell for beginners to understand it better.

**Syntax**

As we know that it is not a function that has some syntax we can write our own logic based on the algorithm, this code may be different from one another but the concepts will be the same. So in this section, we can see the algorithm steps but not the syntax in details see below;

1) Select pivot element

2) Define two-variable named i and j

3) increment i until greater than the pivot.

4) decrement j until less than the pivot.

5) repeat.

As you can see in the above lines of algorithm steps we have to write code keeping these above points in mind, then only we will be able to sort the elements of an array by using quicksort in Haskell. In the coming section of the tutorial we will see its internal working with one example which we will iterate to understand how it works internally, also a code snippet as a sample for this for beginners to understand it better.

### How does quicksort work in Haskell?

As of now we already know that quicksort is a sorting algorithm that tends to work in the same way as other programming languages because the concept behind it going to be the same for all. The only difference is that how we can write it using different languages. So in this section, we will see its internal working how it actually behaves in Haskell, Let’s get started,

**Internal working of quicksort in Haskell:**

So quicksort is a very efficient sorting algorithm when it comes to performance, in this algorithm what we do is divide the original array into two parts called as sub-array. The idea behind quicksort is it dived the array and try to call it recursively to sort the array elements. This algorithm is very efficient to handle large data and the complexity of quicksort in Haskell is given as below;

average complexisty : O(n2)

worst-case complexity : O(n2)

Below is the algorithm steps that we need to follow in order to write our code as per the steps mentioned see below;

4.8 (10,650 ratings)

View Course

1) First we try to select the pivot elements in quicksort in Haskell. These pivot elements will divide the array into the sub array, and we will keep on repeating and creating the sun array until only one elements left into the array. In order to select the pivot element from the given array we choose the highest element from the array and make it as pivot element for quicksort in Haskell.

2) in the next step we try to choose the left and right values which should not include pivot element from the given array.

3) Right value will points toward the high value.

4) Left value will points toward the low value.

5) If we have value right is greater than pivot element then move left.

6) If the value at left is less than pivot than we have to move right.

7) If the above-mentioned step does not follow then we will swap it to the given left and right.

8) If left value if greater than or equal to the right then set it as new pivot element for array.

As you can see in the above lines of steps which clearly define the steps which need to be taken and follow while implementing quicksort in Haskell. Also then concept is same only the code writing standard will be different. Let’s take an sample array to understand t more better in Haskell see below;

given array in haskell as follows;

5, 3, 8, 10, 6, 7, 9

Now we ill consider the element pivot left and right from the given list, supposed we have taken 5 as the pivot 3 as left and 7 as the right value from the given array.

now we have to perform all the step that we have described above in the same order we have written it. Now we will check if left is less than pivot then we will move right in our case we have 3 <5 which is tru so we will move left one step ahead. Same we will check for right if the right is greater than pivot than we will move to left one step. Here we have 9 > 5 this is true, so decrement by one. This steps will be going to perform until we have a sorted array.

### Examples

1. This is a sample example of quicksort in Haskell, we are trying to use the above steps mentioned to create the quicksort logic written in Haskell. This code will be written us the sorted array, we have created several array to test our logic, this is a sample example for beginners to understand it better and start using quicksort algorithm inside the program for better performance to handle large data.

**Example:**

```
quicksortdemo :: (Ord m) => [m] -> [m]
quicksortdemo [] = []
quicksortdemo (q:qs) = quicksortdemo [m | m <- qs, m < q]
++ [q] ++
```

quicksortdemo [n | n <- qs, n >= q]
main = do

print("Demo to show quicksort in haskell !!")

let array1 = [10, 30, 56, 2, 1]
let array2 = [23, 13, 10, 8, 9, 1, 5, 42, 50]
let array3 = [3, 7, 1, 4, 9, 56, 10]
let array4 = [9, 6, 12, 15, 10 , 2 , 4, 20]
let array5 = [200, 100, 400, 250, 450, 150, 350]
print("Printing result after sorting in quicksort in haskell !!")

let result1 = quicksortdemo array1

let result2 = quicksortdemo array2

let result3 = quicksortdemo array3

let result4 = quicksortdemo array4

let result5 = quicksortdemo array5

print("first result array is ::", result1)

print("second result array is ::", result2)

print("third result array is ::", result3)

print("fourth result array is ::", result4)

print("fifth result array is ::", result5)

**Output:**

### Conclusion

By using quicksort algorithm we can archive good performance in Haskell to sort our data. Also it is very easy to use, readable and easy to maintain also. It is not an function or library but it is an concept of data structure to offer good programming practice and increase the perforce of code by writing efficient code.

### Recommended Articles

This is a guide to Haskell quicksort. Here we discuss How does quicksort work in Haskell along with the examples and output. You may also have a look at the following articles to learn more –