SlideShare a Scribd company logo
1 of 98
Programming Language
INSTRUCTOR: Dr. Benish Amin
Sorting
 Sorting takes an unordered collection and makes it an ordered
one.
2
5
12
35
42
77 101
1 2 3 4 5 6
5 12 35 42 77 101
1 2 3 4 5 6
What is Bubble Sort
 Idea:
• Repeatedly pass through the array
• Swaps adjacent elements that are out of order
 Easier to implement, but slower…
1
3
2
9
6
4
8
j
n
1 2 3
"Bubbling Up" the Largest Element
 Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and
swapping
4
5
12
35
42
77 101
1 2 3 4 5 6
"Bubbling Up" the Largest Element
 Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and
swapping
5
5
12
35
42
77 101
1 2 3 4 5 6
Swap
42 77
"Bubbling Up" the Largest Element
 Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and
swapping
6
5
12
35
77
42 101
1 2 3 4 5 6
Swap
35 77
"Bubbling Up" the Largest Element
 Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and swapping
7
5
12
77
35
42 101
1 2 3 4 5 6
Swap
12 77
"Bubbling Up" the Largest Element
 Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and
swapping
8
5
77
12
35
42 101
1 2 3 4 5 6
No need to swap
"Bubbling Up" the Largest Element
 Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and swapping
9
5
77
12
35
42 101
1 2 3 4 5 6
Swap
5 101
"Bubbling Up" the Largest Element
 Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and swapping
10
77
12
35
42 5
1 2 3 4 5 6
101
Largest value correctly placed
Items of Interest
 Notice that only the largest value is correctly placed
 All other values are still out of order
 So, we need to repeat this process
11
77
12
35
42 5
1 2 3 4 5 6
101
Largest value correctly placed
Repeat “Bubble Up” How Many Times?
 If we have N elements…
 And if each time we bubble an element, we place it in its correct
location…
 Then we repeat the “bubble up” process N – 1 times.
 This guarantees we’ll correctly place all N elements.
12
“Bubbling” All the Elements
13
77
12
35
42 5
1 2 3 4 5 6
101
5
42
12
35 77 101
42
5
35
12 77 101
42
35
5
12 77 101
42
35
12
5 77 101
N
-
1
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
Reducing the Number of Comparisons
14
12
35
42
77 101
1 2 3 4 5 6
5
77
12
35
42 5
1 2 3 4 5 6
101
5
42
12
35 77
1 2 3 4 5 6
101
42
5
35
12 77
1 2 3 4 5 6
101
42
35
5
12 77
1 2 3 4 5 6
101
Reducing the Number of Comparisons
 On the Nth “bubble up”, we only need to do MAX - N comparisons.
 For example:
