快速排序算法是否自适应
原文:https://www . geesforgeks . org/is-quick-sort-algorithm-adaptive-or-not/
先决条件:快速排序算法
适应性快速排序算法中的是指如果给我们一个已经排序的数组,那么应该执行还是不执行操作,即如果在排序数组上执行的操作数不等于在未排序数组上执行的操作数,那么该算法称为自适应。
在本文中,我们将看到 快速排序算法是否自适应 。
举个例子:
int arr[] = {1,2,3,4,5 };
所以在上面的例子中,我们可以看到数组是经过排序的。但是当数组是未排序的时,那么就应该执行操作,我们知道在最坏的情况下快速排序的时间复杂度是 O(N^2) 。因此,如果数组未排序,那么要排序的数组的时间复杂度为o(n^2),
原因:因为要排序数组 N-1 传球应该做 N-i-1 互换。
快速排序算法不自适应,
我们能让快速排序算法自适应吗?T3】
是的,我们可以很容易地使它自适应。
例如:
在下面的代码中,我们创建了一个 void 函数AdaptiveBubbleSort,它接受参数“arr[],n”,这是一个数组,其大小为 n 。
我们已经创建了 isSorted 整数变量,该变量用 0 初始化,如果它被排序为 IsSorted = 1 ,则内部 for 循环将不执行,但如果它不执行,则执行内部 for 循环,此外,如果我们发现元素 j+1 比当前元素 j 少,则在交换它们之后交换它们,直到它被排序。
最后,调用导出的变量并返回。
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
Python 3
# 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.
java 描述语言
<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
因此,如果数组已经排序,那么每个元素几乎都会遍历一次。因此,时间复杂度变为 O(N) ,否则取 O(N*log(N))。 在所有情况下辅助空间将为 O(1)。
版权属于:月萌API www.moonapi.com,转载请注明出处