📜  快速排序算法是否自适应

📅  最后修改于: 2022-05-13 01:56:09.743000             🧑  作者: Mango

快速排序算法是否自适应

先决条件:快速排序算法

快速排序算法中的自适应性是指如果给定一个已经排序的数组,那么操作应该执行还是不执行,即,如果对排序后的数组执行的操作数不等于在排序后的数组上执行的操作数。未排序的数组,则该算法称为自适应。

在本文中,我们将了解快速排序算法是否具有自适应性

举个例子:

所以在上面的例子中,我们可以看到数组是有序的。但是当数组未排序时,应该执行操作,并且我们知道快速排序的时间复杂度在最坏的情况下是O(N^2) 。因此,如果数组未排序,则要排序的数组的时间复杂度为O(N^2)

原因:因为要对数组进行排序N-1遍应该完成并且Ni-1交换。

快速排序算法不是自适应的,

我们可以使快速排序算法自适应吗?

是的,我们可以很容易地让它自适应。

例如:

在下面的代码中,我们创建了一个void函数AdaptiveBubbleSort ,它接受参数“arr[], n” ,它是一个数组,其大小为n

我们已经创建了isSorted Integer 变量,它是 Initialize with 0 ,如果它是排序的IsSorted = 1则内部 for 循环将不会执行,但如果它没有执行内部 for 循环,并且如果我们找到一个元素j+比当前元素j1 ,然后在交换它们之后交换它们,它是排序的。

最后,调用isSorted变量并返回它。

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


输出
Array before sorting : 1 2 3 4 5 
Array after sorting : 1 2 3 4 5 

因此,如果数组已经排序,那么每个元素几乎都会被遍历一次。因此,时间复杂度变为O(N) ,否则为O(N*log(N))。
在所有情况下,辅助空间都是O(1)。