Chapter 3
Chapter 3
1
Why do we study sorting and searching
algorithms?
• These algorithms are the most common and useful
tasks operated by computer system.
3
• There are two simple searching algorithms:
a) Sequential Search, and
b) Binary Search
4
Algorithm:
5
Implementation:
6
Complexity Analysis:
8
• Repeat the above steps (I,II and III) until one
element remains.
9
Implementation: if(found==0)
int BinarySearch(int list[ ], int key)index=-1;
{ else
int found=0,index=0; index=middle;
int top=n-1,bottom=0,middle;return index;
do{ }
middle=(top + bottom)/2;
if(key==list[middle])
found=1; Complexity Analysis:
else{
if(key<list[middle]) Example: Find Big-Oh
top=middle-1; of Binary search
else algorithm in the worst
bottom=middle+1; case analysis.
}
}
}while(found==0 && top>=bottom);
10
2. Simple Sorting Algorithms
Stable:
If two elements that are equal remain in the same
relative position after sorting is completed.
12
Simple Sorting Algorithms
13
I. Simple sorting
Algorithm:
• In simple sort algorithm the first
element is compared with the second,
third and all subsequent elements.
• If any one of the other elements is less
than the current first element then the
first element is swapped with that
element.
• Eventually, after the last element of the
list is considered and swapped, then the
first element has the smallest element
in the list.
• The above steps are repeated with the 14
Implementation:
Void SimpleSort(int list[])
{
for(int i=0; i<=n-2;i++)
for(int j=i+1; j<=n-1; j++)
if(list[i] > list[j])
{
int temp;
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
15
`
0 1 2
Example: n-1
--------
i
j
4 2 3 1
i=0 j=1 2 4 3 1
j=2 2 4 3 1
j=3 1 4 3 2
i=1 j=2 1 3 4 2
j=3 1 2 4 3
i=2 j=3 1 2 3 4
16
`
Analysis: O(?)
1st pass----- (n-1) comparisons
2nd pass---- (n-2) comparisons
|
|
|
(n-1)th pass--- 1 comparison
T(n)=1+2+3+4+-------+(n-2)+(n-1)
= (n*(n-1))/2
=n2/2-n/2
=O(n2)
17
Complexity Analysis:
18
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? 19
II. Bubble sort
Algorithm:
III. 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 and final
places
20
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.
• Is the oldest, simplest, and slowest sort in use.
• It works by comparing each item in a list with
an item next to it, and swap them if required.
• This causes the larger values to “bubble” to the
end of the list while smaller values to “sink”
towards the beginning of the list.
• In general case, bubble sort has O(n2) level of
complexity.
21
Advantage:
• Simplicity and ease of implementation.
Disadvantage:
• Horribly inefficient.
22
`
Example of Bubble sort
4 2 3 1
i=3 j=1 2 4 3 1
j=2 2 3 4 1
j=3 2 3 1 4
i=2 j=1 2 3 1 4
j=2 2 1 3 4
i=1 j=1 1 2 3 4
23
Another Example of Bubble sort
27548
24
Implementation:
26
III. Selection Sort
Algorithm
27
• Works by selecting the smallest unsorted item
remaining in the list, and then swapping it with
the item in the next position to be filled.
• Similar to the more efficient insertion sort.
• It yields a 60% performance improvement
over the bubble sort.
Advantage:
• Simple and easy to implement.
Disadvantage:
• Inefficient for larger lists.
28
Selection Sort Example:
7 9 11 3
i=0 j=1 7 9 11 3
j=2 7 9 11 3
j=3 3 9 11 7
i=1 j=2 3 9 11 7
j=3 3 7 11 9
i=2 j=3 3 7 9 11
29
30
Implementation:
void selectionSort(int list[ ] ) {
int minIndex, temp;
for (int i = 0; i <= n - 2; i++) {
minIndex = i;
for (j = i + 1; j <= n-1; j++)
if (list[j] < list[minIndex])
minIndex = j;
if (minIndex != i) {
temp = list[i];
list[i] = list[minIndex];
list[minIndex] = temp;
}
}
31
}
Complexity Analysis
33
IV. Insertion Sort
Algorithm:
• Insertion sort algorithm somewhat resembles Selection
Sort and Bubble sort.
• Array is imaginary divided into two parts - sorted one
and unsorted one.
35
• 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 large lists.
36
4 4 3
3 4 4
2 2 2 2
1 1 1 1
3 3 2
3
4 4 3
4
2 4
1
1 1 1
37
2 2 1
3 3 2 2
4 4 3 3
1 4 4
38
39
C++ implementation
41