Last active
May 8, 2022 11:48
-
-
Save enakai00/e1c4d02c18be4ed2c82a1a2a3490aa81 to your computer and use it in GitHub Desktop.
Explanation of https://arxiv.org/abs/2110.01111
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Explanation of https://arxiv.org/abs/2110.01111", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyMIW4z+0wx72iv3HpC4sPoK", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/enakai00/e1c4d02c18be4ed2c82a1a2a3490aa81/explanation-of-https-arxiv-org-abs-2110-01111.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import random\n", | |
"\n", | |
"a = [random.randint(0, 10) for _ in range(10)]\n", | |
"n = len(a)\n", | |
"\n", | |
"for i in range(0, n):\n", | |
"\n", | |
" # Loop invariant: a[:i] is sorted.\n", | |
" assert a[:i] == sorted(a[:i])\n", | |
"\n", | |
" # The following loop inserts a[i] into a[:i] so that a[:i+1] is sorted.\n", | |
" for j in range(0, i):\n", | |
" if a[i] < a[j]:\n", | |
" a[i], a[j] = a[j], a[i]\n", | |
"\n", | |
" # Loop invariant: a[:i+1] is sorted.\n", | |
" assert a[:i+1] == sorted(a[:i+1])\n", | |
"\n", | |
" # The following loop swaps a[i] and the max element of a[i+1:]\n", | |
" # This is unnecessary, but no harm as it doesn't break the condition that a[:i+1] is sorted.\n", | |
" for j in range(i, n):\n", | |
" if a[i] < a[j]:\n", | |
" a[i], a[j] = a[j], a[i]\n", | |
"\n", | |
" # Loop invariant: a[:i+1] is sorted.\n", | |
" assert a[:i+1] == sorted(a[:i+1])\n", | |
"\n", | |
" print(i, a)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "oG8L95Evs90V", | |
"outputId": "556715f1-0c32-4708-a712-b9124174680f" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"0 [10, 2, 5, 4, 6, 6, 6, 8, 3, 7]\n", | |
"1 [2, 10, 5, 4, 6, 6, 6, 8, 3, 7]\n", | |
"2 [2, 5, 10, 4, 6, 6, 6, 8, 3, 7]\n", | |
"3 [2, 4, 5, 10, 6, 6, 6, 8, 3, 7]\n", | |
"4 [2, 4, 5, 6, 10, 6, 6, 8, 3, 7]\n", | |
"5 [2, 4, 5, 6, 6, 10, 6, 8, 3, 7]\n", | |
"6 [2, 4, 5, 6, 6, 6, 10, 8, 3, 7]\n", | |
"7 [2, 4, 5, 6, 6, 6, 8, 10, 3, 7]\n", | |
"8 [2, 3, 4, 5, 6, 6, 6, 8, 10, 7]\n", | |
"9 [2, 3, 4, 5, 6, 6, 6, 7, 8, 10]\n" | |
] | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment