Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

4_DS&A_Lecture_4_Simple_Sorting_and_Searching_Algorithms_

Uploaded by

brotadese50
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

4_DS&A_Lecture_4_Simple_Sorting_and_Searching_Algorithms_

Uploaded by

brotadese50
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Chapter Four

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.

❖ Computers spend a lot of time searching and sorting.

1. Simple Searching algorithms

❖ Searching:- is a process of finding an element in a


list of items or determining that the item is not in
the list. 2
❖ To keep things simple, we shall deal with a list of
numbers.

❖ A search method looks for a key, arrives by


parameter.

❖ By convention, the method will return the index of


the element corresponding to the key or, if
unsuccessful, the value -1.
3
❖ There are two simple searching algorithms:
a) Sequential Search, and
b) Binary Search

a). Sequential Searching (Linear)

❖ The most natural way of searching an item.


❖ Easy to understand and implement.

4
Algorithm:
❖ In a linear search, we start with top (beginning) of
the list, and compare the element at top with the
key.

❖ If we have a match, the search terminates and the


index number is returned.

❖ If not, we go on the next element in the list.

❖ If we reach the end of the list without finding a


match, we return -1.
5
Implementation:

❖ Assume the size of the list is n.

int LinearSearch(int list[ ], int key)


{
index=-1;
for(int i=0; i<n; i++)
{
if(list[i]==key)
{
index=i;
break;
}
}
return index;
} 6
Complexity Analysis:

❖ Big-Oh of sequential searching ➔ How many


comparisons are made in the worst case ? n
❖ O(n).

b). Binary Searching

❖ Assume sorted data.


❖ Use Divide and conquer strategy (approach).
7
Algorithm:

I. In a binary search, we look for the key in the middle of the


list. If we get a match, the search is over.
II. If the key is greater than the element in the middle of the
list, we make the top (upper) half the list to search.
III. If the key is smaller, we make the bottom (lower) half the
list to search.
IV. Repeat the above steps (I,II and III) until one element
remains.
V. If this element matches return the index of the element, else
return -1 index. (-1 shows that the key is not in the list).

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.

Two basic properties of sorting algorithms:


In-place:
❖ It is possible to sort very large lists without the need to additional
working storage.
11
Simple Sorting Algorithms

Stable:
❖ If two elements that are equal remain in the same relative position
after sorting is completed.

Two classes of sorting algorithms:


O(n2): How ? -> Assignment.
❖ Includes the bubble, insertion, and selection sorting algorithms.
O(nlog n): How ? -> Assignment.
❖ Includes the heap, merge, and quick sorting algorithms.
12
Simple Sorting Algorithms
Simple sorting algorithms include:
I. Simple sorting
II. Bubble Sorting
III. Selection Sorting
IV. Insertion Sorting

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 second, third and
all subsequent elements. 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;
}
} Complexity Analysis:
❖ Big oh = (n*(n-1))/2 =15O(n2).
❖ How ? -> Assignment.
` Example:
0 1 2 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
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.

❖ 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.
19
Advantage:

❖ Simplicity and ease of implementation.

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:

Void BubbleSort(int list[ ])


{
int temp;
for (inti=n-2; i>=0; i--) {
for(intj=0;j<=i; j++)
if (list[j] > list[j+1])
{
temp=list[j];
list[j])=list[j+1];
list[j+1]=temp;
}
} Complexity Analysis:
} ❖ Big oh = O(n2).
❖ How ? -> Assignment.
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
III. Selection Sort
Algorithm
❖ The selection sort algorithm is in many ways similar to
simple sort algorithm.

❖ The idea of algorithm is quite simple. Array is imaginary


divided into two parts - sorted one and unsorted one.

❖ At the beginning, sorted part is empty, while unsorted one


contains whole array.

❖ At every step, algorithm finds minimal element in the


unsorted part and adds it to the end of the sorted one.
24

❖ When unsorted part becomes empty, algorithm stops.


❖ 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.
25
Example:
 Example : -

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:

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;
}
}
}
Complexity Analysis:
❖ Big oh = O(n2).
27
❖ How ? -> Assignment.
IV. Insertion Sort
Algorithm:
❖ Insertionsort algorithm somewhat resembles Selection
Sort and Bubble sort.

❖ Array is imaginary divided into two parts - sorted one and


unsorted one.

❖ At the beginning, sorted part contains first element of the


array and unsorted one contains the rest.

❖ At every step, algorithm takes first element in the unsorted


part and inserts it to the right place of the sorted one.

❖ When unsorted part becomes empty, algorithm stops.


28
Using binary search
❖ It is reasonable to use binary search algorithm to
find a proper place for insertion.

❖ This variant of the insertion sort is called binary


insertion sort.

❖ After position for insertion is found, algorithm


shifts the part of the array and inserts the element.

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:

Void InsertionSort(int list[])


{
for (int i = 1; i <= n-1; i++) {
for(int j = i;j>=1; j--) {
if(list[j-1] > list[j])
{
int temp = list[j];
list[j] = list[j-1];
list[j-1] = temp;
}
else
break;
Complexity Analysis:
}
✓ Number of comparisons may vary depending on
}
the insertion algorithm.
} ❖ O(n2) for shifting or swapping methods.
❖ O(nlogn) for binary insertion sort.
❖ How ? -> Assignment. 33
End of Lesson
Thank you !

You might also like