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++
#include <bits/stdc++.h>
using namespace std;
void Print( int arr[], int n)
{
for ( int i = 0; i < n; i++) {
cout << arr[i] << " " ;
}
cout << endl;
}
void AdaptiveBubbleSort( int arr[], int n)
{
int temp;
int isSorted = 0;
for ( int i = 0; i < n - 1; i++) {
isSorted = 1;
for ( int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
isSorted = 0;
}
}
if (isSorted) {
return ;
}
}
}
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
import java.util.*;
class GFG{
static void Print( int arr[], int n)
{
for ( int i = 0 ; i < n; i++) {
System.out.print(arr[i]+ " " );
}
System.out.println();
}
static void AdaptiveBubbleSort( int arr[], int n)
{
int temp;
int isSorted = 0 ;
for ( int i = 0 ; i < n - 1 ; i++) {
isSorted = 1 ;
for ( int j = 0 ; j < n - i - 1 ; j++) {
if (arr[j] > arr[j + 1 ]) {
temp = arr[j];
arr[j] = arr[j + 1 ];
arr[j + 1 ] = temp;
isSorted = 0 ;
}
}
if (isSorted!= 0 ) {
return ;
}
}
}
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);
}
}
|
Python3
def Print (arr, n):
for i in range (n):
print (arr[i], end = " " )
print ("")
def AdaptiveBubbleSort(arr, n):
isSorted = 0
for i in range (n - 1 ):
isSorted = 1
for j in range (n - i - 1 ):
if (arr[j] > arr[j + 1 ]):
temp = arr[j]
arr[j] = arr[j + 1 ]
arr[j + 1 ] = temp
isSorted = 0
if (isSorted):
return
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)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void Print( int [] arr, int n)
{
for ( int i = 0; i < n; i++) {
Console.Write(arr[i]+ " " );
}
Console.WriteLine();
}
static void AdaptiveBubbleSort( int [] arr, int n)
{
int temp;
int isSorted = 0;
for ( int i = 0; i < n - 1; i++) {
isSorted = 1;
for ( int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
isSorted = 0;
}
}
if (isSorted != 0) {
return ;
}
}
}
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);
}
}
|
Javascript
<script>
const Print = (arr, n) => {
for (let i = 0; i < n; i++) {
document.write(`${arr[i]} `);
}
document.write( "<br/>" );
}
const AdaptiveBubbleSort = (arr, n) => {
let temp;
let isSorted = 0;
for (let i = 0; i < n - 1; i++) {
isSorted = 1;
for (let j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
isSorted = 0;
}
}
if (isSorted) {
return ;
}
}
}
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);
</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(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!
Last Updated :
08 May, 2023
Like Article
Save Article