CS112 Programming Languages 1 (Lecture 7 - Spring 2020)
CS112 Programming Languages 1 (Lecture 7 - Spring 2020)
Programming Languages 1
( General Division – Software Engineering Program – Medical Informatics Program )
Lecture 7
Search and Sort Algorithms [ Part 1 ]
Bubble Sort, Linear Search, .. & .. Binary Search
Spring 2020 (Semester 2)
Week 8
Course & Lectures are based on their counterparts in the following courses:
o Harvard University's CS50; An introduction to the intellectual enterprises of
computer science and the art of programming, Harvard School of
Engineering and Applied Sciences.
o UNSW's CS1: Higher Computing, by Richard Buckland – The University of New
South Wales.
o MIT's 6.087 Practical Programming in C (2010), 6.096 Introduction to C++
(2011), and 6.S096 Introduction to C and C++ (2013), MIT (Massachusetts
1
Institute of Technology) OpenCourseWare.
2
2
Some Popular Sorting Algorithms:
• Bubble Sort
• Selection Sort
• Insertion Sort
• Merge Sort
• Quick Sort
• Heap Sort
Sort Algorithms
So, Sorting takes an unordered collection and makes it an
ordered one.
Bubble
Sort
4
Bubble sort is one way to sort an array of numbers. Adjacent
values are swapped until the array is completely sorted. This
algorithm gets its name from the way values eventually "bubble"
up to their proper position in the sorted array.
Sort Algorithms
Bubble Sort
o The algorithm can be used to sort a list in either ascending or
descending order.
Let's sort the elements of this array in ascending order.
0 1 2 3
8 6 4 2
First Pass: 3 Swaps
During our first pass through
the array, we've swapped (8,6),
0 1 2 3
(8,4), and (8,2). The value 8 has
"bubbled" up to its correct
position.
8 6 4 2
0 1 2 3
6 8 4 2
0 1 2 3
6 4 8 2
Second Pass: 2 Swaps
During our second pass, we've
swapped (6,4) and (6,2). The
0 1 2 3
value 6 has "bubbled" up to its
correct position. 6 4 2 8
0 1 2 3
4 6 2 8
0 1 2 3
4 2 6 8
Third Pass: 1 Swap
During our third pass, we've
swapped (4,2). The value 4 has
0 1 2 3
"bubbled" up to its correct
position. 4 2 6 8
0 1 2 3
2 4 6 8
0 1 2 3
2 4 6 8
Fourth Pass: 0 Swaps
On this final pass through the
list no swaps were made,
0 1 2 3
signaling that the array has
been completely sorted. 2 4 6 8
0 1 2 3
2 4 6 8
0 1 2 3
2 4 6 8
And this is how you can implement bubble sort in C to sort an array in
an ascending order.
Algorithm
initialize counter
do
{
set counter to 0
1 2 3 4 5 6
77 42 35 12 101 5
Sorting Algorithms: Bubble Sort ..
Example 1:
The bubble sort traverses a collection of elements:
o Move from the front to the end
o “Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
42 35 7712 Swap12
77 101 5
Sorting Algorithms: Bubble Sort ..
Example 1:
The bubble sort traverses a collection of elements:
o Move from the front to the end
o “Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
42 35 12 77 101 5
No need to swap
Sorting Algorithms: Bubble Sort ..
Example 1:
The bubble sort traverses a collection of elements:
o Move from the front to the end
o “Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
1 2 3 4 5 6
42 35 12 77 5 101
One Pass:
}
Sorting Algorithms: Bubble Sort ..
the Swap Function within the Bubble Sort
Can we write a function for swapping? What will the
parameters of that function be?
26
Sorting Algorithms: Bubble Sort ..
the Bubble Sort using the Swap Function
28
Search Algorithms: Searching an Array ..
Back in the days when phone numbers weren’t stored in cell phones, you might
have actually had to look them up in a phonebook. How did you go about that?
If you wanted to look up someone with the last name “Smith,” you could flip
through the phonebook one page at a time ( linear search?! ). You don’t need to
be a computer scientist to know that this is an inefficient approach.
Instead, we could start by flipping to the middle of the phonebook, and let's say
we turn to a page with "M" on it. Since we know "Smith" comes after "Mike," we
can literally tear the phonebook in half, throw away the left half of the
phonebook, and leave ourselves with only the right half. We've just broken the
problem in two!
Once again, we flip to the middle and find ourselves at “R.” We can again throw
away the left half. As we continue tearing the book in half and throwing away
pieces of it, we will eventually be left with a single page on which the name
“Smith” appears (assuming it was there in the first place).
Binary Search ..
Does the array contain 7?
We can apply the same logic to searching for a value in an
array of sorted numbers.
0 1 2 3 4 5 6
1 3 5 6 7 9 10
Binary Search ..
0 1 2 3 4 5 6
1 3 5 6 7 9 10
We see that array[3] is 6, which is < 7.
Is array[3] == 7?
Is array[3] < 7?
Is array[3] > 7?
So, just as we tore off half the phone book, we can now
disregard the entire left half of this array.
Binary Search ..
0 1 2 3 4 5 6
1 3 5 6 7 9 10
Next, check the value stored at the middle index of what's
left of the array. We can see that array[5] is 9, which is > 7.
Is array[5] == 7?
Is array[5] < 7?
Is array[5] > 7?
This time, we'll discard the right portion of what's left of
the array.
Binary Search ..
0 1 2 3 4 5 6
1 3 5 6 7 9 10
array[4] == 7! We've found the value we were searching
for!
Is array[4] == 7?
Is array[4] < 7?
Is array[4] > 7?
Note however, that this array was presorted ..
low → low index in range
Binary Search .. high → high index in range
middle= (low+ high)/2
low high middle high Integer
low
division
0 1 2 3 4 5 6 7 8 9
Sorted
5 17 35 59 77 79 83 90 92 120
array
key < a [middle] key > a [middle]
= Key
high = middle - 1 low = middle + 1
Return middle
It is required to find the index of Key in the array
If Key == a[middle] then Return middle
Otherwise, If Key < a[middle] then Search in the left part of a
Otherwise, Search in the right part of a
And So On …
Note that: 36
37
Section 6.8
40
Thanks! .. Questions?
41