Skip to content

Commit b90632c

Browse files
committed
added imlementation information on readme
1 parent a9b6c5f commit b90632c

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

Bubble Sort/README.markdown

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Bubble Sort
22

3-
Bubble sort is a sorting algorithm that is implemented by starting in the beginning of the array and swapping the first two elements only if the first element is greater than the second element. This comparison is then moved onto the next pair and so on and so forth. This is done until the array is completely sorted. The smaller items slowly “bubble” up to the beginning of the array.
3+
Bubble sort is a sorting algorithm that is implemented by starting in the beginning of the array and swapping the first two elements only if the first element is greater than the second element. This comparison is then moved onto the next pair and so on and so forth. This is done until the array is completely sorted. The smaller items slowly “bubble” up to the beginning of the array.Sometimes this algorithm is refered as Sinking sort, due to the larger, or heavier elements sinking to the end of the array.
44

55
##### Runtime:
66
- Average: O(N^2)
@@ -12,3 +12,73 @@ Bubble sort is a sorting algorithm that is implemented by starting in the beginn
1212
### Implementation:
1313

1414
The implementation will not be shown as the average and worst runtimes show that this is a very inefficient algorithm. However, having a grasp of the concept will help you understand the basics of simple sorting algorithms.
15+
16+
Bubble sort is a very simple sorting algorithm, it consists in comparing pairs of adjacent elements in the array, if the first element is larger, swap them, otherwise, you do nothing and go for the next comparison.
17+
This is accomplished by looking through the array `n` times, `n` being the amount of elements in the array.
18+
19+
#### Example
20+
Let us take the array `[5, 1, 4, 2, 8]`, and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Three passes will be required.
21+
22+
##### First Pass
23+
[ **5 1** 4 2 8 ] -> [ **1 5** 4 2 8 ], Here, algorithm compares the first two elements, and swaps since 5 > 1.
24+
25+
[ 1 **5 4** 2 8 ] -> [ 1 **4 5** 2 8 ], Swap since 5 > 4
26+
27+
[ 1 4 **5 2** 8 ] -> [ 1 4 **2 5** 8 ], Swap since 5 > 2
28+
29+
[ 1 4 2 **5 8** ] -> [ 1 4 2 **5 8** ], Now, since these elements are already in order (8 > 5), algorithm does not swap them.
30+
31+
##### Second Pass
32+
[ **1 4** 2 5 8 ] -> [ **1 4** 2 5 8 ]
33+
34+
[ 1 **4 2** 5 8 ] -> [ 1 **2 4** 5 8 ], Swap since 4 > 2
35+
36+
[ 1 2 **4 5** 8 ] -> [ 1 2 **4 5** 8 ]
37+
38+
[ 1 2 4 **5 8** ] -> [ 1 2 4 **5 8** ]
39+
Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
40+
41+
##### Third Pass
42+
[ **1 2** 4 5 8 ] -> [ **1 2** 4 5 8 ]
43+
44+
[ 1 **2 4** 5 8 ] -> [ 1 **2 4** 5 8 ]
45+
46+
[ 1 2 **4 5** 8 ] -> [ 1 2 **4 5** 8 ]
47+
48+
[ 1 2 4 **5 8** ] -> [ 1 2 4 **5 8** ]
49+
50+
This is the same for the forth and fifth passes.
51+
52+
#### Code
53+
```swift
54+
for i in 0..<array.count {
55+
for j in 1..<array.count {
56+
if array[j] < array[i] {
57+
let tmp = array[i]
58+
array[i] = array[j]
59+
array[j] = tmp
60+
}
61+
}
62+
}
63+
return array
64+
```
65+
66+
#### Optimization
67+
The bubble sort algorithm can be easily optimized by observing that the `n-th` pass finds the `n-th` largest element and puts it into its final place. So, the inner loop can avoid looking at the last `n-1` items when running for the `n-th` time:
68+
69+
```swift
70+
for i in 0..<array.count {
71+
for j in 1..<array.count - i {
72+
if array[j] < array[i] {
73+
let tmp = array[i]
74+
array[i] = array[j]
75+
array[j] = tmp
76+
}
77+
}
78+
}
79+
return array
80+
```
81+
82+
The only change made was on the second line, changing the interval from `1..<array.count` to `1..<array.count - i`, effectively cutting the number of comparisons by half.
83+
84+
That said, this is still a terribly inefficient sorting algorithm.

0 commit comments

Comments
 (0)