4_DS&A_Lecture_4_Simple_Sorting_and_Searching_Algorithms_
4_DS&A_Lecture_4_Simple_Sorting_and_Searching_Algorithms_
1
Why do we study sorting and searching
algorithms?
❖ These algorithms are the most common and useful
tasks operated by computer system.
4
Algorithm:
❖ In a linear search, we start with top (beginning) of
the list, and compare the element at top with the
key.
8
Implementation: Example:
2 5 11 21 24 30 46 48 50 94
int BinarySearch(int list[ ], int 0 1 2 3 4 5 6 7 8 9
key)
{ ❖ Let our key be 50.
int found=0,index=0;
int top=n-1,bottom=0,middle; ❖ middle =0+9/2 ≈ 4.
do{ ❖ 50>24, bottom = 4+1=5.
middle=(top + bottom)/2;
if(key==list[middle]) ❖ middle = 5+9/2 = 7, 50>48.
found=1; ❖ Bottom = 7+1 = 8.
else{
if(key<list[middle]) ❖ Middle = 8+9/2 = 8.5 ≈ 8.
top=middle-1; ❖ Element at index 8 is 50 which
else
bottom=middle+1; is the same as our key (50).
} ❖ The key and our middle
}
}while(found==0 && top>=bottom); element match, Search over !
if(found==0)
index=-1;
else
index=middle;
return index; Complexity Analysis:
} ❖ Big-Oh of Binary search
algorithm = logn.
❖ How ? -> Assignment. 9
2. Sorting Algorithms
10
2. Simple Sorting Algorithms
❖ Sorting: is a process of reordering a list of items in either
increasing or decreasing order.
❖ Ordering a list of items is fundamental problem of computer
science.
❖ Sorting is the most important operation performed by computers.
❖ Sorting is the first step in more complex algorithms.
Stable:
❖ If two elements that are equal remain in the same relative position
after sorting is completed.
13
I. Simple sorting
Algorithm:
❖ In simple sort algorithm the first element is compared
with the second, third and all subsequent elements.
❖ The above steps are repeated with the second, third and
all subsequent elements. 14
Implementation:
16
Example: Suppose we have 32 unsorted data.
a). How many comparisons are made by sequential search in the
worst-case?
➔ Number of comparisons =32.
b). How many comparisons are made by binary search in the
worst-case? (Assuming simple sorting).
➔ Number of comparisons = Number of comparisons for
sorting + Number of comparisons for binary search
= (n*(n-1))/2 + logn
= 32/2(32-1)+ log 32
=16*31 + 5
c). How many comparisons are made by binary search in the
worst-case if data is found to be already sorted?
➔ Number of comparisons = log2 32 = 5.
17
II. Bubble sort
Algorithm:
I. Compare each element (except the last one) with
its neighbor to the right.
❖ If they are out of order, swap them.
❖ This puts the largest element at the very end.
❖ The last element is now in the correct and final place.
II. Compare each element (except the last two) with
its neighbor to the right.
❖ If they are out of order, swap them.
❖ This puts the second largest element before last.
❖ The last two elements are now in their correct and18 final places.
III. Compare each element (except the last three)
with its neighbor to the right.
IV. Continue as above until you have no unsorted
elements on the left.
Disadvantage:
❖ Horribly inefficient.
20
Example of bubble sort
7 2 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8
2 7 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8
2 7 8 5 4 2 5 7 4 8 2 4 5 7 8 (done)
2 7 5 8 4 2 5 4 7 8
2 7 5 4 8
Implementation:
23
III. Selection Sort
Algorithm
❖ The selection sort algorithm is in many ways similar to
simple sort algorithm.
i=0 J=1 7 9 11 3
J=2 7 9 11 3
J=3 7 9 11 3
i=1 J=2 3 9 11 7
J=3 3 9 11 7
i=2 J=3 3 7 11 9
Finally 3 7 9 1126
Implementation:
29
❖ Insertion sort works by inserting item into its proper place
in the list.
❖ Insertion sort is simply like playing cards: To sort the
cards in your hand, you extract a card, shift the remaining
cards and then insert the extracted card in the correct
place.
❖ This process is repeated until all the cards are in the
correct sequence.
❖ Is over twice as fast as the bubble sort and is just as easy to
implement as the selection sort.
Advantage:
❖ Relatively simple and easy to implement.
Disadvantage:
❖ Inefficient for larger lists.
30
4 4 3
3 4 4
2 2 2 2
1 1 1 1
3 3 2
4 3
4 3
2 4
4
1 1
1 1
31
2 2 1
3 3 2 2
4 4 3 3
1 4 4
32
Implementation: