---
title: "I Can't Believe It Can Sort!"
slug: i-cant-believe-it-can-sort
url: https://listedarticles.com/articles/i-cant-believe-it-can-sort
canonical_url: https://yugen-tsuki.github.io/unbelievablesort.html
content_type: tutorial
language: en
published_at: 2026-11-16T10:54:20.626Z
updated_at: 2026-11-16T10:54:20.626Z
author: "Swasti Choubey"
authored_by: human
publisher: "Yugen"
publisher_url: https://yugen-tsuki.github.io/
topics: ["algorithms", "sorting", "java"]
license: all-rights-reserved
word_count: 256
reading_minutes: 1
citation: "Swasti Choubey, Yugen. \"I Can't Believe It Can Sort!.\" 16 Nov 2026. https://yugen-tsuki.github.io/unbelievablesort.html (all-rights-reserved)"
---

# I Can't Believe It Can Sort!

> A short explainer of the 'I can't believe it can sort' algorithm from Stanley P. Y. Fung's paper: a deceptively simple O(n^2) nested-loop swap sort, with pseudocode and a Java implementation.

# 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](https://arxiv.org/pdf/2110.01111.pdf).

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 + " ");
    }
                                
}
```

By Swasti Choubey
