Related Articles

# Is Quick Sort Algorithm Adaptive or not

Pre-Requisites: Quick Sort Algorithm

Adaptiveness in the Quick Sort Algorithm refers to the decision that if we are given an array that is already sorted, then the operations should be Performed or Not, i.e., if the number of operations performed on sorted Array is Not Equal to the operations performed on unsorted Array, then the Algorithm is known as Adaptive.

Let’s take an example:

int arr[] = {1,2,3,4,5};

So in the above example, we can see that the array is sorted. But when the array is Unsorted then the Operation should be performed, and as we know that the Time Complexity Of Quick Sort is O(N^2) in the worst case. So if the array is Unsorted then the time complexity of the array to sort is O(N^2)

Reason: Because to sort the array N-1 passes should be done and N-i-1 swaps.

The Quicksort Algorithm is Not Adaptive

Can we make QuickSort Algorithm Adaptive?

Yes, we can make it Adaptive very easily.

For example:

In the below code, we have created a void function AdaptiveBubbleSort which takes arguments “arr[], n”  that is an array and its size that is n.

We have created isSorted Integer variable which is Initialize with 0, if it is sorted that is IsSorted = 1  then the inner for loop will not execute, but if it not then execute the inner for loop, and further if we find an element j+1 less then current element j, then swap them after swapping them it is Sorted.

At last, invoke the isSorted Variable and return it.

## C++

 `// C++ program for the above approach` `#include ` `using` `namespace` `std;`   `// Function to print the array` `void` `Print(``int` `arr[], ``int` `n)` `{` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``cout << arr[i] << ``" "``;` `    ``}` `    ``cout << endl;` `}`   `// Function for adaptive sort algorithm` `// to sort the array if the array is not` `// sorted otherwise return in one-pass.` `void` `AdaptiveBubbleSort(``int` `arr[], ``int` `n)` `{`   `    ``int` `temp;`   `    ``// Stores the status of the array of` `    ``// sorted or not.` `    ``int` `isSorted = 0;`   `    ``// Traverse the array` `    ``for` `(``int` `i = 0; i < n - 1; i++) {`   `        ``// Initialize it with 1` `        ``isSorted = 1;`   `        ``// Compare the adjacent elements` `        ``for` `(``int` `j = 0; j < n - i - 1; j++) {`   `            ``// Violates the condition that the` `            ``// array is not sorted` `            ``if` `(arr[j] > arr[j + 1]) {` `                ``temp = arr[j];` `                ``arr[j] = arr[j + 1];` `                ``arr[j + 1] = temp;` `                ``isSorted = 0;` `            ``}` `        ``}`   `        ``// If the array is sorted, then return` `        ``// the array and no need to compare elements` `        ``if` `(isSorted) {` `            ``return``;` `        ``}` `    ``}` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `arr[] = { 1, 2, 3, 4, 5 };` `    ``int` `n = 5;` `    ``cout << ``"Array before sorting : "``;` `    ``Print(arr, n);` `    ``AdaptiveBubbleSort(arr, n);` `    ``cout << ``"Array after sorting : "``;` `    ``Print(arr, n);` `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `import` `java.util.*;`   `class` `GFG{`   `// Function to print the array` `static` `void` `Print(``int` `arr[], ``int` `n)` `{` `    ``for` `(``int` `i = ``0``; i < n; i++) {` `        ``System.out.print(arr[i]+ ``" "``);` `    ``}` `    ``System.out.println();` `}`   `// Function for adaptive sort algorithm` `// to sort the array if the array is not` `// sorted otherwise return in one-pass.` `static` `void` `AdaptiveBubbleSort(``int` `arr[], ``int` `n)` `{`   `    ``int` `temp;`   `    ``// Stores the status of the array of` `    ``// sorted or not.` `    ``int` `isSorted = ``0``;`   `    ``// Traverse the array` `    ``for` `(``int` `i = ``0``; i < n - ``1``; i++) {`   `        ``// Initialize it with 1` `        ``isSorted = ``1``;`   `        ``// Compare the adjacent elements` `        ``for` `(``int` `j = ``0``; j < n - i - ``1``; j++) {`   `            ``// Violates the condition that the` `            ``// array is not sorted` `            ``if` `(arr[j] > arr[j + ``1``]) {` `                ``temp = arr[j];` `                ``arr[j] = arr[j + ``1``];` `                ``arr[j + ``1``] = temp;` `                ``isSorted = ``0``;` `            ``}` `        ``}`   `        ``// If the array is sorted, then return` `        ``// the array and no need to compare elements` `        ``if` `(isSorted!=``0``) {` `            ``return``;` `        ``}` `    ``}` `}`   `// Driver Code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `arr[] = { ``1``, ``2``, ``3``, ``4``, ``5` `};` `    ``int` `n = ``5``;` `    ``System.out.print(``"Array before sorting : "``);` `    ``Print(arr, n);` `    ``AdaptiveBubbleSort(arr, n);` `    ``System.out.print(``"Array after sorting : "``);` `    ``Print(arr, n);` `}` `}`   `// This code is contributed by 29AjayKumar`

