← back to the blog


Swift 5, Swift 5.1 Bubble Sort algorithm implementation

Posted on January 31st, 2020 in Swift by George

 

Bubble Sort algorithm example in Swift 5 and Swift 5.1

Bubble sort algorithm implementation in Swift 5 and Swift 5.1 with explanatory comments and  explicit expanded output 

 


var exampleArray = [5,688,8,15,3,4,6,8,33,2,1, 5, 7, 100, 7];

struct MyBBSort {
    
    func bubbleSort(_ array: [Int]) -> [Int] {
        
        var mutableArray = array;
        let length = array.count;
        var tempVal = 0;
        
        for outerIteration in 1...length  {
            print("Iteration nr: \(outerIteration) of the outer loop - For each item of the array run a loop where find the first value bigger than the subsequent value and swap them around ");
            for innerIteration in 0...length - 2 {
                print("\(innerIteration) Inner loop ")
                if(mutableArray[innerIteration] > mutableArray[innerIteration + 1]) {
                    tempVal = mutableArray[innerIteration + 1];
                    print("\(mutableArray[innerIteration]) --value to swap over subseqent value -- \(mutableArray[innerIteration + 1]) ");
                    mutableArray[innerIteration + 1] = mutableArray[innerIteration];
                    mutableArray[innerIteration] = tempVal;
                }
            }
        }
        return mutableArray;
    }
}

//init struct
var myStruct = MyBBSort();

let result = myStruct.bubbleSort(exampleArray)
print(result)

 

See bellow output with easy to understand prints:

 

