Skip to content
Related Articles

Related Articles

Is Quick Sort Algorithm Adaptive or not

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 03 Dec, 2021

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.

In this article, we will see if Quick Sort Algorithm is Adaptive or not.

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 <bits/stdc++.h>
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




<script>
    // JavaScript program for the above approach
 
    // Function to print the array
    const Print = (arr, n) => {
        for (let i = 0; i < n; i++) {
            document.write(`${arr[i]} `);
        }
        document.write("<br/>");
    }
 
    // Function for adaptive sort algorithm
    // to sort the array if the array is not
    // sorted otherwise return in one-pass.
    const AdaptiveBubbleSort = (arr, n) => {
 
        let temp;
 
        // Stores the status of the array of
        // sorted or not.
        let isSorted = 0;
 
        // Traverse the array
        for (let i = 0; i < n - 1; i++) {
 
            // Initialize it with 1
            isSorted = 1;
 
            // Compare the adjacent elements
            for (let 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
    let arr = [1, 2, 3, 4, 5];
    let n = 5;
    document.write("Array before sorting : ");
    Print(arr, n);
    AdaptiveBubbleSort(arr, n);
    document.write("Array after sorting : ");
    Print(arr, n);
 
    // This code is contributed by rakeshsahni
 
</script>


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(N*log(N)).
In all the cases the Auxiliary Space will be O(1).


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!