# Fast Sorting with Quicksort | kirupa.com

**URL:** https://forum.kirupa.com/t/fast-sorting-with-quicksort-kirupa-com/389116
**Category:** programming
**Created:** [November 30, 2014, 8:35am UTC](https://forum.kirupa.com/t/fast-sorting-with-quicksort-kirupa-com/389116 "2014-11-30T08:35:20Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [November 30, 2014, 8:35am UTC](https://forum.kirupa.com/t/fast-sorting-with-quicksort-kirupa-com/389116/1 "2014-11-30T08:35:23Z")

</div>

by [kirupa](http://www.kirupa.com/me/index.htm) | 29 November 2014

When it comes to sorting stuff, one of the most popular algorithms you have is **quicksort**. It is popular because it is fast - really fast when compared to other algorithms for similar types of workloads. Key to its speed is that quicksort is a **divide-and-conquer algorithm**. It is called that because of how it breaks up its work. Instead of eating a giant chunk of data in one bite and chewing it over a long period of time (kinda like an anaconda), quicksort breaks up its data into smaller pieces and chews on each smaller piece quickly.

* * *
This is a companion discussion topic for the original entry at [http://www.kirupa.com/sorts/quicksort.htm](http://www.kirupa.com/sorts/quicksort.htm)

---

<div class="post-metadata">

### Author: ![WouterVos](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/woutervos/32/9098_2.png) [@WouterVos](https://forum.kirupa.com/u/WouterVos)
#### Post date: [May 2, 2018, 10:24am UTC](https://forum.kirupa.com/t/fast-sorting-with-quicksort-kirupa-com/389116/2 "2018-05-02T10:24:37Z")

</div>

Before swapping values you use the following code to check whether items should be swapped:

> [@](#):
>
> if (i \<= j) {

This checks whether the left index is smaller or equal to the right one.  
But if both indexes are equal, the swap necessarily cannot have an effect.  
Why not check with \< instead of \<=?

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [May 2, 2018, 6:56pm UTC](https://forum.kirupa.com/t/fast-sorting-with-quicksort-kirupa-com/389116/3 "2018-05-02T18:56:48Z")

</div>

You totally can. There are different implementations for that part. I chose `<=` for it made my end conditions easier. If you remove the `<=` and replace it with just a `<`, you’ll need to go back and update not only the `while` loop but also the initial array you pass in to account for the last value not being checked. It gets a bit messy IMO.

---

<div class="post-metadata">

### Author: ![WouterVos](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/woutervos/32/9098_2.png) [@WouterVos](https://forum.kirupa.com/u/WouterVos)
#### Post date: [May 3, 2018, 5:17am UTC](https://forum.kirupa.com/t/fast-sorting-with-quicksort-kirupa-com/389116/4 "2018-05-03T05:17:23Z")

</div>

Why is that? Since the indexes are the same the swap can’t do anything, right?  
In what scenario does that make a difference?

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [May 3, 2018, 6:28pm UTC](https://forum.kirupa.com/t/fast-sorting-with-quicksort-kirupa-com/389116/5 "2018-05-03T18:28:47Z")

</div>

You are totally right! Ignore what I said earlier. The important part is that we shift the values of `i` and `j`. You could simply write it as follows:

```
if (i < j) {
  var tempStore = arrayInput[i];

  arrayInput[i] = arrayInput[j];
  arrayInput[j] = tempStore;
}

i++;
j--;

```

Thanks for pointing that out 🙂