• This is the 4th “bubble up”
• MAX is 6
• Thus, we have 2 comparisons to do
15
42
5
35
12 77
1 2 3 4 5 6
101
16
#include <iostream>
using namespace std;
// perform bubble sort
void bubbleSort(int array[], int size) {
// loop to access each array element
for (int step = 0; step < size; ++step) {
// loop to compare array elements
for (int i = 0; i < size - step; ++i) {
// compare two adjacent elements
// change > to < to sort in descending order
if (array[i] > array[i + 1]) {
// swapping elements if elements
// are not in the intended order
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
}
17
// print array
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
cout << " " << array[i];
}
cout << "n";
}
int main() {
int data[] = {-2, 45, 0, 11, -9};
// find array's length
int size = sizeof(data) / sizeof(data[0]);
bubbleSort(data, size);
cout << "Sorted Array in Ascending Order:n";
printArray(data, size);
return 0;
}
Already Sorted Array?
 What if the array was already sorted?
 What if only a few elements were out of place and after a couple
of “bubble ups,” the collection was sorted?
 We want to be able to detect this and “stop early”!
18
42
35
12
5 77
1 2 3 4 5 6
101
An Animated Example
19
67
45
23 14 6 33
98 42
1 2 3 4 5 6 7 8
to_do
index
7
N 8 did_swap true
An Animated Example
20
67
45
23 14 6 33
98 42
to_do
index
7
1
N 8 did_swap false
1 2 3 4 5 6 7 8
An Animated Example
21
67
45
23 14 6 33
98 42
to_do
index
7
1
N 8
Swap
did_swap false
1 2 3 4 5 6 7 8
An Animated Example
22
67
45
98 14 6 33
23 42
to_do
index
7
1
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
23
67
45
98 14 6 33
23 42
to_do
index
7
2
N 8 did_swap true
1 2 3 4 5 6 7 8
An Animated Example
24
67
45
98 14 6 33
23 42
to_do
index
7
2
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
25
67
98
45 14 6 33
23 42
to_do
index
7
2
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
26
67
98
45 14 6 33
23 42
to_do
index
7
3
N 8 did_swap true
1 2 3 4 5 6 7 8
An Animated Example
27
67
98
45 14 6 33
23 42
to_do
index
7
3
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
28
67
14
45 98 6 33
23 42
to_do
index
7
3
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
29
67
14
45 98 6 33
23 42
to_do
index
7
4
N 8 did_swap true
1 2 3 4 5 6 7 8
An Animated Example
30
67
14
45 98 6 33
23 42
to_do
index
7
4
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
31
67
14
45 6 98 33
23 42
to_do
index
7
4
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
32
67
14
45 6 98 33
23 42
to_do
index
7
5
N 8 did_swap true
1 2 3 4 5 6 7 8
An Animated Example
33
67
14
45 6 98 33
23 42
to_do
index
7
5
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
34
98
14
45 6 67 33
23 42
to_do
index
7
5
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
35
98
14
45 6 67 33
23 42
to_do
index
7
6
N 8 did_swap true
1 2 3 4 5 6 7 8
An Animated Example
36
98
14
45 6 67 33
23 42
to_do
index
7
6
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
37
33
14
45 6 67 98
23 42
to_do
index
7
6
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
38
33
14
45 6 67 98
23 42
to_do
index
7
7
N 8 did_swap true
1 2 3 4 5 6 7 8
An Animated Example
39
33
14
45 6 67 98
23 42
to_do
index
7
7
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
40
33
14
45 6 67 42
23 98
to_do
index
7
7
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
After First Pass of Outer Loop
41
33
14
45 6 67 42
23 98
to_do
index
7
8
N 8
Finished first “Bubble Up”
did_swap true
1 2 3 4 5 6 7 8
The Second “Bubble Up”
42
33
14
45 6 67 42
23 98
to_do
index
6
1
N 8 did_swap false
1 2 3 4 5 6 7 8
The Second “Bubble Up”
43
33
14
45 6 67 42
23 98
to_do
index
6
1
N 8 did_swap false
No Swap
1 2 3 4 5 6 7 8
The Second “Bubble Up”
44
33
14
45 6 67 42
23 98
to_do
index
6
2
N 8 did_swap false
1 2 3 4 5 6 7 8
The Second “Bubble Up”
45
33
14
45 6 67 42
23 98
to_do
index
6
2
N 8 did_swap false
Swap
1 2 3 4 5 6 7 8
The Second “Bubble Up”
46
33
45
14 6 67 42
23 98
to_do
index
6
2
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Second “Bubble Up”
47
33
45
14 6 67 42
23 98
to_do
index
6
3
N 8 did_swap true
1 2 3 4 5 6 7 8
The Second “Bubble Up”
48
33
45
14 6 67 42
23 98
to_do
index
6
3
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Second “Bubble Up”
49
33
6
14 45 67 42
23 98
to_do
index
6
3
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Second “Bubble Up”
50
33
6
14 45 67 42
23 98
to_do
index
6
4
N 8 did_swap true
1 2 3 4 5 6 7 8
The Second “Bubble Up”
51
33
6
14 45 67 42
23 98
to_do
index
6
4
N 8 did_swap true
No Swap
1 2 3 4 5 6 7 8
The Second “Bubble Up”
52
33
6
14 45 67 42
23 98
to_do
index
6
5
N 8 did_swap true
1 2 3 4 5 6 7 8
The Second “Bubble Up”
53
33
6
14 45 67 42
23 98
to_do
index
6
5
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Second “Bubble Up”
54
67
6
14 45 33 42
23 98
to_do
index
6
5
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Second “Bubble Up”
55
67
6
14 45 33 42
23 98
to_do
index
6
6
N 8 did_swap true
1 2 3 4 5 6 7 8
The Second “Bubble Up”
56
67
6
14 45 33 42
23 98
to_do
index
6
6
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Second “Bubble Up”
57
42
6
14 45 33 67
23 98
to_do
index
6
6
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
After Second Pass of Outer Loop
58
42
6
14 45 33 67
23 98
to_do
index
6
7
N 8 did_swap true
Finished second “Bubble Up”
1 2 3 4 5 6 7 8
The Third “Bubble Up”
59
42
6
14 45 33 67
23 98
to_do
index
5
1
N 8 did_swap false
1 2 3 4 5 6 7 8
The Third “Bubble Up”
60
42
6
14 45 33 67
23 98
to_do
index
5
1
N 8 did_swap false
Swap
1 2 3 4 5 6 7 8
The Third “Bubble Up”
61
42
6
23 45 33 67
14 98
to_do
index
5
1
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Third “Bubble Up”
62
42
6
23 45 33 67
14 98
to_do
index
5
2
N 8 did_swap true
1 2 3 4 5 6 7 8
The Third “Bubble Up”
63
42
6
23 45 33 67
14 98
to_do
index
5
2
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Third “Bubble Up”
64
42
23
6 45 33 67
14 98
to_do
index
5
2
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Third “Bubble Up”
65
42
23
6 45 33 67
14 98
to_do
index
5
3
N 8 did_swap true
1 2 3 4 5 6 7 8
The Third “Bubble Up”
66
42
23
6 45 33 67
14 98
to_do
index
5
3
N 8 did_swap true
No Swap
1 2 3 4 5 6 7 8
The Third “Bubble Up”
67
42
23
6 45 33 67
14 98
to_do
index
5
4
N 8 did_swap true
1 2 3 4 5 6 7 8
The Third “Bubble Up”
68
42
23
6 45 33 67
14 98
to_do
index
5
4
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Third “Bubble Up”
69
42
23
6 33 45 67
14 98
to_do
index
5
4
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Third “Bubble Up”
70
42
23
6 33 45 67
14 98
to_do
index
5
5
N 8 did_swap true
1 2 3 4 5 6 7 8
The Third “Bubble Up”
71
42
23
6 33 45 67
14 98
to_do
index
5
5
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Third “Bubble Up”
72
45
23
6 33 42 67
14 98
to_do
index
5
5
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Third “Bubble Up”
73
45
23
6 33 42 67
14 98
to_do
index
5
6
N 8 did_swap true
Finished third “Bubble Up”
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
74
45
23
6 33 42 67
14 98
to_do
index
4
1
N 8 did_swap false
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
75
45
23
6 33 42 67
14 98
to_do
index
4
1
N 8 did_swap false
Swap
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
76
45
23
14 33 42 67
6 98
to_do
index
4
1
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
77
45
23
14 33 42 67
6 98
to_do
index
4
2
N 8 did_swap true
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
78
45
23
14 33 42 67
6 98
to_do
index
4
2
N 8 did_swap true
No Swap
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
79
45
23
14 33 42 67
6 98
to_do
index
4
3
N 8 did_swap true
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
80
45
23
14 33 42 67
6 98
to_do
index
4
3
N 8 did_swap true
No Swap
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
81
45
23
14 33 42 67
6 98
to_do
index
4
4
N 8 did_swap true
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
82
45
23
14 33 42 67
6 98
to_do
index
4
4
N 8 did_swap true
No Swap
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
83
45
23
14 33 42 67
6 98
to_do
index
4
5
N 8 did_swap true
Finished fourth “Bubble Up”
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
84
45
23
14 33 42 67
6 98
to_do
index
3
1
N 8 did_swap false
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
85
45
23
14 33 42 67
6 98
to_do
index
3
1
N 8 did_swap false
No Swap
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
86
45
23
14 33 42 67
6 98
to_do
index
3
2
N 8 did_swap false
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
87
45
23
14 33 42 67
6 98
to_do
index
3
2
N 8 did_swap false
No Swap
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
88
45
23
14 33 42 67
6 98
to_do
index
3
3
N 8 did_swap false
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
89
45
23
14 33 42 67
6 98
to_do
index
3
3
N 8 did_swap false
No Swap
1 2 3 4 5 6 7 8
After Fifth Pass of Outer Loop
90
45
23
14 33 42 67
6 98
to_do
index
3
4
N 8 did_swap false
Finished fifth “Bubble Up”
1 2 3 4 5 6 7 8
Finished “Early”
91
45
23
14 33 42 67
6 98
to_do
index
3
4
N 8 did_swap false
We didn’t do any swapping, so all of the other
elements must be correctly placed.
We can “skip” the last two passes of the outer loop.
1 2 3 4 5 6 7 8
Using a Boolean “Flag”
 We can use a boolean variable to determine if any swapping occurred
during the “bubble up.”
 If no swapping occurred, then we know that the array is already
sorted!
 This boolean “flag” needs to be reset after each “bubble up.”
92
93
#include <iostream>
using namespace std;
// perform bubble sort
void bubbleSort(int array[], int size) {
// check if swapping occurs
int flag = 0;
// loop to access each array element
for (int step = 0; step < size; ++step) {
// loop to compare array elements
for (int i = 0; i < size - step; ++i) {
// compare two adjacent elements
// change > to < to sort in descending order
if (array[i] > array[i + 1]) {
// swapping elements if elements
// are not in the intended order
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
flag = 1;
}
}
// no swapping means the array is already sorted
// so no need of further comparison
if (flag == 0)
break;
}
}
94
// print array
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
cout << " " << array[i];
}
cout << "n";
}
int main() {
int data[] = {-2, 45, 0, 11, -9};
// find array's length
int size = sizeof(data) / sizeof(data[0]);
bubbleSort(data, size);
cout << "Sorted Array in Ascending Order:n";
printArray(data, size);
return 0;
}
Selection Sort
 Idea:
