C++C++11C++14C++17Learn C++

Quickly Learn To Use Quick Sort Algorithm In C++ On Windows

Quick Sort (QuickSort) Algorithm, also called as partition exchange sort, is a very fast sorting algorithm that developed by British computer scientist Tony Hoare in 1959. It gets a pivot element as pivot and partitions the given array around this element. QuickSort method can be summirized as Divide and Conquer Algorithm. There are many versions of QuickSort that pick elements in different ways. Basically, 4 methods are listed as below;

  1. Always pick first element as pivot.
  2. Always pick last element as pivot (implemented below)
  3. Pick a random element as pivot.
  4. Pick median as pivot.

In C++, standard library also has qsort algorithm that can be directly used with a given comparison method. To use this qsort(…) algorithm we must define our compare function and it should return integer.

The main idea in QuickSort is partitioning to element groups and using itself as a recursive procedure. Target of partitions is, given an array and an element x of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. All this should be done in linear time.

Let’s learn to build our own quicksort. QuickSort algorithm gets a pivot ( here it is pi ) by partition sort in range . Then it recursively keeps running quicksort() to sort two lower and higher partitions by using these high and low parameters. An example of my_quicksort(…) is given as below;

To use this my_quicksort(…) we need to write my_partitionsort(…) partition sort algorithm as below;

Here we used comp(…) compare function which has two integer parameters and returns bool, for specific comparisons we can define our own compare function as below;

Here is the full C++ Builder Console VCL Example below;

Head over and find out more about building Windows apps in C++Builder.

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is A Forced (Default) Copy Assignment Operator In Modern C++

C++C++11C++14C++17C++20Learn C++Syntax

What is Implicitly-declared Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What is Avoiding Implicit Copy Assignment In C++?

C++C++11C++14C++17C++20Learn C++

Typical Declaration Of A Copy Assignment Operator Without std::swap