## Python3

 `# Python program for the above approach`   `# Function to print the array` `def` `Print``(arr, n):` `    ``for` `i ``in` `range``(n):` `        ``print``(arr[i], end``=``" "``)` `    ``print``("")`   `# Function for adaptive sort algorithm` `# to sort the array if the array is not` `# sorted otherwise return in one-pass.` `def` `AdaptiveBubbleSort(arr, n):`   `    ``# Stores the status of the array of` `    ``# sorted or not.` `    ``isSorted ``=` `0`   `    ``# Traverse the array` `    ``for` `i ``in` `range``(n ``-` `1``):`   `        ``# Initialize it with 1` `        ``isSorted ``=` `1`   `        ``# Compare the adjacent elements` `        ``for` `j ``in` `range``(n ``-` `i ``-` `1``):`   `            ``# Violates the condition that the` `            ``# array is not sorted` `            ``if` `(arr[j] > arr[j ``+` `1``]):` `                ``temp ``=` `arr[j]` `                ``arr[j] ``=` `arr[j ``+` `1``]` `                ``arr[j ``+` `1``] ``=` `temp` `                ``isSorted ``=` `0`   `        ``# If the array is sorted, then return` `        ``# the array and no need to compare elements` `        ``if` `(isSorted):` `            ``return`   `# Driver Code` `arr ``=` `[``1``, ``2``, ``3``, ``4``, ``5``]` `n ``=` `5` `print``(``"Array before sorting : "``)` `Print``(arr, n)` `AdaptiveBubbleSort(arr, n)` `print``(``"Array after sorting : "``)` `Print``(arr, n)`   `# This code is contributed by gfgking.`

## C#

 `// C# program for the above approach` `using` `System;` `using` `System.Collections.Generic;`   `class` `GFG{`   `// Function to print the array` `static` `void` `Print(``int``[] arr, ``int` `n)` `{` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``Console.Write(arr[i]+ ``" "``);` `    ``}` `    ``Console.WriteLine();` `}`   `// Function for adaptive sort algorithm` `// to sort the array if the array is not` `// sorted otherwise return in one-pass.` `static` `void` `AdaptiveBubbleSort(``int``[] arr, ``int` `n)` `{`   `    ``int` `temp;`   `    ``// Stores the status of the array of` `    ``// sorted or not.` `    ``int` `isSorted = 0;`   `    ``// Traverse the array` `    ``for` `(``int` `i = 0; i < n - 1; i++) {`   `        ``// Initialize it with 1` `        ``isSorted = 1;`   `        ``// Compare the adjacent elements` `        ``for` `(``int` `j = 0; j < n - i - 1; j++) {`   `            ``// Violates the condition that the` `            ``// array is not sorted` `            ``if` `(arr[j] > arr[j + 1]) {` `                ``temp = arr[j];` `                ``arr[j] = arr[j + 1];` `                ``arr[j + 1] = temp;` `                ``isSorted = 0;` `            ``}` `        ``}`   `        ``// If the array is sorted, then return` `        ``// the array and no need to compare elements` `        ``if` `(isSorted != 0) {` `            ``return``;` `        ``}` `    ``}` `}`   `// Driver Code` `public` `static` `void` `Main()` `{` `    ``int``[] arr = { 1, 2, 3, 4, 5 };` `    ``int` `n = 5;` `    ``Console.Write(``"Array before sorting : "``);` `    ``Print(arr, n);` `    ``AdaptiveBubbleSort(arr, n);` `    ``Console.Write(``"Array after sorting : "``);` `    ``Print(arr, n);` `}` `}`   `// This code is contributed by sanjoy_62.`

## Javascript

 ``

Output

```Array before sorting : 1 2 3 4 5
Array after sorting : 1 2 3 4 5 ```

Therefore, if the array is already sorted, therefore each element is traversed almost once. Therefore, the time complexity becomes O(N) and otherwise, it takes O(N2).
In all the cases the Auxiliary Space will be O(1).

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next