Slow Sort

Slow Sort :

Author(s) : Andrei Broder and Jorge Stolfi Date : 1984

Slowsort is a humorous and intentionally inefficient sorting algorithm designed as a parody of optimal sorting algorithms. It operates on the principle of 'multiply and surrender', the opposite of the divide-and-conquer approach used by efficient algorithms like quicksort. Instead of dividing the data structure for faster sorting, slowsort performs a single comparison between two randomly chosen elements. If they are in the wrong order (larger element comes before the smaller one), the algorithm simply surrenders and restarts the entire sorting process from scratch. This repetitive comparison and restart approach guarantee a very slow sorting time, making slowsort unsuitable for any practical use. Its purpose lies in highlighting the importance of efficient sorting algorithms and demonstrating the significant performance difference between well-designed and deliberately inefficient approaches.

Time Complexity O(n log(n))
Best Case O(n log(n))
Worst Case O(n log(n))
Space Complexity O(n)
Stable No

Code Integration:

* the code might contain some bugs or may not work correctly

Python JavaScript Java Go C
def slowsort(arr, lo, hi):
	if lo < hi:
		mid = (lo + hi) // 2
		max_left = slowsort(arr, lo, mid)
		max_right = slowsort(arr, mid + 1, hi)
		arr[hi] = max(max_left, max_right)
		slowsort(arr, lo, hi - 1)
	return arr[hi]

# Test array
arr = [12, 11, 13, 5, 6, 7]
print('Sorted array is:', slowsort(arr.copy(), 0, len(arr) - 1))
function slowsort(arr, lo, hi) {
	if (lo < hi) {
		const mid = Math.floor((lo + hi) / 2);
		const maxLeft = slowsort(arr, lo, mid);
		const maxRight = slowsort(arr, mid + 1, hi);
		arr[hi] = Math.max(maxLeft, maxRight);
		slowsort(arr, lo, hi - 1);
	}
	return arr[hi];
}

// Test array
const arr = [12, 11, 13, 5, 6, 7];
console.log('Sorted array is:', slowsort(arr.slice(), 0, arr.length - 1));
public class SlowSort {
	public static void slowsort(int[] arr, int lo, int hi) {
		if (lo < hi) {
			int mid = (lo + hi) / 2;
			int maxLeft = slowsort(arr, lo, mid);
			int maxRight = slowsort(arr, mid + 1, hi);
			arr[hi] = Math.max(maxLeft, maxRight);
			slowsort(arr, lo, hi - 1);
		}
	}

	public static void main(String[] args) {
		int[] arr = {12, 11, 13, 5, 6, 7};
		slowsort(arr, 0, arr.length - 1);
		System.out.print("Sorted array is: ");
		for (int num : arr) {
			System.out.print(num + " ");
		}
	}
}
package main

import (
	"fmt"
)

func slowsort(arr []int, lo, hi int) []int {
	if lo < hi {
		mid := (lo + hi) / 2
		maxLeft := slowsort(arr[:mid+1], lo, mid)
		maxRight := slowsort(arr[mid+1:], mid+1, hi)
		arr[hi] = max(maxLeft[len(maxLeft)-1], maxRight[len(maxRight)-1])
		slowsort(arr[:hi], lo, hi-1)
	}
	return arr
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func main() {
	arr := []int{12, 11, 13, 5, 6, 7}
	sortedArr := slowsort(arr, 0, len(arr)-1)
	fmt.Println("Sorted array is:", sortedArr)
}
#include <stdio.h>

int max(int a, int b) {
	return (a > b) ? a : b;
}

void slowsort(int arr[], int lo, int hi) {
	if (lo < hi) {
		int mid = (lo + hi) / 2;
		slowsort(arr, lo, mid);
		slowsort(arr, mid + 1, hi);
		arr[hi] = max(arr[hi], max(arr[lo], arr[mid]));
		slowsort(arr, lo, hi - 1);
	}
}

int main() {
	int arr[] = {12, 11, 13, 5, 6, 7};
	int n = sizeof(arr) / sizeof(arr[0]);
	slowsort(arr, 0, n - 1);
	printf("Sorted array is: ");
	for (int i = 0; i < n; i++) {
		printf("%d ", arr[i]);
	}
	printf("\n");
	return 0;
}
light dark