Bubble Sort
Bubble Sort
1
2
3
4
Sorting ‘Algorithms’ {
5
6
7
8
9
< We don’t have better algorithms, we just
10 have more data>
11
12
13 }
14
Sorting Algorithms
Data Structures workshop.css
01 {
1
2
3
4
5
6 [Bubble Sort]
7
8 < A simple sorting algorithm used to rearrange
9 a set of elements in ascending or descending
10 order. >
11
}
12
13
14
Bubble Sort
Data Structures Algorithms
1
2
Introduction; {
‘Bubble sort is a basic algorithm for arranging a string of
3 numbers or other elements in the correct order.’
4
<p The method works by examining each set of adjacent
5 elements in the string, from left to right, switching
6 their positions if they are out of order.
7 “The algorithm then repeats this process until it can
run through the entire string and find no two elements
8
that need to be swapped.”
9
10
Useful for smaller sets, inefficient for larger
11 sets.
12
13 </p>
14 }
Programming Language
Data Structures Algorithms
1
2
Concept < /1 > { 5 3
10
11 < If Bubble Element is in order with next element,
12 there’ll be no swapping & bubble will be the next
element. >
13
14 }
Bubble Sort
1
2
Pseudo Code
3 1. BubbleSort (Array[], size)
4
2. for i = 0 to i < size-1
5
3. for j = 0 to j < size-i-1
6
7 4. if(Array[j] > Array[j+1])
8 5. swap(Array[j], Array[j+1])
9
10
11
12
13
14
Data Structures Algorithms
1 Recursive Implementation
2
3
4
5
6
7
8
9
10
11
12
13
14
Data Structures Algorithms
1 Iterative Implementation
2
3
4
5
6
7
8
9
10
11
12
13
14
Data Structures Algorithms
1
2
3
4
5
6
7
8
9
10
11 The Worst-Case condition for
12 bubble sort occurs when elements
of the array are arranged in
13 decreasing order.
14
Bubble Sort