Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Chapter Two

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Chapter Two

Simple Sorting and


Searching Algorithms
Department of Computer Science
Academic Year 2022
Abebe.zeihun@aau.edu.et
Sorting Algorithms
• Sorting refers to arranging data in a particular format. Sorting
algorithm specifies the way to arrange data in a particular order.
• Most common orders are in numerical or lexicographical order.
• The importance of sorting lies in the fact that data searching
can be optimized to a very high level, if data is stored in a
sorted manner.
• Sorting is also used to represent data in more readable formats.
• Telephone Directory − The telephone directory stores the
telephone numbers of people sorted by their names, so that the
names can be searched easily.
• Dictionary − The dictionary stores words in an alphabetical
order so that searching of any word becomes easy.
Bubble Sort
• Bubble sort is a simple sorting algorithm.
• It’s comparison-based algorithm in which each pair of
adjacent elements is compared and the elements are
swapped if they are not in order.
• Bubble sort is a reasonable sort to use for sorting a
fairly small number of items and is easy to implement.
• It’s not suitable for large data sets as its average and
worst case complexity are of Ο(n2) where n is the
number of items.
• For ex. Let an array having five numbers.

• Step-1: In the 1st iteration the 0th element 8 is


compared with 1st element 12 and since 8 is less than
12 then there is nothing to do.
• Step-2: Now the 1st element 12 is compared with the
2nd element 10 and here the swapping will be
performed.
• Step-3: This process is repeated until (n – 2)th
element is compared with the (n – 1)th element and
during comparison if the 1st element other wise no
interchange.
• Step-4: If there are n number of elements then n-1
iterations are required.
• Algorithm:
Complexity of Bubble Sort
• The complexity of any sorting algorithm depends
upon the number of comparisons and swaps.
• To compute the complexity of bubble sort, we need
to calculate the total number of comparisons.
• It can be given as:
Insertion Sort
• Insertion sort is implemented by inserting a particular
element at the appropriate position.
• In this method, the first iteration starts with the comparison
of 1st element with the 0th element.
• In the second iteration 2nd element is compared with the 0th
element and 1st element.
• In general, in every iteration an element is compared with all
the
elements before it.
• During comparison if it is found that the element in question
can be inserted at a suitable position then a space is
created for it by shifting the other elements one position to
the right and inserting the element at a suitable position.
• This procedure is repeated for all the elements in the array.
• For Ex. Consider the array
76 52 66 45 33

• Step-1 : In the first loop the 1 element 52 is compared with


st

the 0 element 76. Since 52<76, 52 is inserted at 0 place.


th th

The 0 element 76 is shifted one position to the right.


th

• Step-2 : In the second loop, the 2 element 66 and the 0


nd th

element 52 are compared since 66>52, then no change will


be
performed. Then the second element is compared with the
1st
element and same procedure will be continued.
• Step-3 : In the third loop ,the 3 element is compared with
rd

the 0 element 52, since 45 is smaller than 52 then 45 is


th

inserted in the 0 place in the array and all the elements


th

from 0 to 2 are shifted to right by one position.


th nd
• Step-4 : In the fourth loop the fourth element 33 is
compared with the 0th element 45,since 33<45
then 4th element is inserted into the 0th place and
all the elements from 0th to 3rd are shifted by one
position and as a result we will got the sorted
array.
• Algorithm:
Complexity of Insertion Sort
• If the initial tile is sorted, only one comparison is
made on each iteration, so that the sort is O(n).
• If the file is initially sorted in the reverse order the
worst case complexity is O(n2)
• Since the total number of comparisons is:
(n-1)+ (n-2) +...+3+2+1= (n-1) * n/2, which is O (n2)

• The average case or the average number of


comparisons in the simple insertion sort is O (n2).
Selection Sort
• This is the simplest method of sorting. The selection
sort starts from 1st element and searches the entire list
until it finds the minimum value.
• The sort places the minimum value in the first place,
select the second element and searches for the second
smallest element. This process will continue until the
complete list is sorted.
• The smallest element is selected from the unsorted
array and swapped with the leftmost element, and that
element becomes a part of the sorted array. This
process continues moving unsorted array boundary by
one element to the right.
• Working of Selection sort: Select first element as
minimum.
• ALGORITHM
Step-1 Repeat through step-3 while I <
n
Step-2 Repeat through step-3 while k=
I+1 to n
Step-3 If arr[i] >arr[k]
Temp = arr[k]
arr[k] = arr[i]
arr[i] = temp
Step-4 exit
Complexity of Selection Sort
• Number of comparisons: (n-1)+(n-2) + (n-3) + …… +
2+1
• Nearly equal to n2
• Time complexity t(n) = O(n2)
• Also, we can analyze the complexity by simply
observing the number of loops. There are 2 loops
so the complexity is n*n = n2.
Quick Sort
• Quick sort uses the concepts of divide and conquer
method. It is also known as partition exchange sort.
• To Partition the list, we first choose some key from
the list for which about half the keys will come
before and half after.
• This selected key is called as pivot.
• We next partition the entries so that all the keys
which are less than the pivot come in one sub list
and all the keys which are greater than the pivot
come in
another sub list.
Algorithm
Partition Algorithm:
• Partition algorithm rearranges the sub arrays in a
place.
Figure: shows the execution trace partition algorithm
Time complexity
• The worst case occurs when the partition
process always picks greatest or smallest
element as pivot.
• If we consider above partition strategy where
last element is always picked as pivot, the
worst case would occur when the array is
already sorted in increasing or decreasing
order.
Following is recurrence for worst case
Searching Algorithms
• Searching is the process of finding out the
position of an element in an list.
• If the element is found inside the list then the
searching process is successful otherwise the
searching process is failure.
• Searching is of two types such as
• Linear Search
• Binary Search
Linear Search
• This is the simplest method of searching .In this method
the element to be found is sequentially searched in the list.
• This method can be applied to a sorted or an un-sorted
list.
• Searching is case of sorted list starts from 0th element
and continues until the element is found or an element
whose value is greater (Assuming the list is sorted in
ascending order) than the value being searched is
reached.
• As against this, searching in case of unsorted list starts
from 0th element and continues until the element is found
or the end of list is reached.
How linear search works?
• Linear search is a very simple search algorithm.
• In this type of search, a sequential search is made
over all items one by one.
• Every item is checked and if a match is found then
that particular item is returned, otherwise the
search continues till the end of the data collection.
• Algorithm
Binary Search
• Binary search method is very fast and efficient with run-
time complexity of Ο(log n). This method requires that
the list of element is to be sorted.
• Binary search looks for a particular item by comparing
the middle most item of the collection. If a match
occurs, then the index of item is returned.
• If the middle item is greater than the item, then the item
is searched in the sub-array to the left of the middle item.
• Otherwise, the item is searched for in the sub-array to
the right of the middle item. This process continues on
the sub-array as well until the size of the subarray
reduces to zero.
How Binary search works?
• For a binary search to work, it is mandatory for the
target array to be sorted.
• Ex.
• Now we compare the value stored at location 4,
with the value being searched, i.e. 31.
• We find that the value at location 4 is 27, which is
not a match.
• As the value is greater than 27 and we have a
sorted array, so we also know that the target value
must be in the upper portion of the array.
• Our new mid is 7 now. We compare the value
stored at location 7 with our target value 31.
• We conclude that the target value 31 is stored at
location 5.
• Binary search halves the searchable items and thus
reduces the count of comparisons to be made to
very less numbers.

You might also like