### How To Make Quicksort Algorithm With C# – Made Easy

Quicksort algorithm or divide and conquer algorithm is one of the most used sorting algorithms, because of its supperior efficiency.

Filter by Category

- C# Tutorial(85)
- C# Image Processing(69)
- Morphological Processes(20)
- Image Processing(16)
- Image Restoration and Reconstruction(16)
- Image Segmentation(13)
- C# Data Structures And Algorithms(11)
- Color Image Processing(8)
- Frequency Domain Filtering(8)
- Image Noise(6)
- Grayscale Morphology(5)
- Thresholding(4)
- Order-Statistic Filters(4)
- Mean Filters(4)
- Sorting Algorithms(4)
- Morphological Reconstruction(3)
- Edge Detection(3)
- Simple Lists(2)
- RGB to HSI Color Model(2)
- Adaptive Filters(2)
- Tone and Color Corrections(2)
- Linked Lists(2)
- Stacks(1)
- Queues(1)
- Point Detection(1)
- Line Detection(1)
- C# Arrays(1)
- Region Segmentation Using Superpixels(1)
- Region Segmentation With K Means Clustering(1)
- Region Splitting And Merging(1)
- Sorted Lists(1)
- Region Growing Segmentation(1)
- Digital Image Watermarking(1)
- Using Color In Image Segmentation(1)
- Social Games(1)
- Bandreject Filters(1)
- Bandpass filters(1)
- Notch Filters(1)
- Landing Pages(1)
- Intensity Slicing and Color Coding(1)
- Color Slicing(1)
- Histogram Processing Color Images(1)
- Color Image Smoothing And Sharpening(1)
- C# Basics(1)

Back to Latest Articles
###
How To Make Quicksort Algorithm With C# – Made Easy

###
How To Make Bubble Sort Algorithm In C# – Made Easy

###
How To Make Insertion Sort Algorithm In C# – Made Easy

###
How to Selection Sort Algorithm in C# Made Easy

Sorting Algorithms

Quicksort algorithm or divide and conquer algorithm is one of the most used sorting algorithms, because of its supperior efficiency.

Sorting Algorithms

Bubble sort is one of the simplest sorting algorithms and this guide shows how to implement it in C# programming language.

Sorting Algorithms

Insertion sort is one of the simplest sorting algorithms for sorting single dimensional arrays of various different data types.

Sorting Algorithms

Selection sort algorithm is one of the simplest sorting algorithms out there. You can use it to sort different data type arrays.

How To Make Quicksort Algorithm With C#...

Quicksort algorithm is one of the most popular sorting algorithms in use out there. In case you haven’t heard for it before, we can also call it divide and conquer algorithm. Furthermore, as it’s name suggests, it divides arrays into partitions and sorts them and objects inside.

Unlike sorting algorithms I’ve covered so far on this blog, quicksort algorithm sorts objects with best efficiency among them all. And in order to be this way, we don’t need to sacrifice any other aspect of it as it is also pretty simple to understand.

Now let’s dive into details on how exactly does it work. First of all, algorithm picks a value from the array and sets it as a pivot. Next, it reorganizes the array in such a way that all values lower than the pivot are placed before it and higher after it.

In doing so, we form lower and higher subarray, on which we repeat this whole operation recursively. Therefore, we need to stop the recursion when there is only 1 or no elements inside the subarray we want to sort.

Since the process utilizes recursion, this makes it way more efficient than other sorting algorithms I’ve covered so far. In case you’re not familiar what it is, it’s basically calling a method inside the same method. A little mindbending, but nothing we can’t clear up with implementation in code.

So let’s take a look at how it works in practice.

```
public static class Quicksort
{
public static void Sort<T>(T[] array) where T : IComparable
{
Sort(array, 0, array.Length - 1);
}
private static T[] Sort<T>(T[] array, int lower, int higher) where T : IComparable
{
if (lower < higher)
{
int p = Partition(array, lower, higher);
Sort(array, lower, p);
Sort(array, p + 1, higher);
}
return array;
}
private static int Partition<T>(T[] array, int lower, int higher) where T : IComparable
{
int i = lower;
int j = higher;
T pivot = array[lower];
do
{
while (array[i].CompareTo(pivot) < 0)
{
i++;
}
while (array[j].CompareTo(pivot) > 0)
{
j--;
}
if (i >= j)
{
break;
}
Swap(array, i, j);
} while (i <= j);
return j;
}
private static void Swap<T>(T[] array, int first, int second)
{
T temp = array[first];
array[first] = array[second];
array[second] = temp;
}
}
```

As you can see from the code above, we created a public static class Quicksort, which holds 4 methods in total. However, only 1 is public while others are private. Therefore, private methods only serve as individual components of the entire process.

For versatility, we can use IComparable inteface which holds method CompareTo. Furthermore, this method is compatible with various different data types. Therefore, we’ll use a generic data type T when we’re declaring methods.

First of all, the **Swap** method just swaps 2 objects of the array, which takes 3 arguments, the array and indexes of objects.

Next, the **Partition** method swaps the objects around the pivot object and returns object index at which we divide the array into subarrays.

Third private method is an overloaded **Sort** method, which utilizes **Partition** method and **itself** to recursively sort the subarrays.

And finally, the public Sort method, which takes only one argument, the array. We call the private Sort method inside it, where we also set all 3 arguments that it needs.

I hope this guide helped you gain a better understanding of how a Quicksort algorithm works in C#.

In case you want to see it in action, you can also download the demo project and try it out for yourself.