I Can't Believe It Can Sort!
The I can't believe it can sort algorithm is a very simple but inefficient algorithm. The reason that it is named so is because if you were to look at the pseudocode, you won't think that it can actually sort but it does. Here is the pseudocode:
for i = 1 to n do
for j = 1 to n do
if A[i] < A[j] then
swap A[i] and A[j]
The pseudocode was lifted, verbatim, from the research paper "Is this the simplest (and most surprising) sorting algorithm ever?" by Stanley P. Y. Fung. You can find the paper here.
As evidenced by the pseudocode, I Can't Believe It Can Sort is a simple and inefficient sorting algorithm that uses nested loops to repeatedly compare and swap elements until the array is sorted. This algorithm has a time complexity of O(n^2), making it much less efficient than well-known sorting algorithms like quicksort or mergesort, which have better average and worst-case performance.
Here is a java implementation of this algorithm:
import java.util.*;
import java.lang.*;
class ICantBelieveItCanSort{
public static void iCantBelieveItCanSort(int[] arr){
int n = arr.length;
for(int i=0; i<n; i++){
for(int j=0;j <n;j++){
if(arr[i]<arr[j]){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
public static void main(String[] args){
int[] arr = {1,3,5,3,67,7,2,8,9,34,54,22,65,21};
iCantBelieveItCanSort(arr);
for(int i : arr)
System.out.print(i + " ");
}
}
