Chapter 3 - Simple Searching and Sorting
Chapter 3 - Simple Searching and Sorting
1
Topics
▪ Searching
– Linear/Sequential Search
– Binary Search
▪ Sorting
– Bubble Sort
– Insertion Sort
– Selection sort
2
Common Problems
3
Searching
5
Algorithm : Linear Search
When do we know that there wasn’t item in the List that matched the key?
4/21/2021 6
Linear Search (Sequential Search)
• Example Implementation:
4/21/2021 8
Linear Search – Questions ?
▪ Assume you have observed that some of the data items are searched
more frequently than others in the list. What modification can be
made to make this algorithm better ?
▪ Homework
– Write an implementation for your answers of Q1 and Q2
9
Sequential Search of
Ordered vs. Unordered List
– Also, keep in mind that the list has to first be placed in order for the ordered
search.
▪ Conclusion: the efficiency of these algorithms is roughly the same.
– So, if we need a faster search, on sorted list we need a completely different
algorithm.
11
Binary Search
▪ If we have an ordered list and we know how many things are in the
list, we can use a different algorithm named binary-search.
▪ The binary search gets its name because the algorithm continually
divides the list into two parts.
– Uses the divide-and-conquer technique to search the list
12
Basic Idea: Binary Search
4/21/2021 13
Example Implementation
int binary_search(int list[],int n, int key)
{ int left=0; int right=n-1;
int mid;
while(left<=right){
mid=(left+right)/2;
if(key==list[mid])
return mid;
else if(key > list[mid])
left=mid+1;
else
right=mid-1;
}
return -1; 14
Example with illustration: Binary Search
4/21/2021 15
How Fast is a Binary Search?
16
How Fast is a Binary Search? (con’t)
▪ Internal sorting:
– The process of sorting is done in main memory
– The number of elements in main memory is relatively small (less than
millions). The input is fit into main memory
▪ In this type of sorting the main advantage is memory is directly
addressable,
– which bust-up performance of the sorting process.
▪ External sorting:
– Can not be performed in main memory due to their large input size. i.e., the input
is much larger to fit into main memory
– Sorting is done on disk or tape.
– It is device dependent than internal sorting 20
Common Sorting Algorithms
Bubble sort is a simple algorithm with a memorable name and a simple idea
▪ The idea:
– Starting at the front, traverse the list, find the largest item, and move (or
bubble) it to the top
– How:
▪ Compare adjacent elements and swap the elements if they are not in
order.
– With each subsequent iteration, find the next largest item and bubble it up
towards the top of the array
▪ This algorithm is not suitable for large data sets as its average and worst
case complexity are of O(n2) where n is the number of items.
4/21/2021 22
Obama on bubble sort
When asked the most efficient way to sort a million 32-bit integers,
Senator Obama had an answer:
www.youtube.com/watch?v=k4RRi_ntQc8
Algorithm: Bubble Sort
4/21/2021 25
Implementation: Bubble Sort
void bubbleSort (int a[ ] , int size)
{
int i, j, temp;
for ( i = 0; i < size; i++ )/*controls passes through the list */
{
for ( j = 0; j < size - 1; j++ )/*performs adjacent comparisons */
{
if ( a[ j ] > a[ j+1 ] )/*determines if a swap should occur */
{
temp = a[ j ]; /* swap is performed */
a[ j ] = a[ j + 1 ];
a[ j+1 ] = temp;
}}}
}
26
Example with illustration: Bubble Sort
We start with the element in the first location, and move forward:
– if the current and next items are in order, continue with the next item, otherwise swap the two
entries
– After one loop, the largest element is in the last location
– Repeat again
4/21/2021 27
Analysis- Bubble Sort
How many comparisons?
(n * n) = O(n2) In all best, average and worst cases
How many swaps?
0 = O(1) in best cases where the list is already sorted
n2= O(n2) in worst case scenario where the list is sorted in reverse order
How much space?
In-place algorithm
Meaning, bubble sort requires no additional memory therefore, O(1).
28
Insertion Sort
▪ Consider the following observations:
– A list with one element is sorted
– In general, if we have a sorted list of k items, we can insert a new
item to create a sorted list of size k + 1
▪ Insertion sort works the same way as arranging your hand when
playing cards.
– Out of the pile of unsorted cards that were dealt to you, you pick up a card and
place it in your hand in the correct position relative to the cards you’re already
holding.
4/21/2021 30
Implementation- Insertion sort
5 7
32
Arranging Your Hand
5 7
5 6 7
5 6 7 K
5 6 7 8 K
33
Insertion Sort
7 K Unsorted – shaded
▪ Look at 2nd item - 5.
1
7 5 ▪ Compare 5 to 7.
v ▪ 5 is smaller, so move 5 to temp, leaving an empty slot in
7 5 position 2
▪ Move 7 into the empty slot, leaving position 1 open .
5< 7 3
34
Insertion Sort (con’t)
5 7
2 >
5 6< 7 3
35
Insertion Sort (con’t)
5 6 7 K
36
Insertion Sort (con’t)
5 6 7 K 8
1
5 6 7 K 8
v
5 6 7 8
K
5 6 7 K
2 >
5 6 7 8 K 3
<
37
Analysis – Insertion sort
▪ Basic Idea:
– Loop through the list from I = 0 to n - 1.
– Select the smallest element in the array from i to n
– Swap this value with value at position i.
▪ Demos\21DemoSelectionSort.mov
▪ This algorithm is not suitable for large data sets as its
average and worst case complexities are of O(n2), where n is
the number of items.
39
Algorithm: Selection Sort
4/21/2021 40
Implementation- Selection Sort
void selection_sort(int list[])
{
int i, j, smallest;
for(i = 0; i < n; i++){
smallest = i;
for(j = i + 1; j < n; j++){
if(list[j] < list[smallest])
smallest = j;
}//end of inner loop
temp = list[smallest];
list[smallest] = list[i];
list[i] = temp;
} //end of outer loop
}//end of selection_sort
41
Example with illustration: Selection Sort
4/21/2021 42
Analysis- Selection Sort
43
Exercise
Element 1 2 3 4 5 6 7 8
Data 27 63 1 72 64 58 14 9
1. Insertion Sort
2. Selection Sort
3. Bubble Sort
4/21/2021 45
Assignment (15%)
4/21/2021 46
Title
4/21/2021 48
Next Time!!
Ch4-List
49