使用排序算法根据上的元素运算符重新排列给定阵列或列表元素。运算符用于决定在相应的数据结构中的元素的新的订单。但下面是一些最慢的排序算法:
Stooge 排序: Stooge 排序是一种递归排序算法。它递归地对数组进行分割和排序。以下是Stooge Sort的步骤:
- 如果索引0处的值大于最后一个索引处的值,则交换它们。
- 如果数组中的元素数大于两个:
- 对数组的初始2/3 元素递归调用stoogesort函数。
- 对数组的最后2/3 个元素递归调用stoogesort函数。
- 再次为初始的2/3rd 元素递归调用stoogesort函数以确认结果数组是否已排序。
- 打印排序后的数组。
下面是上述方法的实现:
C++
// C++ program for the stooge sort
#include
using namespace std;
// Function to implement stooge sort
void stoogesort(int arr[], int l, int h)
{
// Base Case
if (l >= h)
return;
// If first element is smaller than
// last element, swap them
if (arr[l] > arr[h])
swap(arr[l], arr[h]);
// If there are more than 2 elements
// in the array
if (h - l + 1 > 2) {
int t = (h - l + 1) / 3;
// Recursively sort the first
// 2/3 elements
stoogesort(arr, l, h - t);
// Recursively sort the last
// 2/3 elements
stoogesort(arr, l + t, h);
// Recursively sort the first
// 2/3 elements again
stoogesort(arr, l, h - t);
}
}
// Driver Code
int main()
{
int arr[] = { 2, 4, 5, 3, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
stoogesort(arr, 0, N - 1);
// Display the sorted array
for (int i = 0; i < N; i++) {
cout << arr[i] << " ";
}
return 0;
}
Java
// Java program for the
// stooge sort
class GFG{
// Function to implement
// stooge sort
static void stoogesort(int arr[],
int l, int h)
{
// Base Case
if (l >= h)
return;
// If first element is smaller
// than last element, swap them
if (arr[l] > arr[h])
{
int temp = arr[l];
arr[l] = arr[h];
arr[h] = temp;
}
// If there are more than
// 2 elements in the array
if (h - l + 1 > 2)
{
int t = (h - l + 1) / 3;
// Recursively sort the
// first 2/3 elements
stoogesort(arr, l, h - t);
// Recursively sort the
// last 2/3 elements
stoogesort(arr, l + t, h);
// Recursively sort the
// first 2/3 elements again
stoogesort(arr, l, h - t);
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = {2, 4, 5, 3, 1};
int N = arr.length;
// Function Call
stoogesort(arr, 0, N - 1);
// Display the sorted array
for (int i = 0; i < N; i++)
{
System.out.print(arr[i] + " ");
}
}
}
// This code is contributed by Chitranayal
Python3
# Python3 program for the stooge sort
# Function to implement stooge sort
def stoogesort(arr, l, h):
# Base Case
if (l >= h):
return
# If first element is smaller than
# last element, swap them
if (arr[l] > arr[h]):
temp = arr[l]
arr[l] = arr[h]
arr[h] = temp
# If there are more than 2 elements
# in the array
if (h - l + 1 > 2):
t = (h - l + 1) // 3
# Recursively sort the first
# 2/3 elements
stoogesort(arr, l, h - t)
# Recursively sort the last
# 2/3 elements
stoogesort(arr, l + t, h)
# Recursively sort the first
# 2/3 elements again
stoogesort(arr, l, h - t)
# Driver Code
arr = [ 2, 4, 5, 3, 1 ]
N = len(arr)
# Function Call
stoogesort(arr, 0, N - 1)
# Display the sorted array
for i in range(N):
print(arr[i], end = " ")
# This code is contributed by code_hunt
C#
// C# program for the
// stooge sort
using System;
class GFG{
// Function to implement
// stooge sort
static void stoogesort(int []arr,
int l, int h)
{
// Base Case
if (l >= h)
return;
// If first element is smaller
// than last element, swap them
if (arr[l] > arr[h])
{
int temp = arr[l];
arr[l] = arr[h];
arr[h] = temp;
}
// If there are more than
// 2 elements in the array
if (h - l + 1 > 2)
{
int t = (h - l + 1) / 3;
// Recursively sort the
// first 2/3 elements
stoogesort(arr, l, h - t);
// Recursively sort the
// last 2/3 elements
stoogesort(arr, l + t, h);
// Recursively sort the
// first 2/3 elements again
stoogesort(arr, l, h - t);
}
}
// Driver Code
public static void Main(String[] args)
{
int []arr = {2, 4, 5, 3, 1};
int N = arr.Length;
// Function Call
stoogesort(arr, 0, N - 1);
// Display the sorted array
for (int i = 0; i < N; i++)
{
Console.Write(arr[i] + " ");
}
}
}
// This code is contributed by Princi Singh
C++
// C++ program to implement Slow sort
#include
using namespace std;
// Function for swap two numbers using
// pointers
void swap(int* xp, int* yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// Function that implements Slow Sort
void slowSort(int A[], int i, int j)
{
// Base Case
if (i >= j)
return;
// Middle value
int m = (i + j) / 2;
// Recursively call with left half
slowSort(A, i, m);
// Recursively call with right half
slowSort(A, m + 1, j);
// Swap if first element
// is lower than second
if (A[j] < A[m]) {
swap(&A[j], &A[m]);
}
// Recursively call with whole
// array except maximum element
slowSort(A, i, j - 1);
}
// Function to print the array
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
// Driver Code
int main()
{
int arr[] = { 6, 8, 9, 4, 12, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
slowSort(arr, 0, N - 1);
// Display the sorted array
printArray(arr, N);
return 0;
}
Java
// Java program to implement Slow sort
import java.util.*;
class GFG
{
// Function that implements Slow Sort
static void slowSort(int A[], int i, int j)
{
// Base Case
if (i >= j)
return;
// Middle value
int m = (i + j) / 2;
// Recursively call with left half
slowSort(A, i, m);
// Recursively call with right half
slowSort(A, m + 1, j);
// Swap if first element
// is lower than second
if (A[j] < A[m])
{
int temp = A[j];
A[j] = A[m];
A[m] = temp;
}
// Recursively call with whole
// array except maximum element
slowSort(A, i, j - 1);
}
// Function to print the array
static void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
System.out.print(arr[i]+ " ");
System.out.println();
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 6, 8, 9, 4, 12, 1 };
int N = arr.length;
// Function call
slowSort(arr, 0, N - 1);
// Display the sorted array
printArray(arr, N);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program to implement Slow sort
# Function that implements Slow Sort
def slowSort(A, i, j):
# Base Case
if (i >= j):
return;
# Middle value
m = (i + j) // 2;
# Recursively call with left half
slowSort(A, i, m);
# Recursively call with right half
slowSort(A, m + 1, j);
# Swap if first element
# is lower than second
if (A[j] < A[m]):
temp = A[j];
A[j] = A[m];
A[m] = temp;
# Recursively call with whole
# array except maximum element
slowSort(A, i, j - 1);
# Function to prthe array
def printArray(arr, size):
i = 0;
for i in range(size):
print(arr[i], end=" ");
print();
# Driver Code
if __name__ == '__main__':
arr = [6, 8, 9, 4, 12, 1];
N = len(arr);
# Function call
slowSort(arr, 0, N - 1);
# Display the sorted array
printArray(arr, N);
# This code contributed by gauravrajput1
C#
// C# program to implement Slow sort
using System;
class GFG
{
// Function that implements Slow Sort
static void slowSort(int []A, int i, int j)
{
// Base Case
if (i >= j)
return;
// Middle value
int m = (i + j) / 2;
// Recursively call with left half
slowSort(A, i, m);
// Recursively call with right half
slowSort(A, m + 1, j);
// Swap if first element
// is lower than second
if (A[j] < A[m])
{
int temp = A[j];
A[j] = A[m];
A[m] = temp;
}
// Recursively call with whole
// array except maximum element
slowSort(A, i, j - 1);
}
// Function to print the array
static void printArray(int []arr, int size)
{
int i;
for (i = 0; i < size; i++)
Console.Write(arr[i] + " ");
Console.WriteLine();
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 6, 8, 9, 4, 12, 1 };
int N = arr.Length;
// Function call
slowSort(arr, 0, N - 1);
// Display the sorted array
printArray(arr, N);
}
}
// This code is contributed by 29AjayKumar
C++
// C++ program to implement Sleep sort
#ifdef _WIN32
// sleep() function for windows machine
#include
#else
// sleep() function for linux machine
#include
#endif
#include
#include
#include
using namespace std;
// Array for storing the sorted values
vector A;
// Function for print the array
void printArray(vector arr, int size)
{
int i;
for (i = 0; i < size; i++) {
cout << arr[i] << " ";
}
}
// The instruction set for a thread
void add(int x)
{
// Temporarily suspend execution
// of each thread for x amount
// of seconds
sleep(x);
// Every thead will wake up after
// a particular time and push the
// value in sorted array
A.push_back(x);
}
// Function for Sleep sort
void sleepSort(int arr[], int N)
{
vector threads;
for (int i = 0; i < N; i++) {
// New threads were launched by
// using function pointer as
// callable
threads.push_back(
thread(add, arr[i]));
}
// Waiting for each thread
// to finish execution
for (auto& th : threads) {
th.join();
}
// Display the sorted array
cout << "Array after sorting: ";
printArray(A, A.size());
}
// Driver Code
int main()
{
int arr[] = { 8, 9, 1, 4, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
// sleep_sort function call
sleepSort(arr, N);
return 0;
}
// To run compile using -pthread
// { 1, 3, 4, 8, 9}
C++
// C++ program to implement Bogo Sort
// using permutation
#include
using namespace std;
// Function to sort array using bogosort
void bogosort(int arr[], int N)
{
// Run the loop until
// array is not sorted
while (!is_sorted(arr, arr + N)) {
// All possible permutations
next_permutation(arr, arr + N);
}
}
// Driver Code
int main()
{
int arr[] = { 8, 9, 1, 4, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
bogosort(arr, N);
// Display the sorted array
cout << "Array after sorting ";
for (int i = 0; i < N; ++i) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
C++
// C++ program to implement Bogo Sort
// using random shuffle
#include
using namespace std;
// Function to check if array is
// sorted or not
bool isSorted(int a[], int N)
{
while (--N > 1) {
// Break condition for
// unsorted array
if (a[N] < a[N - 1])
return false;
}
return true;
}
// Function to generate permuatation
// of the array
void shuffle(int a[], int N)
{
for (int i = 0; i < N; i++)
swap(a[i], a[rand() % N]);
}
// Function to sort array using
// Bogo sort
void bogosort(int a[], int N)
{
// If array is not sorted
// then shuffle array again
while (!isSorted(a, N)) {
shuffle(a, N);
}
}
// Function to print the array
void printArray(int a[], int N)
{
for (int i = 0; i < N; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
// Driver Code
int main()
{
int a[] = { 3, 2, 5, 1, 0, 4 };
int N = sizeof a / sizeof a[0];
// Function Call
bogosort(a, N);
printf("Array after sorting:");
printArray(a, N);
return 0;
}
1 2 3 4 5
时间复杂度: O(N 2.709 )。因此,它甚至比时间复杂度为 O(N 2 ) 的冒泡排序还要慢。
慢排序:慢排序是乘法和投降的一个例子,这是一个关于分而治之的半开玩笑的笑话。慢排序通过递归地将数组除以一半并比较每个元素,将数组的最大元素存储在最后一个位置。然后它递归调用没有前一个最大元素的数组,并将新的最大元素存储在新的最后位置。下面是慢排序的步骤:
- 找到数组的最大值并将其放置在数组的末尾
- 对前N/2 个元素的最大值递归调用慢排序函数。
- 对剩余N/2 个元素的最大值递归调用慢排序函数。
- 找到这两个最大值中的最大值并将其存储在最后。
- 对除最大值外的整个数组递归调用慢排序函数。
- 打印排序后的数组。
下面是上述方法的实现:
C++
// C++ program to implement Slow sort
#include
using namespace std;
// Function for swap two numbers using
// pointers
void swap(int* xp, int* yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// Function that implements Slow Sort
void slowSort(int A[], int i, int j)
{
// Base Case
if (i >= j)
return;
// Middle value
int m = (i + j) / 2;
// Recursively call with left half
slowSort(A, i, m);
// Recursively call with right half
slowSort(A, m + 1, j);
// Swap if first element
// is lower than second
if (A[j] < A[m]) {
swap(&A[j], &A[m]);
}
// Recursively call with whole
// array except maximum element
slowSort(A, i, j - 1);
}
// Function to print the array
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
// Driver Code
int main()
{
int arr[] = { 6, 8, 9, 4, 12, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
slowSort(arr, 0, N - 1);
// Display the sorted array
printArray(arr, N);
return 0;
}
Java
// Java program to implement Slow sort
import java.util.*;
class GFG
{
// Function that implements Slow Sort
static void slowSort(int A[], int i, int j)
{
// Base Case
if (i >= j)
return;
// Middle value
int m = (i + j) / 2;
// Recursively call with left half
slowSort(A, i, m);
// Recursively call with right half
slowSort(A, m + 1, j);
// Swap if first element
// is lower than second
if (A[j] < A[m])
{
int temp = A[j];
A[j] = A[m];
A[m] = temp;
}
// Recursively call with whole
// array except maximum element
slowSort(A, i, j - 1);
}
// Function to print the array
static void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
System.out.print(arr[i]+ " ");
System.out.println();
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 6, 8, 9, 4, 12, 1 };
int N = arr.length;
// Function call
slowSort(arr, 0, N - 1);
// Display the sorted array
printArray(arr, N);
}
}
// This code is contributed by 29AjayKumar
蟒蛇3
# Python program to implement Slow sort
# Function that implements Slow Sort
def slowSort(A, i, j):
# Base Case
if (i >= j):
return;
# Middle value
m = (i + j) // 2;
# Recursively call with left half
slowSort(A, i, m);
# Recursively call with right half
slowSort(A, m + 1, j);
# Swap if first element
# is lower than second
if (A[j] < A[m]):
temp = A[j];
A[j] = A[m];
A[m] = temp;
# Recursively call with whole
# array except maximum element
slowSort(A, i, j - 1);
# Function to prthe array
def printArray(arr, size):
i = 0;
for i in range(size):
print(arr[i], end=" ");
print();
# Driver Code
if __name__ == '__main__':
arr = [6, 8, 9, 4, 12, 1];
N = len(arr);
# Function call
slowSort(arr, 0, N - 1);
# Display the sorted array
printArray(arr, N);
# This code contributed by gauravrajput1
C#
// C# program to implement Slow sort
using System;
class GFG
{
// Function that implements Slow Sort
static void slowSort(int []A, int i, int j)
{
// Base Case
if (i >= j)
return;
// Middle value
int m = (i + j) / 2;
// Recursively call with left half
slowSort(A, i, m);
// Recursively call with right half
slowSort(A, m + 1, j);
// Swap if first element
// is lower than second
if (A[j] < A[m])
{
int temp = A[j];
A[j] = A[m];
A[m] = temp;
}
// Recursively call with whole
// array except maximum element
slowSort(A, i, j - 1);
}
// Function to print the array
static void printArray(int []arr, int size)
{
int i;
for (i = 0; i < size; i++)
Console.Write(arr[i] + " ");
Console.WriteLine();
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 6, 8, 9, 4, 12, 1 };
int N = arr.Length;
// Function call
slowSort(arr, 0, N - 1);
// Display the sorted array
printArray(arr, N);
}
}
// This code is contributed by 29AjayKumar
1 4 6 8 9 12
时间复杂度:
- 基本情况: O(N ((log N)/(2+e))其中,e > 0
- 平均情况: O(N (log(N)/2) )
即使是最好的情况也比冒泡排序更糟糕。它的效率低于 Stooge 排序。
睡眠排序:以下是Stooge排序的步骤:
- 为输入数组中的每个元素创建不同的线程,然后每个线程休眠的时间与相应数组元素的值成正比。
- 睡眠时间最少的线程首先唤醒,然后打印数字,然后打印第二个最少的元素,依此类推。
- 最大的元素在很长一段时间后唤醒,然后该元素最后打印。因此,输出是一个排序的。
所有这些多线程过程都发生在后台和操作系统的核心
下面是上述方法的实现:
C++
// C++ program to implement Sleep sort
#ifdef _WIN32
// sleep() function for windows machine
#include
#else
// sleep() function for linux machine
#include
#endif
#include
#include
#include
using namespace std;
// Array for storing the sorted values
vector A;
// Function for print the array
void printArray(vector arr, int size)
{
int i;
for (i = 0; i < size; i++) {
cout << arr[i] << " ";
}
}
// The instruction set for a thread
void add(int x)
{
// Temporarily suspend execution
// of each thread for x amount
// of seconds
sleep(x);
// Every thead will wake up after
// a particular time and push the
// value in sorted array
A.push_back(x);
}
// Function for Sleep sort
void sleepSort(int arr[], int N)
{
vector threads;
for (int i = 0; i < N; i++) {
// New threads were launched by
// using function pointer as
// callable
threads.push_back(
thread(add, arr[i]));
}
// Waiting for each thread
// to finish execution
for (auto& th : threads) {
th.join();
}
// Display the sorted array
cout << "Array after sorting: ";
printArray(A, A.size());
}
// Driver Code
int main()
{
int arr[] = { 8, 9, 1, 4, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
// sleep_sort function call
sleepSort(arr, N);
return 0;
}
// To run compile using -pthread
// { 1, 3, 4, 8, 9}
Array after sorting 1 3 4 8 9
时间复杂度: O(max(input) + N) 其中,input = 数组元素的值
其他算法的时间复杂度取决于数据的数量,但对于睡眠排序,则取决于数据的数量。该算法不适用于负数,因为线程无法休眠负数的时间。
Bogo Sort:该算法有两种版本:一种是枚举所有排列直到遇到已排序的排列,另一种是随机排列其输入的随机版本。
示例 1:
C++
// C++ program to implement Bogo Sort
// using permutation
#include
using namespace std;
// Function to sort array using bogosort
void bogosort(int arr[], int N)
{
// Run the loop until
// array is not sorted
while (!is_sorted(arr, arr + N)) {
// All possible permutations
next_permutation(arr, arr + N);
}
}
// Driver Code
int main()
{
int arr[] = { 8, 9, 1, 4, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
bogosort(arr, N);
// Display the sorted array
cout << "Array after sorting ";
for (int i = 0; i < N; ++i) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
Array after sorting 1 3 4 8 9
时间复杂度:
- 基本情况: O(N)
- 平均情况: O(N!)
- 最坏情况: O(N!)
示例 2:
C++
// C++ program to implement Bogo Sort
// using random shuffle
#include
using namespace std;
// Function to check if array is
// sorted or not
bool isSorted(int a[], int N)
{
while (--N > 1) {
// Break condition for
// unsorted array
if (a[N] < a[N - 1])
return false;
}
return true;
}
// Function to generate permuatation
// of the array
void shuffle(int a[], int N)
{
for (int i = 0; i < N; i++)
swap(a[i], a[rand() % N]);
}
// Function to sort array using
// Bogo sort
void bogosort(int a[], int N)
{
// If array is not sorted
// then shuffle array again
while (!isSorted(a, N)) {
shuffle(a, N);
}
}
// Function to print the array
void printArray(int a[], int N)
{
for (int i = 0; i < N; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
// Driver Code
int main()
{
int a[] = { 3, 2, 5, 1, 0, 4 };
int N = sizeof a / sizeof a[0];
// Function Call
bogosort(a, N);
printf("Array after sorting:");
printArray(a, N);
return 0;
}
Array after sorting:0 1 2 3 4 5
时间复杂度:
- 基本情况: O(N)
- 平均情况: O(N*N!)
- 最坏情况: O(∞)
显然,在最坏的情况下,使用随机 shuffle 的 Bogo 排序需要无限量的时间来对数组进行排序,我们可以说这是最慢的排序算法。但是 Bogo Sort 的问题在于它违反了复杂性分析中的一些规则。其中一条规则是,您实际上必须朝着目标前进。例如,您不能仅仅通过放置延迟循环来浪费时间。慢排序或走狗排序算法实际上永远不会出错。一旦交换了两个节点,节点之间的顺序就会正确,并且它们的顺序不会颠倒。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live