{
  "format": "tokbit",
  "version": 1,
  "name": "Sort with swaps",
  "main": [
    {
      "id": "u1",
      "kind": "declare",
      "name": "numbers",
      "type": "list[int]",
      "init": "[]"
    },
    {
      "id": "u2",
      "kind": "for",
      "variable": "i",
      "range": "8",
      "body": [
        {
          "id": "u3",
          "kind": "call",
          "call": "numbers.append(randint(1, 99))"
        }
      ],
      "comment": "Eight random numbers."
    },
    {
      "id": "u4",
      "kind": "output",
      "values": "\"Before:\", numbers"
    },
    {
      "id": "u5",
      "kind": "declare",
      "name": "n",
      "type": "int",
      "init": "len(numbers)"
    },
    {
      "id": "u6",
      "kind": "declare",
      "name": "temp",
      "type": "int",
      "init": "0"
    },
    {
      "id": "u7",
      "kind": "declare",
      "name": "swaps",
      "type": "int",
      "init": "0"
    },
    {
      "id": "u8",
      "kind": "for",
      "variable": "k",
      "range": "n - 1",
      "body": [
        {
          "id": "u9",
          "kind": "for",
          "variable": "i",
          "range": "n - 1 - k",
          "body": [
            {
              "id": "u10",
              "kind": "if",
              "condition": "numbers[i] > numbers[i + 1]",
              "then": [
                {
                  "id": "u11",
                  "kind": "assign",
                  "target": "temp",
                  "value": "numbers[i]"
                },
                {
                  "id": "u12",
                  "kind": "assign",
                  "target": "numbers[i]",
                  "value": "numbers[i + 1]"
                },
                {
                  "id": "u13",
                  "kind": "assign",
                  "target": "numbers[i + 1]",
                  "value": "temp"
                },
                {
                  "id": "u14",
                  "kind": "assign",
                  "target": "swaps",
                  "value": "swaps + 1"
                }
              ],
              "else": [],
              "comment": "Neighbours in the wrong order swap through a helper variable."
            }
          ]
        }
      ],
      "comment": "Each pass moves the largest remaining number to the end."
    },
    {
      "id": "u15",
      "kind": "output",
      "values": "\"After:\", numbers"
    },
    {
      "id": "u16",
      "kind": "output",
      "values": "\"Swaps:\", swaps"
    }
  ],
  "functions": [],
  "comment": "Solution to the task “How many swaps”: Count how many times neighbours were swapped and print it. How many swaps does an already sorted list need?"
}