• Find the smallest element in the array
• Exchange it with the element in the first position
• Find the second smallest element and exchange it with the element in the
second position
• Continue until the array is sorted
 Disadvantage:
• Running time depends only slightly on the amount of order in the file
95
Example
96
1
3
2
9
6
4
8
8
3
2
9
6
4
1
8
3
4
9
6
2
1
8
6
4
9
3
2
1
8
9
6
4
3
2
1
8
6
9
4
3
2
1
9
8
6
4
3
2
1
9
8
6
4
3
2
1
Selection Sort
Alg.: SELECTION-SORT(A)
n ← length[A]
for j ← 1 to n - 1
do smallest ← j
for i ← j + 1 to n
do if A[i] < A[smallest]
then smallest ← i
exchange A[j] ↔ A[smallest]
97
1
3
2
9
6
4
8
Assignment # 1
• Perform selection sort on user defined data and implement the code
in C++.
• Deadline: 4th March, 2024
98

More Related Content

Similar to Sorting of arrays, types in c++ .IST .ppt

Sorting techniques Anil Dutt
Sorting techniques Anil DuttSorting techniques Anil Dutt
Sorting techniques Anil DuttAnil Dutt
 
Sorting bubble-sort anim
Sorting   bubble-sort animSorting   bubble-sort anim
Sorting bubble-sort animFajar Zain
 
Bubble sort
Bubble sortBubble sort
Bubble sortManek Ar
 
Sorting and Hashing Algorithm full pdfs.
Sorting and Hashing Algorithm full pdfs.Sorting and Hashing Algorithm full pdfs.
Sorting and Hashing Algorithm full pdfs.NikhilSoni177492
 
