0% found this document useful (0 votes)
26 views13 pages

Bubble Sort

Uploaded by

Fatima Humayun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views13 pages

Bubble Sort

Uploaded by

Fatima Humayun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Data Structures Algorithms

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

3 < If Bubble Element is out of order with next


4 element, swap the bubble with next element. >
5
6
7
}
8
9 Concept < /2 > { 1 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 Ascending Order Sorting


2
3 1st Cycle
4 4 Comparisons
5
6
7 2nd Cycle
8
3 Comparisons
9
10 3rd Cycle
11 2 Comparisons
12
13 4th Cycle
14 1 Comparison
Data Structures Algorithms

1 Ascending Order Sorting


2
3 1st Cycle
4 4 Comparisons
5
6
7 2nd Cycle
8
3 Comparisons
9
10 3rd Cycle
11 2 Comparisons
12
13 4th Cycle
14 1 Comparison
Data Structures Algorithms

1 Recursive Implementation
2
3
4
5
6
7
8
9
10
11
12
13
14
Data Structures Algorithms

1 What about this?


2
3
4
5 3 5 4 7 11 12 15 21
6
7
8
9
10
11
12
13
14
Data Structures Algorithms

1 Optimized Recursive Implementation


2
3
4
5
6
7
8
9
10
11
12
13
14
Data Structures Algorithms

1 Optimized 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

You might also like