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

saadpisjes 32 views 98 slides Apr 25, 2024
Slide 1
Slide 1 of 98
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93
Slide 94
94
Slide 95
95
Slide 96
96
Slide 97
97
Slide 98
98

About This Presentation

Sorting of arrays


Slide Content

Programming Language
INSTRUCTOR: Dr. Benish Amin

Sorting
Sorting takes an unordered collection and makes it an ordered
one.
2
512354277 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…
1329648
j
n1 2 3

"Bubbling Up" the Largest Element
Traverse a collection of elements
•Move from the front to the end
•“Bubble” the largest valueto the end using pair-wise comparisons and
swapping
4
512354277 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
512354277 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
512357742 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
512773542 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
577123542 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
577123542 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
77123542 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
77123542 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
77123542 5
1 2 3 4 5 6
101
5421235 77 101
4253512 77 101
4235512 77 101
4235125 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
12354277 101
1 2 3 4 5 6
5
77123542 5
1 2 3 4 5 6
101
5421235 77
1 2 3 4 5 6
101
4253512 77
1 2 3 4 5 6
101
4235512 77
1 2 3 4 5 6
101

Reducing the Number of Comparisons
On the N
th
“bubble up”, we only need to do MAX -N comparisons.
For example:
•This is the 4
th
“bubble up”
•MAX is 6
•Thus, we have 2 comparisonsto do
15
4253512 77
1 2 3 4 5 6
101

16
#include <iostream>
usingnamespace std;
// perform bubble sort
voidbubbleSort(intarray[], intsize) {
// loop to access each array element
for(intstep = 0; step < size; ++step) {
// loop to compare array elements
for (inti= 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
inttemp = array[i];
array[i] = array[i+ 1];
array[i+ 1] = temp;
}
}
}
}

17
// print array
voidprintArray(intarray[], intsize) {
for (inti= 0; i< size; ++i) {
cout<< " " << array[i];
}
cout<< "\n";
}
intmain() {
intdata[] = {-2, 45, 0, 11, -9};
// find array's length
intsize = sizeof(data) / sizeof(data[0]);
bubbleSort(data, size);
cout<< "Sorted Array in Ascending Order: \n";
printArray(data, size);
return0;
}

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
4235125 77
1 2 3 4 5 6
101

An Animated Example
19
674523 14 6 3398 42
1 2 3 4 5 6 7 8
to_do
index
7
N 8 did_swap true

An Animated Example
20
674523 14 6 3398 42
to_do
index
7
1
N 8 did_swap false
1 2 3 4 5 6 7 8

An Animated Example
21
674523 14 6 3398 42
to_do
index
7
1
N 8
Swap
did_swap false
1 2 3 4 5 6 7 8

An Animated Example
22
674598 14 6 3323 42
to_do
index
7
1
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8

An Animated Example
23
674598 14 6 3323 42
to_do
index
7
2
N 8 did_swap true
1 2 3 4 5 6 7 8

An Animated Example
24
674598 14 6 3323 42
to_do
index
7
2
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8

An Animated Example
25
679845 14 6 3323 42
to_do
index
7
2
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8

An Animated Example
26
679845 14 6 3323 42
to_do
index
7
3
N 8 did_swap true
1 2 3 4 5 6 7 8

An Animated Example
27
679845 14 6 3323 42
to_do
index
7
3
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8

An Animated Example
28
671445 98 6 3323 42
to_do
index
7
3
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8

An Animated Example
29
671445 98 6 3323 42
to_do
index
7
4
N 8 did_swap true
1 2 3 4 5 6 7 8

An Animated Example
30
671445 98 6 3323 42
to_do
index
7
4
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8

An Animated Example
31
671445 6 98 3323 42
to_do
index
7
4
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8

An Animated Example
32
671445 6 98 3323 42
to_do
index
7
5
N 8 did_swap true
1 2 3 4 5 6 7 8

An Animated Example
33
671445 6 98 3323 42
to_do
index
7
5
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8

An Animated Example
34
981445 6 67 3323 42
to_do
index
7
5
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8

An Animated Example
35
981445 6 67 3323 42
to_do
index
7
6
N 8 did_swap true
1 2 3 4 5 6 7 8

An Animated Example
36
981445 6 67 3323 42
to_do
index
7
6
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8

An Animated Example
37
331445 6 67 9823 42
to_do
index
7
6
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8

An Animated Example
38
331445 6 67 9823 42
to_do
index
7
7
N 8 did_swap true
1 2 3 4 5 6 7 8

An Animated Example
39
331445 6 67 9823 42
to_do
index
7
7
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8