Heapsortokkay
HeapsortokkayHeapsortokkay
HeapsortokkayRemesha
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Muhammad Hammad Waseem
 
8 elementary sorts-bubble
8 elementary sorts-bubble8 elementary sorts-bubble
8 elementary sorts-bubbleirdginfo
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithmsZaid Hameed
 
Complete binary tree and heap
Complete binary tree and heapComplete binary tree and heap
Complete binary tree and heapNazir Ahmed
 
Overview of sparse and low-rank matrix / tensor techniques
Overview of sparse and low-rank matrix / tensor techniques Overview of sparse and low-rank matrix / tensor techniques
Overview of sparse and low-rank matrix / tensor techniques Alexander Litvinenko
 
Aizaz comb sort
Aizaz comb sortAizaz comb sort
Aizaz comb sortNauman Ali
 
Borrowingwed1.10
Borrowingwed1.10Borrowingwed1.10
Borrowingwed1.10PDS
 
7-7 Equivalent Fractions
7-7 Equivalent Fractions7-7 Equivalent Fractions
7-7 Equivalent FractionsRudy Alfonso
 
C73ad8809250590f02a19367374e4d51
C73ad8809250590f02a19367374e4d51C73ad8809250590f02a19367374e4d51
C73ad8809250590f02a19367374e4d51guest3cb79f
 

Similar to Sorting of arrays, types in c++ .IST .ppt (20)

Sorting techniques Anil Dutt
Sorting techniques Anil DuttSorting techniques Anil Dutt
Sorting techniques Anil Dutt
 
Sorting bubble-sort anim
Sorting   bubble-sort animSorting   bubble-sort anim
Sorting bubble-sort anim
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
BUBBLESORT
BUBBLESORT BUBBLESORT
BUBBLESORT
 
Sorting and Hashing Algorithm full pdfs.
Sorting and Hashing Algorithm full pdfs.Sorting and Hashing Algorithm full pdfs.
Sorting and Hashing Algorithm full pdfs.
 
Heapsortokkay
HeapsortokkayHeapsortokkay
Heapsortokkay
 
[ACM-ICPC] Sort
[ACM-ICPC] Sort[ACM-ICPC] Sort
[ACM-ICPC] Sort
 
LEC 8-DS ALGO(heaps).pdf
LEC 8-DS  ALGO(heaps).pdfLEC 8-DS  ALGO(heaps).pdf
LEC 8-DS ALGO(heaps).pdf
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
8 elementary sorts-bubble
8 elementary sorts-bubble8 elementary sorts-bubble
8 elementary sorts-bubble
 
Sorting
SortingSorting
Sorting
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Complete binary tree and heap
Complete binary tree and heapComplete binary tree and heap
Complete binary tree and heap
 
Overview of sparse and low-rank matrix / tensor techniques
Overview of sparse and low-rank matrix / tensor techniques Overview of sparse and low-rank matrix / tensor techniques
Overview of sparse and low-rank matrix / tensor techniques
 
Aizaz comb sort
Aizaz comb sortAizaz comb sort
Aizaz comb sort
 
Borrowingwed1.10
Borrowingwed1.10Borrowingwed1.10
Borrowingwed1.10
 
7-7 Equivalent Fractions
7-7 Equivalent Fractions7-7 Equivalent Fractions
7-7 Equivalent Fractions
 
7-7 Equivalent Fractions
7-7 Equivalent Fractions7-7 Equivalent Fractions
7-7 Equivalent Fractions
 
Math 5
Math 5 Math 5
Math 5
 
C73ad8809250590f02a19367374e4d51
C73ad8809250590f02a19367374e4d51C73ad8809250590f02a19367374e4d51
C73ad8809250590f02a19367374e4d51
 

More from saadpisjes

Lab 6- Constructors in c++ . IST .pptx
Lab 6- Constructors in c++ .  IST  .pptxLab 6- Constructors in c++ .  IST  .pptx
Lab 6- Constructors in c++ . IST .pptxsaadpisjes
 
Safety_Health_and_Environment .IST .pptx
Safety_Health_and_Environment .IST .pptxSafety_Health_and_Environment .IST .pptx
Safety_Health_and_Environment .IST .pptxsaadpisjes
 
Safety_Health_and_Environment . ist .pptx
Safety_Health_and_Environment . ist .pptxSafety_Health_and_Environment . ist .pptx
Safety_Health_and_Environment . ist .pptxsaadpisjes
 
Concept of Classes in c++.ist mse16.pptx
Concept of Classes in c++.ist mse16.pptxConcept of Classes in c++.ist mse16.pptx
Concept of Classes in c++.ist mse16.pptxsaadpisjes
 
Administrative Structure of Pakistan IST.pptx
Administrative Structure of Pakistan  IST.pptxAdministrative Structure of Pakistan  IST.pptx
Administrative Structure of Pakistan IST.pptxsaadpisjes
 
Administrative Structure of Pakistan IST.pptx
Administrative Structure of Pakistan  IST.pptxAdministrative Structure of Pakistan  IST.pptx
Administrative Structure of Pakistan IST.pptxsaadpisjes
 
Geo-Strategic Significance of Pakistan.pptx IST.pptx
Geo-Strategic Significance of Pakistan.pptx IST.pptxGeo-Strategic Significance of Pakistan.pptx IST.pptx
Geo-Strategic Significance of Pakistan.pptx IST.pptxsaadpisjes
 