Iteration nr: 1 of outer loop - For each item of the array run a loop where find the first value bigger than the subsequent value and swap them around 
0 Inner loop 
1 Inner loop 
688 --value to swap over subseqent value -- 8 
2 Inner loop 
688 --value to swap over subseqent value -- 15 
3 Inner loop 
688 --value to swap over subseqent value -- 3 
4 Inner loop 
688 --value to swap over subseqent value -- 4 
5 Inner loop 
688 --value to swap over subseqent value -- 6 
6 Inner loop 
688 --value to swap over subseqent value -- 8 
7 Inner loop 
688 --value to swap over subseqent value -- 33 
8 Inner loop 
688 --value to swap over subseqent value -- 2 
9 Inner loop 
688 --value to swap over subseqent value -- 1 
10 Inner loop 
688 --value to swap over subseqent value -- 5 
11 Inner loop 
688 --value to swap over subseqent value -- 7 
12 Inner loop 
688 --value to swap over subseqent value -- 100 
13 Inner loop 
688 --value to swap over subseqent value -- 7 
Iteration nr: 2 of outer loop - For each item of the array run a loop where find the first value bigger than the subsequent value and swap them around 
0 Inner loop 
1 Inner loop 
2 Inner loop 
15 --value to swap over subseqent value -- 3 
3 Inner loop 
15 --value to swap over subseqent value -- 4 
4 Inner loop 
15 --value to swap over subseqent value -- 6 
5 Inner loop 
15 --value to swap over subseqent value -- 8 
6 Inner loop 
7 Inner loop 
33 --value to swap over subseqent value -- 2 
8 Inner loop 
33 --value to swap over subseqent value -- 1 
9 Inner loop 
33 --value to swap over subseqent value -- 5 
10 Inner loop 
33 --value to swap over subseqent value -- 7 
11 Inner loop 
12 Inner loop 
100 --value to swap over subseqent value -- 7 
13 Inner loop 
Iteration nr: 3 of outer loop - For each item of the array run a loop where find the first value bigger than the subsequent value and swap them around 
0 Inner loop 
1 Inner loop 
8 --value to swap over subseqent value -- 3 
2 Inner loop 
8 --value to swap over subseqent value -- 4 
3 Inner loop 
8 --value to swap over subseqent value -- 6 
4 Inner loop 
5 Inner loop 
6 Inner loop 
15 --value to swap over subseqent value -- 2 
7 Inner loop 
15 --value to swap over subseqent value -- 1 
8 Inner loop 
15 --value to swap over subseqent value -- 5 
9 Inner loop 
15 --value to swap over subseqent value -- 7 
10 Inner loop 
11 Inner loop 
33 --value to swap over subseqent value -- 7 
12 Inner loop 
13 Inner loop 
Iteration nr: 4 of outer loop - For each item of the array run a loop where find the first value bigger than the subsequent value and swap them around 
0 Inner loop 
5 --value to swap over subseqent value -- 3 
1 Inner loop 
5 --value to swap over subseqent value -- 4 
2 Inner loop 
3 Inner loop 
4 Inner loop 
5 Inner loop 
8 --value to swap over subseqent value -- 2 
6 Inner loop 
8 --value to swap over subseqent value -- 1 
7 Inner loop 
8 --value to swap over subseqent value -- 5 
8 Inner loop 
8 --value to swap over subseqent value -- 7 
9 Inner loop 
10 Inner loop 
15 --value to swap over subseqent value -- 7 
11 Inner loop 
12 Inner loop 
13 Inner loop 
Iteration nr: 5 of outer loop - For each item of the array run a loop where find the first value bigger than the subsequent value and swap them around 
0 Inner loop 
1 Inner loop 
2 Inner loop 
3 Inner loop 
4 Inner loop 
8 --value to swap over subseqent value -- 2 
5 Inner loop 
8 --value to swap over subseqent value -- 1 
6 Inner loop 
8 --value to swap over subseqent value -- 5 
7 Inner loop 
8 --value to swap over subseqent value -- 7 
8 Inner loop 
9 Inner loop 
8 --value to swap over subseqent value -- 7 
10 Inner loop 
11 Inner loop 
12 Inner loop 
13 Inner loop 
Iteration nr: 6 of outer loop - For each item of the array run a loop where find the first value bigger than the subsequent value and swap them around 
0 Inner loop 
1 Inner loop 
2 Inner loop 
3 Inner loop 
6 --value to swap over subseqent value -- 2 
4 Inner loop 
6 --value to swap over subseqent value -- 1 
5 Inner loop 
6 --value to swap over subseqent value -- 5 
6 Inner loop 
7 Inner loop 
8 Inner loop 
8 --value to swap over subseqent value -- 7 
9 Inner loop 
10 Inner loop 
11 Inner loop 
12 Inner loop 
13 Inner loop 
Iteration nr: 7 of outer loop - For each item of the array run a loop where find the first value bigger than the subsequent value and swap them around 
0 Inner loop 
1 Inner loop 
2 Inner loop 
5 --value to swap over subseqent value -- 2 
3 Inner loop 
5 --value to swap over subseqent value -- 1 
4 Inner loop 
5 Inner loop 
6 Inner loop 
7 Inner loop 
8 Inner loop 
9 Inner loop 
10 Inner loop 
11 Inner loop 
12 Inner loop 
13 Inner loop 
Iteration nr: 8 of outer loop - For each item of the array run a loop where find the first value bigger than the subsequent value and swap them around 
0 Inner loop 
1 Inner loop 
4 --value to swap over subseqent value -- 2 
2 Inner loop 
4 --value to swap over subseqent value -- 1 
3 Inner loop 
4 Inner loop 
5 Inner loop 
6 Inner loop 
7 Inner loop 
8 Inner loop 
9 Inner loop 
10 Inner loop 
11 Inner loop 
12 Inner loop 
13 Inner loop 
Iteration nr: 9 of outer loop - For each item of the array run a loop where find the first value bigger than the subsequent value and swap them around 
0 Inner loop 
3 --value to swap over subseqent value -- 2 
1 Inner loop 
3 --value to swap over subseqent value -- 1 
2 Inner loop 
3 Inner loop 
4 Inner loop 
5 Inner loop 
6 Inner loop 
7 Inner loop 
8 Inner loop 
9 Inner loop 
10 Inner loop 
11 Inner loop 
12 Inner loop 
13 Inner loop 
Iteration nr: 10 of outer loop - For each item of the array run a loop where find the first value bigger than the subsequent value and swap them around 
0 Inner loop 
2 --value to swap over subseqent value -- 1 
1 Inner loop 
2 Inner loop 
3 Inner loop 
4 Inner loop 
5 Inner loop 
6 Inner loop 
7 Inner loop 
8 Inner loop 
9 Inner loop 
10 Inner loop 
11 Inner loop 
12 Inner loop 
13 Inner loop 
Iteration nr: 11 of outer loop - For each item of the array run a loop where find the first value bigger than the subsequent value and swap them around 
0 Inner loop 
1 Inner loop 
2 Inner loop 
3 Inner loop 
4 Inner loop 
5 Inner loop 
6 Inner loop 
7 Inner loop 
8 Inner loop 
9 Inner loop 
10 Inner loop 
11 Inner loop 
12 Inner loop 
13 Inner loop 
Iteration nr: 12 of outer loop - For each item of the array run a loop where find the first value bigger than the subsequent value and swap them around 
0 Inner loop 
1 Inner loop 
2 Inner loop 
3 Inner loop 
4 Inner loop 
5 Inner loop 
6 Inner loop 
7 Inner loop 
8 Inner loop 
9 Inner loop 
10 Inner loop 
11 Inner loop 
12 Inner loop 
13 Inner loop 
Iteration nr: 13 of outer loop - For each item of the array run a loop where find the first value bigger than the subsequent value and swap them around 
0 Inner loop 
1 Inner loop 
2 Inner loop 
3 Inner loop 
4 Inner loop 
5 Inner loop 
6 Inner loop 
7 Inner loop 
8 Inner loop 
9 Inner loop 
10 Inner loop 
11 Inner loop 
12 Inner loop 
13 Inner loop 
Iteration nr: 14 of outer loop - For each item of the array run a loop where find the first value bigger than the subsequent value and swap them around 
0 Inner loop 
1 Inner loop 
2 Inner loop 
3 Inner loop 
4 Inner loop 
5 Inner loop 
6 Inner loop 
7 Inner loop 
8 Inner loop 
9 Inner loop 
10 Inner loop 
11 Inner loop 
12 Inner loop 
13 Inner loop 
Iteration nr: 15 of outer loop - For each item of the array run a loop where find the first value bigger than the subsequent value and swap them around 
0 Inner loop 
1 Inner loop 
2 Inner loop 
3 Inner loop 
4 Inner loop 
5 Inner loop 
6 Inner loop 
7 Inner loop 
8 Inner loop 
9 Inner loop 
10 Inner loop 
11 Inner loop 
12 Inner loop 
13 Inner loop 
[1, 2, 3, 4, 5, 5, 6, 7, 7, 8, 8, 15, 33, 100, 688]