func bubbleSort(array: [Int]) -> Void { var arr = array var noSwap: Bool = true repeat { /** Outer Loop - Controls how many full pass are needed to perform the sorting - It continues until a complete pass is made without any swaps, indicating that the array is sorted. */ for item in arr { /** Inner Loop - actually responsible for comparing adjacent elements and swap them if they are in the wrong order. */ for (idx, element) in arr.enumerated() { // If left element > right element if idx+1 < arr.count { print("\(arr[idx]) > \(arr[idx+1]) ? ") if arr[idx] > arr[idx+1] { // Swap let temp = arr[idx] arr[idx] = arr[idx+1] arr[idx+1] = temp // Flag noSwap = false print(arr) } else { print("no swap") } } } } } while noSwap print("sorted arr: \(arr)")}bubbleSort(array: [4,8,6,7,3,2,5,1])