An Animated Example
40
331445 6 67 4223 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
331445 6 67 4223 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
331445 6 67 4223 98
to_do
index
6
1
N 8 did_swap false
1 2 3 4 5 6 7 8

The Second “Bubble Up”
43
331445 6 67 4223 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
331445 6 67 4223 98
to_do
index
6
2
N 8 did_swap false
1 2 3 4 5 6 7 8

The Second “Bubble Up”
45
331445 6 67 4223 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
334514 6 67 4223 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
334514 6 67 4223 98
to_do
index
6
3
N 8 did_swap true
1 2 3 4 5 6 7 8

The Second “Bubble Up”
48
334514 6 67 4223 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
33614 4567 4223 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
33614 4567 4223 98
to_do
index
6
4
N 8 did_swap true
1 2 3 4 5 6 7 8

The Second “Bubble Up”
51
33614 4567 4223 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
33614 4567 4223 98
to_do
index
6
5
N 8 did_swap true
1 2 3 4 5 6 7 8

The Second “Bubble Up”
53
33614 4567 4223 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
67614 4533 4223 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
67614 4533 4223 98
to_do
index
6
6
N 8 did_swap true
1 2 3 4 5 6 7 8

The Second “Bubble Up”
56
67614 4533 4223 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
42614 4533 6723 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
42614 4533 6723 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
42614 4533 6723 98
to_do
index
5
1
N 8 did_swap false
1 2 3 4 5 6 7 8

The Third “Bubble Up”
60
42614 4533 6723 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
42623 4533 6714 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
42623 4533 6714 98
to_do
index
5
2
N 8 did_swap true
1 2 3 4 5 6 7 8

The Third “Bubble Up”
63
42623 4533 6714 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
42236 4533 6714 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
42236 4533 6714 98
to_do
index
5
3
N 8 did_swap true
1 2 3 4 5 6 7 8

The Third “Bubble Up”
66
42236 4533 6714 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
42236 4533 6714 98
to_do
index
5
4
N 8 did_swap true
1 2 3 4 5 6 7 8

The Third “Bubble Up”
68
42236 4533 6714 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
42236 3345 6714 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
42236 3345 6714 98
to_do
index
5
5
N 8 did_swap true
1 2 3 4 5 6 7 8

The Third “Bubble Up”
71
42236 3345 6714 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
45236 3342 6714 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
45236 3342 6714 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
45236 3342 6714 98
to_do
index
4
1
N 8 did_swap false
1 2 3 4 5 6 7 8

The Fourth “Bubble Up”
75
45236 3342 6714 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
452314 3342 676 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
452314 3342 676 98
to_do
index
4
2
N 8 did_swap true
1 2 3 4 5 6 7 8

The Fourth “Bubble Up”
78
452314 3342 676 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
452314 3342 676 98
to_do
index
4
3
N 8 did_swap true
1 2 3 4 5 6 7 8

The Fourth “Bubble Up”
80
452314 3342 676 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
452314 3342 676 98
to_do
index
4
4
N 8 did_swap true
1 2 3 4 5 6 7 8

The Fourth “Bubble Up”
82
452314 3342 676 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
452314 3342 676 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
452314 3342 676 98
to_do
index
3
1
N 8 did_swap false
1 2 3 4 5 6 7 8

The Fifth “Bubble Up”
85
452314 3342 676 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
452314 3342 676 98
to_do
index
3
2
N 8 did_swap false
1 2 3 4 5 6 7 8

The Fifth “Bubble Up”
87
452314 3342 676 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
452314 3342 676 98
to_do
index
3
3
N 8 did_swap false
1 2 3 4 5 6 7 8

The Fifth “Bubble Up”
89
452314 3342 676 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
452314 3342 676 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
452314 3342 676 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>
usingnamespace std;
// perform bubble sort
voidbubbleSort(intarray[], intsize) {
// check if swapping occurs
intflag= 0;
// loop to access each array element
for(intstep = 0; step < size; ++step) {
// loop to compare array elements
for (inti= 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
inttemp = 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
voidprintArray(intarray[], intsize) {
for (inti= 0; i< size; ++i) {
cout<< " " << array[i];
}
cout<< "\n";
}
intmain() {
intdata[] = {-2, 45, 0, 11, -9};
// find array's length
intsize = sizeof(data) / sizeof(data[0]);
bubbleSort(data, size);
cout<< "Sorted Array in Ascending Order: \n";
printArray(data, size);
return0;
}

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
1329648
8329641
8349621
8649321
8964321
8694321
9864321
9864321

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
1329648

Assignment # 1
•Perform selection sort on user defined data and implement the code
in C++.
•Deadline: 4
th
March, 2024
98
Tags