Quick sort

Quicksort is a sorting algorithm based on the divide and conquer approach where an array is divided into subarrays by selecting a pivot element (element selected from the array).

  1. While dividing the array, the pivot element should be positioned in such a way that elements less than the pivot are kept on the left side, and elements greater than the pivot is on the right side of the pivot.
  2. The left and right subarrays are also divided using the same approach. This process continues until each subarray contains a single element.
  3. At this point, elements are already sorted. Finally, elements are combined to form a sorted array.

Working of Quick Sort algorithm:

To know the functioning of Quick sort, let’s consider an array arr[] = {10, 80, 30, 90, 40, 50, 70}

  • Indexes:  0   1   2   3   4   5   6 
  • low = 0, high =  6, pivot = arr[h] = 70
  • Initialize index of smaller element, i = -1

Step1

  • Traverse elements from j = low to high-1
  • j = 0: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
  • i = 0 
  • arr[] = {10, 80, 30, 90, 40, 50, 70} // No change as i and j are same
  • j = 1: Since arr[j] > pivot, do nothing

Step2

  • j = 2 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
  • i = 1
  • arr[] = {10, 30, 80, 90, 40, 50, 70} // We swap 80 and 30 

Step3

  • j = 3 : Since arr[j] > pivot, do nothing // No change in i and arr[]
  • j = 4 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
  • i = 2
  • arr[] = {10, 30, 40, 90, 80, 50, 70} // 80 and 40 Swapped

Step 4

  • j = 5 : Since arr[j] <= pivot, do i++ and swap arr[i] with arr[j] 
  • i = 3 
  • arr[] = {10, 30, 40, 50, 80, 90, 70} // 90 and 50 Swapped 

Step 5

  • We come out of loop because j is now equal to high-1.
  • Finally we place pivot at correct position by swapping arr[i+1] and arr[high] (or pivot) 
  • arr[] = {10, 30, 40, 50, 70, 90, 80} // 80 and 70 Swapped 

Step 6

  • Now 70 is at its correct place. All elements smaller than 70 are before it and all elements greater than 70 are after it.
  • Since quick sort is a recursive function, we call the partition function again at left and right partitions

Step 7

  • Again call function at right part and swap 80 and 90

Step 8

Introduction to Sorting Techniques – Data Structure and Algorithm Tutorials

Sorting refers to rearrangement of a given array or list of elements according to a comparison operator on the elements. The comparison operator is used to decide the new order of elements in the respective data structure.

When we have a large amount of data, it can be difficult to deal with it, especially when it is arranged randomly. When this happens, sorting that data becomes crucial. It is necessary to sort data in order to make searching easier.

Similar Reads

Types of Sorting Techniques

There are various sorting algorithms are used in data structures. The following two types of sorting algorithms can be broadly classified:...

Why Sorting Algorithms are Important

The sorting algorithm is important in Computer Science because it reduces the complexity of a problem. There is a wide range of applications for these algorithms, including searching algorithms, database algorithms, divide and conquer methods, and data structure algorithms....

Some of the most common sorting algorithms are:

Below are some of the most common sorting algorithms:...

1. Selection sort

Selection sort is another sorting technique in which we find the minimum element in every iteration and place it in the array beginning from the first index. Thus, a selection sort also gets divided into a sorted and unsorted subarray....

2. Bubble sort

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity is quite high....

3. Insertion Sort

Insertion sort is a simple sorting algorithm that works similarly to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part....

4. Merge Sort

The Merge Sort algorithm is a sorting algorithm that is based on the Divide and Conquers paradigm. In this algorithm, the array is initially divided into two equal halves and then they are combined in a sorted manner....

5. Quick sort

Quicksort is a sorting algorithm based on the divide and conquer approach where an array is divided into subarrays by selecting a pivot element (element selected from the array)....

6. Heap sort

Heap sort is a comparison-based sorting technique based on Binary Heap data structure. It is similar to the selection sort where we first find the minimum element and place the minimum element at the beginning. Repeat the same process for the remaining elements....

7. Counting sort

Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing). Then do some arithmetic to calculate the position of each object in the output sequence....

Some other Sorting algorithms:

1. Radix sort...

Comparison of Complexity Analysis Between Sorting Algorithms:

NameBest Case  Average Case  Worst Case MemoryStable   Method UsedQuick Sort[Tex]n log n[/Tex][Tex]n log n[/Tex][Tex]n^{2}[/Tex][Tex]log n[/Tex]NoPartitioningMerge Sort[Tex]n log n[/Tex][Tex]n log n[/Tex][Tex]n log n[/Tex]nYesMergingHeap Sort[Tex]n log n[/Tex][Tex]n log n[/Tex][Tex]n log n[/Tex]1NoSelectionInsertion Sortn[Tex]n^{2}[/Tex][Tex]n^{2}[/Tex]1YesInsertionTim Sortn[Tex]n log n[/Tex][Tex]n log n[/Tex]nYesInsertion & MergingSelection Sort[Tex]n^{2}[/Tex][Tex]n^{2}[/Tex][Tex]n^{2}[/Tex]1NoSelectionShell Sort[Tex]n log n[/Tex][Tex]n^{4/3}[/Tex][Tex]n^{3/2}[/Tex]1NoInsertionBubble Sortn[Tex]n^{2}[/Tex][Tex]n^{2}[/Tex]1YesExchangingTree Sort[Tex]n log n[/Tex][Tex]n log n[/Tex][Tex]n log n[/Tex]nYesInsertionCycle Sort[Tex]n^{2}[/Tex][Tex]n^{2}[/Tex][Tex]n^{2}[/Tex]1NoSelectionStrand Sortn[Tex]n^{2}[/Tex][Tex]n^{2}[/Tex]nYesSelectionCocktail Shaker Sortn[Tex]n^{2}[/Tex][Tex]n^{2}[/Tex]1YesExchangingComb Sort[Tex]n log n[/Tex][Tex]n^{2}[/Tex][Tex]n^{2}[/Tex]1NoExchangingGnome Sortn[Tex]n^{2}[/Tex][Tex]n^{2}[/Tex]1YesExchangingOdd–even Sortn[Tex]n^{2}[/Tex][Tex]n^{2}[/Tex]1YesExchanging...

Contact Us