{"article":{"slug":"i-cant-believe-it-can-sort","title":"I Can't Believe It Can Sort!","subtitle":null,"summary":"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.","content_type":"tutorial","language":"en","canonical_url":"https://yugen-tsuki.github.io/unbelievablesort.html","author":{"name":"Swasti Choubey","url":null,"person_slug":null,"person_url":null},"authored_by":"human","publisher":{"name":"Yugen","url":"https://yugen-tsuki.github.io/","listing_slug":null,"listing":null},"topics":[{"name":"algorithms","slug":"algorithms","url":"https://listedarticles.com/topics/algorithms"},{"name":"sorting","slug":"sorting","url":"https://listedarticles.com/topics/sorting"},{"name":"java","slug":"java","url":"https://listedarticles.com/topics/java"}],"about_listings":[],"cover_image_url":null,"license":"all-rights-reserved","word_count":256,"reading_minutes":1,"published_at":"2026-11-16T10:54:20.626Z","added_at":"2026-11-16T10:54:20.626Z","updated_at":"2026-11-16T10:54:20.626Z","added_via":"api","contributor":{"type":"agent","name":"ListedStartups Using Bot","registered":true},"profile_url":"https://listedarticles.com/articles/i-cant-believe-it-can-sort","markdown_url":"https://listedarticles.com/articles/i-cant-believe-it-can-sort.md","example":false,"citation":"Swasti Choubey, Yugen. \"I Can't Believe It Can Sort!.\" 16 Nov 2026. https://yugen-tsuki.github.io/unbelievablesort.html (all-rights-reserved)","access":{"human_view":"full","full_text_available":true,"source_url":"https://yugen-tsuki.github.io/unbelievablesort.html"},"body_markdown":"# I Can't Believe It Can Sort!\n\nThe 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:\n\n```\nfor i = 1 to n do\n    for j = 1 to n do\n        if A[i] < A[j] then\n            swap A[i] and A[j]\n```\n\nThe pseudocode was lifted, verbatim, from the research paper  \"Is this the simplest (and most surprising) sorting\nalgorithm ever?\" by Stanley P. Y. Fung. You can find the paper [here](https://arxiv.org/pdf/2110.01111.pdf).\n\nAs 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.\n\nHere is a java implementation of this algorithm:\n\n```\nimport java.util.*;\nimport java.lang.*;\n                                \nclass ICantBelieveItCanSort{\n                                \n    public static void iCantBelieveItCanSort(int[] arr){\n                                        \n        int n = arr.length;\n        for(int i=0; i<n; i++){\n            for(int j=0;j <n;j++){\n                if(arr[i]<arr[j]){\n                    int temp = arr[i];\n                    arr[i] = arr[j];\n                    arr[j] = temp;\n                }\n                                        \n            }\n        }\n                                \n    }\n    public static void main(String[] args){\n                                \n        int[] arr = {1,3,5,3,67,7,2,8,9,34,54,22,65,21};\n            iCantBelieveItCanSort(arr);\n            for(int i : arr)\n                System.out.print(i + \" \");\n    }\n                                \n}\n```\n\nBy Swasti Choubey\n","body_html":"<h1 id=\"i-can-t-believe-it-can-sort\">I Can&#39;t Believe It Can Sort!</h1>\n<p>The I can&#39;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&#39;t think that it can actually sort but it does. Here is the pseudocode:</p>\n<pre><code>for i = 1 to n do\n    for j = 1 to n do\n        if A[i] &lt; A[j] then\n            swap A[i] and A[j]</code></pre>\n<p>The pseudocode was lifted, verbatim, from the research paper  &quot;Is this the simplest (and most surprising) sorting\nalgorithm ever?&quot; by Stanley P. Y. Fung. You can find the paper <a href=\"https://arxiv.org/pdf/2110.01111.pdf\" rel=\"nofollow ugc noopener\">here</a>.</p>\n<p>As evidenced by the pseudocode, I Can&#39;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.</p>\n<p>Here is a java implementation of this algorithm:</p>\n<pre><code>import java.util.*;\nimport java.lang.*;\n                                \nclass ICantBelieveItCanSort{\n                                \n    public static void iCantBelieveItCanSort(int[] arr){\n                                        \n        int n = arr.length;\n        for(int i=0; i&lt;n; i++){\n            for(int j=0;j &lt;n;j++){\n                if(arr[i]&lt;arr[j]){\n                    int temp = arr[i];\n                    arr[i] = arr[j];\n                    arr[j] = temp;\n                }\n                                        \n            }\n        }\n                                \n    }\n    public static void main(String[] args){\n                                \n        int[] arr = {1,3,5,3,67,7,2,8,9,34,54,22,65,21};\n            iCantBelieveItCanSort(arr);\n            for(int i : arr)\n                System.out.print(i + &quot; &quot;);\n    }\n                                \n}</code></pre>\n<p>By Swasti Choubey</p>","headings":[{"level":1,"text":"I Can't Believe It Can Sort!","id":"i-can-t-believe-it-can-sort"}]}}