More from saadpisjes (7)

Lab 6- Constructors in c++ . IST .pptx
Lab 6- Constructors in c++ .  IST  .pptxLab 6- Constructors in c++ .  IST  .pptx
Lab 6- Constructors in c++ . IST .pptx
 
Safety_Health_and_Environment .IST .pptx
Safety_Health_and_Environment .IST .pptxSafety_Health_and_Environment .IST .pptx
Safety_Health_and_Environment .IST .pptx
 
Safety_Health_and_Environment . ist .pptx
Safety_Health_and_Environment . ist .pptxSafety_Health_and_Environment . ist .pptx
Safety_Health_and_Environment . ist .pptx
 
Concept of Classes in c++.ist mse16.pptx
Concept of Classes in c++.ist mse16.pptxConcept of Classes in c++.ist mse16.pptx
Concept of Classes in c++.ist mse16.pptx
 
Administrative Structure of Pakistan IST.pptx
Administrative Structure of Pakistan  IST.pptxAdministrative Structure of Pakistan  IST.pptx
Administrative Structure of Pakistan IST.pptx
 
Administrative Structure of Pakistan IST.pptx
Administrative Structure of Pakistan  IST.pptxAdministrative Structure of Pakistan  IST.pptx
Administrative Structure of Pakistan IST.pptx
 
Geo-Strategic Significance of Pakistan.pptx IST.pptx
Geo-Strategic Significance of Pakistan.pptx IST.pptxGeo-Strategic Significance of Pakistan.pptx IST.pptx
Geo-Strategic Significance of Pakistan.pptx IST.pptx
 

Recently uploaded

Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxLimon Prince
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...Nguyen Thanh Tu Collection
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽中 央社
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesAmanpreetKaur157993
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppCeline George
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxCeline George
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptxPoojaSen20
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFVivekanand Anglo Vedic Academy
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...Gary Wood
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...Nguyen Thanh Tu Collection
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxMohamed Rizk Khodair
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi RajagopalEADTU
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 

Recently uploaded (20)

Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 

Sorting of arrays, types in c++ .IST .ppt

  • 2. Sorting  Sorting takes an unordered collection and makes it an ordered one. 2 5 12 35 42 77 101 1 2 3 4 5 6 5 12 35 42 77 101 1 2 3 4 5 6
  • 3. What is Bubble Sort  Idea: • Repeatedly pass through the array • Swaps adjacent elements that are out of order  Easier to implement, but slower… 1 3 2 9 6 4 8 j n 1 2 3
  • 4. "Bubbling Up" the Largest Element  Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 4 5 12 35 42 77 101 1 2 3 4 5 6
  • 5. "Bubbling Up" the Largest Element  Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 5 5 12 35 42 77 101 1 2 3 4 5 6 Swap 42 77
  • 6. "Bubbling Up" the Largest Element  Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 6 5 12 35 77 42 101 1 2 3 4 5 6 Swap 35 77
  • 7. "Bubbling Up" the Largest Element  Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 7 5 12 77 35 42 101 1 2 3 4 5 6 Swap 12 77
  • 8. "Bubbling Up" the Largest Element  Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 8 5 77 12 35 42 101 1 2 3 4 5 6 No need to swap
  • 9. "Bubbling Up" the Largest Element  Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 9 5 77 12 35 42 101 1 2 3 4 5 6 Swap 5 101
  • 10. "Bubbling Up" the Largest Element  Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 10 77 12 35 42 5 1 2 3 4 5 6 101 Largest value correctly placed
  • 11. Items of Interest  Notice that only the largest value is correctly placed  All other values are still out of order  So, we need to repeat this process 11 77 12 35 42 5 1 2 3 4 5 6 101 Largest value correctly placed
  • 12. Repeat “Bubble Up” How Many Times?  If we have N elements…  And if each time we bubble an element, we place it in its correct location…  Then we repeat the “bubble up” process N – 1 times.  This guarantees we’ll correctly place all N elements. 12
  • 13. “Bubbling” All the Elements 13 77 12 35 42 5 1 2 3 4 5 6 101 5 42 12 35 77 101 42 5 35 12 77 101 42 35 5 12 77 101 42 35 12 5 77 101 N - 1 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6
  • 14. Reducing the Number of Comparisons 14 12 35 42 77 101 1 2 3 4 5 6 5 77 12 35 42 5 1 2 3 4 5 6 101 5 42 12 35 77 1 2 3 4 5 6 101 42 5 35 12 77 1 2 3 4 5 6 101 42 35 5 12 77 1 2 3 4 5 6 101
  • 15. Reducing the Number of Comparisons  On the Nth “bubble up”, we only need to do MAX - N comparisons.  For example: • This is the 4th “bubble up” • MAX is 6 • Thus, we have 2 comparisons to do 15 42 5 35 12 77 1 2 3 4 5 6 101
  • 16. 16 #include <iostream> using namespace std; // perform bubble sort void bubbleSort(int array[], int size) { // loop to access each array element for (int step = 0; step < size; ++step) { // loop to compare array elements for (int i = 0; i < size - step; ++i) { // compare two adjacent elements // change > to < to sort in descending order if (array[i] > array[i + 1]) { // swapping elements if elements // are not in the intended order int temp = array[i]; array[i] = array[i + 1]; array[i + 1] = temp; } } } }
  • 17. 17 // print array void printArray(int array[], int size) { for (int i = 0; i < size; ++i) { cout << " " << array[i]; } cout << "n"; } int main() { int data[] = {-2, 45, 0, 11, -9}; // find array's length int size = sizeof(data) / sizeof(data[0]); bubbleSort(data, size); cout << "Sorted Array in Ascending Order:n"; printArray(data, size); return 0; }
  • 18. Already Sorted Array?  What if the array was already sorted?  What if only a few elements were out of place and after a couple of “bubble ups,” the collection was sorted?  We want to be able to detect this and “stop early”! 18 42 35 12 5 77 1 2 3 4 5 6 101
  • 19. An Animated Example 19 67 45 23 14 6 33 98 42 1 2 3 4 5 6 7 8 to_do index 7 N 8 did_swap true
  • 20. An Animated Example 20 67 45 23 14 6 33 98 42 to_do index 7 1 N 8 did_swap false 1 2 3 4 5 6 7 8
  • 21. An Animated Example 21 67 45 23 14 6 33 98 42 to_do index 7 1 N 8 Swap did_swap false 1 2 3 4 5 6 7 8
  • 22. An Animated Example 22 67 45 98 14 6 33 23 42 to_do index 7 1 N 8 Swap did_swap true 1 2 3 4 5 6 7 8
  • 23. An Animated Example 23 67 45 98 14 6 33 23 42 to_do index 7 2 N 8 did_swap true 1 2 3 4 5 6 7 8
  • 24. An Animated Example 24 67 45 98 14 6 33 23 42 to_do index 7 2 N 8 Swap did_swap true 1 2 3 4 5 6 7 8
  • 25. An Animated Example 25 67 98 45 14 6 33 23 42 to_do index 7 2 N 8 Swap did_swap true 1 2 3 4 5 6 7 8
  • 26. An Animated Example 26 67 98 45 14 6 33 23 42 to_do index 7 3 N 8 did_swap true 1 2 3 4 5 6 7 8
  • 27. An Animated Example 27 67 98 45 14 6 33 23 42 to_do index 7 3 N 8 Swap did_swap true 1 2 3 4 5 6 7 8
  • 28. An Animated Example 28 67 14 45 98 6 33 23 42 to_do index 7 3 N 8 Swap did_swap true 1 2 3 4 5 6 7 8
  • 29. An Animated Example 29 67 14 45 98 6 33 23 42 to_do index 7 4 N 8 did_swap true 1 2 3 4 5 6 7 8
  • 30. An Animated Example 30 67 14 45 98 6 33 23 42 to_do index 7 4 N 8 Swap did_swap true 1 2 3 4 5 6 7 8
  • 31. An Animated Example 31 67 14 45 6 98 33 23 42 to_do index 7 4 N 8 Swap did_swap true 1 2 3 4 5 6 7 8
  • 32. An Animated Example 32 67 14 45 6 98 33 23 42 to_do index 7 5 N 8 did_swap true 1 2 3 4 5 6 7 8
  • 33. An Animated Example 33 67 14 45 6 98 33 23 42 to_do index 7 5 N 8 Swap did_swap true 1 2 3 4 5 6 7 8
  • 34. An Animated Example 34 98 14 45 6 67 33 23 42 to_do index 7 5 N 8 Swap did_swap true 1 2 3 4 5 6 7 8
  • 35. An Animated Example 35 98 14 45 6 67 33 23 42 to_do index 7 6 N 8 did_swap true 1 2 3 4 5 6 7 8
  • 36. An Animated Example 36 98 14 45 6 67 33 23 42 to_do index 7 6 N 8 Swap did_swap true 1 2 3 4 5 6 7 8
  • 37. An Animated Example 37 33 14 45 6 67 98 23 42 to_do index 7 6 N 8 Swap did_swap true 1 2 3 4 5 6 7 8
  • 38. An Animated Example 38 33 14 45 6 67 98 23 42 to_do index 7 7 N 8 did_swap true 1 2 3 4 5 6 7 8
  • 39. An Animated Example 39 33 14 45 6 67 98 23 42 to_do index 7 7 N 8 Swap did_swap true 1 2 3 4 5 6 7 8
  • 40. An Animated Example 40 33 14 45 6 67 42 23 98 to_do index 7 7 N 8 Swap did_swap true 1 2 3 4 5 6 7 8
  • 41. After First Pass of Outer Loop 41 33 14 45 6 67 42 23 98 to_do index 7 8 N 8 Finished first “Bubble Up” did_swap true 1 2 3 4 5 6 7 8
  • 42. The Second “Bubble Up” 42 33 14 45 6 67 42 23 98 to_do index 6 1 N 8 did_swap false 1 2 3 4 5 6 7 8
  • 43. The Second “Bubble Up” 43 33 14 45 6 67 42 23 98 to_do index 6 1 N 8 did_swap false No Swap 1 2 3 4 5 6 7 8
  • 44. The Second “Bubble Up” 44 33 14 45 6 67 42 23 98 to_do index 6 2 N 8 did_swap false 1 2 3 4 5 6 7 8
  • 45. The Second “Bubble Up” 45 33 14 45 6 67 42 23 98 to_do index 6 2 N 8 did_swap false Swap 1 2 3 4 5 6 7 8
  • 46. The Second “Bubble Up” 46 33 45 14 6 67 42 23 98 to_do index 6 2 N 8 did_swap true Swap 1 2 3 4 5 6 7 8
  • 47. The Second “Bubble Up” 47 33 45 14 6 67 42 23 98 to_do index 6 3 N 8 did_swap true 1 2 3 4 5 6 7 8
  • 48. The Second “Bubble Up” 48 33 45 14 6 67 42 23 98 to_do index 6 3 N 8 did_swap true Swap 1 2 3 4 5 6 7 8
  • 49. The Second “Bubble Up” 49 33 6 14 45 67 42 23 98 to_do index 6 3 N 8 did_swap true Swap 1 2 3 4 5 6 7 8
  • 50. The Second “Bubble Up” 50 33 6 14 45 67 42 23 98 to_do index 6 4 N 8 did_swap true 1 2 3 4 5 6 7 8
  • 51. The Second “Bubble Up” 51 33 6 14 45 67 42 23 98 to_do index 6 4 N 8 did_swap true No Swap 1 2 3 4 5 6 7 8
  • 52. The Second “Bubble Up” 52 33 6 14 45 67 42 23 98 to_do index 6 5 N 8 did_swap true 1 2 3 4 5 6 7 8
  • 53. The Second “Bubble Up” 53 33 6 14 45 67 42 23 98 to_do index 6 5 N 8 did_swap true Swap 1 2 3 4 5 6 7 8
  • 54. The Second “Bubble Up” 54 67 6 14 45 33 42 23 98 to_do index 6 5 N 8 did_swap true Swap 1 2 3 4 5 6 7 8
  • 55. The Second “Bubble Up” 55 67 6 14 45 33 42 23 98 to_do index 6 6 N 8 did_swap true 1 2 3 4 5 6 7 8
  • 56. The Second “Bubble Up” 56 67 6 14 45 33 42 23 98 to_do index 6 6 N 8 did_swap true Swap 1 2 3 4 5 6 7 8
  • 57. The Second “Bubble Up” 57 42 6 14 45 33 67 23 98 to_do index 6 6 N 8 did_swap true Swap 1 2 3 4 5 6 7 8
  • 58. After Second Pass of Outer Loop 58 42 6 14 45 33 67 23 98 to_do index 6 7 N 8 did_swap true Finished second “Bubble Up” 1 2 3 4 5 6 7 8
  • 59. The Third “Bubble Up” 59 42 6 14 45 33 67 23 98 to_do index 5 1 N 8 did_swap false 1 2 3 4 5 6 7 8
  • 60. The Third “Bubble Up” 60 42 6 14 45 33 67 23 98 to_do index 5 1 N 8 did_swap false Swap 1 2 3 4 5 6 7 8
  • 61. The Third “Bubble Up” 61 42 6 23 45 33 67 14 98 to_do index 5 1 N 8 did_swap true Swap 1 2 3 4 5 6 7 8
  • 62. The Third “Bubble Up” 62 42 6 23 45 33 67 14 98 to_do index 5 2 N 8 did_swap true 1 2 3 4 5 6 7 8
  • 63. The Third “Bubble Up” 63 42 6 23 45 33 67 14 98 to_do index 5 2 N 8 did_swap true Swap 1 2 3 4 5 6 7 8
  • 64. The Third “Bubble Up” 64 42 23 6 45 33 67 14 98 to_do index 5 2 N 8 did_swap true Swap 1 2 3 4 5 6 7 8
  • 65. The Third “Bubble Up” 65 42 23 6 45 33 67 14 98 to_do index 5 3 N 8 did_swap true 1 2 3 4 5 6 7 8
  • 66. The Third “Bubble Up” 66 42 23 6 45 33 67 14 98 to_do index 5 3 N 8 did_swap true No Swap 1 2 3 4 5 6 7 8
  • 67. The Third “Bubble Up” 67 42 23 6 45 33 67 14 98 to_do index 5 4 N 8 did_swap true 1 2 3 4 5 6 7 8
  • 68. The Third “Bubble Up” 68 42 23 6 45 33 67 14 98 to_do index 5 4 N 8 did_swap true Swap 1 2 3 4 5 6 7 8
  • 69. The Third “Bubble Up” 69 42 23 6 33 45 67 14 98 to_do index 5 4 N 8 did_swap true Swap 1 2 3 4 5 6 7 8
  • 70. The Third “Bubble Up” 70 42 23 6 33 45 67 14 98 to_do index 5 5 N 8 did_swap true 1 2 3 4 5 6 7 8
  • 71. The Third “Bubble Up” 71 42 23 6 33 45 67 14 98 to_do index 5 5 N 8 did_swap true Swap 1 2 3 4 5 6 7 8
  • 72. The Third “Bubble Up” 72 45 23 6 33 42 67 14 98 to_do index 5 5 N 8 did_swap true Swap 1 2 3 4 5 6 7 8
  • 73. The Third “Bubble Up” 73 45 23 6 33 42 67 14 98 to_do index 5 6 N 8 did_swap true Finished third “Bubble Up” 1 2 3 4 5 6 7 8
  • 74. The Fourth “Bubble Up” 74 45 23 6 33 42 67 14 98 to_do index 4 1 N 8 did_swap false 1 2 3 4 5 6 7 8
  • 75. The Fourth “Bubble Up” 75 45 23 6 33 42 67 14 98 to_do index 4 1 N 8 did_swap false Swap 1 2 3 4 5 6 7 8
  • 76. The Fourth “Bubble Up” 76 45 23 14 33 42 67 6 98 to_do index 4 1 N 8 did_swap true Swap 1 2 3 4 5 6 7 8
  • 77. The Fourth “Bubble Up” 77 45 23 14 33 42 67 6 98 to_do index 4 2 N 8 did_swap true 1 2 3 4 5 6 7 8
  • 78. The Fourth “Bubble Up” 78 45 23 14 33 42 67 6 98 to_do index 4 2 N 8 did_swap true No Swap 1 2 3 4 5 6 7 8
  • 79. The Fourth “Bubble Up” 79 45 23 14 33 42 67 6 98 to_do index 4 3 N 8 did_swap true 1 2 3 4 5 6 7 8
  • 80. The Fourth “Bubble Up” 80 45 23 14 33 42 67 6 98 to_do index 4 3 N 8 did_swap true No Swap 1 2 3 4 5 6 7 8
  • 81. The Fourth “Bubble Up” 81 45 23 14 33 42 67 6 98 to_do index 4 4 N 8 did_swap true 1 2 3 4 5 6 7 8
  • 82. The Fourth “Bubble Up” 82 45 23 14 33 42 67 6 98 to_do index 4 4 N 8 did_swap true No Swap 1 2 3 4 5 6 7 8
  • 83. The Fourth “Bubble Up” 83 45 23 14 33 42 67 6 98 to_do index 4 5 N 8 did_swap true Finished fourth “Bubble Up” 1 2 3 4 5 6 7 8
  • 84. The Fourth “Bubble Up” 84 45 23 14 33 42 67 6 98 to_do index 3 1 N 8 did_swap false 1 2 3 4 5 6 7 8
  • 85. The Fifth “Bubble Up” 85 45 23 14 33 42 67 6 98 to_do index 3 1 N 8 did_swap false No Swap 1 2 3 4 5 6 7 8
  • 86. The Fifth “Bubble Up” 86 45 23 14 33 42 67 6 98 to_do index 3 2 N 8 did_swap false 1 2 3 4 5 6 7 8
  • 87. The Fifth “Bubble Up” 87 45 23 14 33 42 67 6 98 to_do index 3 2 N 8 did_swap false No Swap 1 2 3 4 5 6 7 8
  • 88. The Fifth “Bubble Up” 88 45 23 14 33 42 67 6 98 to_do index 3 3 N 8 did_swap false 1 2 3 4 5 6 7 8
  • 89. The Fifth “Bubble Up” 89 45 23 14 33 42 67 6 98 to_do index 3 3 N 8 did_swap false No Swap 1 2 3 4 5 6 7 8
  • 90. After Fifth Pass of Outer Loop 90 45 23 14 33 42 67 6 98 to_do index 3 4 N 8 did_swap false Finished fifth “Bubble Up” 1 2 3 4 5 6 7 8
  • 91. Finished “Early” 91 45 23 14 33 42 67 6 98 to_do index 3 4 N 8 did_swap false We didn’t do any swapping, so all of the other elements must be correctly placed. We can “skip” the last two passes of the outer loop. 1 2 3 4 5 6 7 8
  • 92. Using a Boolean “Flag”  We can use a boolean variable to determine if any swapping occurred during the “bubble up.”  If no swapping occurred, then we know that the array is already sorted!  This boolean “flag” needs to be reset after each “bubble up.” 92
  • 93. 93 #include <iostream> using namespace std; // perform bubble sort void bubbleSort(int array[], int size) { // check if swapping occurs int flag = 0; // loop to access each array element for (int step = 0; step < size; ++step) { // loop to compare array elements for (int i = 0; i < size - step; ++i) { // compare two adjacent elements // change > to < to sort in descending order if (array[i] > array[i + 1]) { // swapping elements if elements // are not in the intended order int temp = array[i]; array[i] = array[i + 1]; array[i + 1] = temp; flag = 1; } } // no swapping means the array is already sorted // so no need of further comparison if (flag == 0) break; } }
  • 94. 94 // print array void printArray(int array[], int size) { for (int i = 0; i < size; ++i) { cout << " " << array[i]; } cout << "n"; } int main() { int data[] = {-2, 45, 0, 11, -9}; // find array's length int size = sizeof(data) / sizeof(data[0]); bubbleSort(data, size); cout << "Sorted Array in Ascending Order:n"; printArray(data, size); return 0; }
  • 95. Selection Sort  Idea: • Find the smallest element in the array • Exchange it with the element in the first position • Find the second smallest element and exchange it with the element in the second position • Continue until the array is sorted  Disadvantage: • Running time depends only slightly on the amount of order in the file 95
  • 97. Selection Sort Alg.: SELECTION-SORT(A) n ← length[A] for j ← 1 to n - 1 do smallest ← j for i ← j + 1 to n do if A[i] < A[smallest] then smallest ← i exchange A[j] ↔ A[smallest] 97 1 3 2 9 6 4 8
  • 98. Assignment # 1 • Perform selection sort on user defined data and implement the code in C++. • Deadline: 4th March, 2024 98