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

Chapter 2 - Elementary Searching and Sorting Algorithms

computer organization and architecture power point for unifying concepts visualized at the specified level of accuracy for most students that are in a close relationship with the behavior and operating of computer systems involving core concepts under the likelihood of theorists.

Uploaded by

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

Chapter 2 - Elementary Searching and Sorting Algorithms

computer organization and architecture power point for unifying concepts visualized at the specified level of accuracy for most students that are in a close relationship with the behavior and operating of computer systems involving core concepts under the likelihood of theorists.

Uploaded by

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

Data Structure and Algorithms

Hawasa University
Chapter Two
Elementary Searching and Sorting Algorithms

This Chapter Covers


 Sequential search
 Binary Search
 Simple Sort
 Bubble Sort
 Selection Sort
 Insertion Sort
Why do we study sorting and searching
algorithms?
 They are the most common & useful tasks in any
software development.
Example:
o Searching documents over the internet
o Searching files and folders in a hard disk
o Sorting students by their name, year and so on
o Sorting file, search results by file name, date created
and so on.
o Deleting and recording data from a database
Searching Algorithm
 Searching is a process of looking for a specific element in a list of
items or determining that the item is not in the list.
 Searching is a process of finding an element from a collection of
elements.
 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.
 Searching algorithms to be considered.
 Sequential search
 Binary search
Sequential Searching
 It is natural way of searching.
 The algorithm does not assume that the data is sorted.
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.
◦ Search list from the beginning until key is found or end of list
reached.
Cont’d…

Implementation:
int i;
bool found =false;
for(i=0;i<n;i++;)
if (DataElement[i]==key)
{
found=true;
break;
}
return (found);
Example
List index 0 1 2 3 4 5 6 7
Data C A E F N Q R K
Key=Q after six comparison the algorithm returns found
Key=X after 8 comparison the algorithm returns found false
What is the Big O of sequential searching algorithm?
◦ For searching algorithm the dominant factor is
comparison of keys
◦ How many comparisons are made in sequential
searching?
◦ The maximum number of comparison is n, i.e. O (n).
Binary search

 Binary search is a fast search algorithm with run-time


complexity of Ο(log n).
 This search algorithm works on the principle of
divide and conquer.
 For this algorithm to work properly the data
collection should be in sorted form.
Binary Searching
 This searching algorithms works only on an ordered list.
The basic idea is:
 Locate midpoint of array to search
Determine if target is in lower half or upper half of an
array.
 If in lower half, make this half the array to search
 If in the upper half, make this half the array to
search
Loop back to step 1 until the size of the array to search is
one, and this element does not match, in which case
return –1.
cont …

 The computational time for this algorithm is proportional


to log2 n. Therefore the time complexity is O(log n)
Algorithm:
1. Divide the list into halves
2. See which half of the list the key belongs
3. repeat 1 and 2 until only one element remains
4. if the remaining element is equal to search key then
found =true else key not found.
How binary search works?
 For a binary search to work, it is mandatory for the target array to
be sorted. We shall learn the process of binary search with an
pictorial example. The below given is our sorted array and assume
that we need to search location of value 31 using binary search.

First, we shall determine the half of the array by using this


formula − mid = low + (high - low) / 2
Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So 4 is the
mid of array.
cont …
 Now we compare the value stored at location 4, with the value
being searched i.e. 31. We find that value at location 4 is 27,
which is not a match. Because value is greater than 27 and we
have a sorted array so we also know that target value must be in
upper portion of the array.

We change our low to mid + 1 and find the new mid value again.
low = mid + 1 mid = low + (high - low) / 2
Our new mid is 7 now. We compare the value stored at location 7 with our
target value 31.
…cont

The value stored at location 7 is not a match, rather


it is less that what we are looking for. So the value
must be in lower part from this location.

So we calculate the mid again. This time it is 5.

We conclude that the target value 31 is stored at location 5.


Implementation:
int Mid,bottom=0,top=N-1;
while(bottom<=top)
{
Mid=(bottom+top)/2;
if(key>dataElement[Mid])
bottom=Mid+1;
else if(key<dataElemen[Mid])
top=Mid-1;
else
return 1;
}
How many comparisons are made in binary search
algorithm?

n Max. Number of comparison


1 1
2 2
4 3
8 4
16 5
32 6
Then for n number of data items, logn+1 number of
comparison is needed; so, it is O(logn).
Sorting Algorithms
 Sorting is one of the most important operations performed by
computers. Sorting is a process of reordering a list of items in either
increasing or decreasing order.
 Sorting is used to arrange names or numbers in meaningful ways.
by default sorting is performed in ascending order.
 A sequence of values is said to be in increasing order, if the
successive element is greater than the previous one.
 For example, 1, 3, 4, 6, 8, 9 are in increasing order, as every next
element is greater than the previous.
 A sequence of values is said to be in decreasing order, if the
successive element is less than the current one.
 For example, 9, 8, 6, 4, 3, 1 are in decreasing order, as every next
element is less than the previous.
Simple (Elementary) Sorting Algorithms
Example…
Telephone Directory − Telephone directory keeps telephone
no. of people sorted on their names. So that names can be
searched.
Dictionary − Dictionary keeps words in alphabetical order so
that searching of any work becomes easy.
For searching, sorting is important
Take as an example a telephone directory
Sorting algorithms commonly consist of two types of
operation: comparisons and data movements
Sorting algorithms to be considered:
◦ Simple sort, bubble sort, selection sort, insertion sort
Simple Sort Algorithm

 In simple sort algorithm,


– The first element is compared with the second,
third, and all subsequent elements.

– If any of these other is less than the current first


element then the first element is swapped with that
element.

– The above step is repeated with the second, third


and all other subsequent elements.
Implementation:
void swap( dataType x, dataType y)
{
dataType temp;
temp=x;
x=y;
y=temp;
}
for (i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(dataElement[i]>dataElement[j])
Swap(dataElement[i],dataElement[j]);
Example
i j (pass) Data elements Remarks
0 1 2,4,3,1 The smallest element
2 2,4,3,1 is placed in its
proper position
3 1,4,3,2
1 2 1,3,4,2 The second smallest
3 1,2,4,3 element is placed
in its proper place.
2 3 1,2,3,4 All the data elements
are sorted at the n-
1 path
20
Algorithm Analysis
Analysis involves number of comparisons and number of
swaps.
Iteration Number of comparison number of swaps
(compares)
1st pass n-1 n-1
2nd pass n-2 n-2
. . .
. . .
. . .
(n-1)th 1 1
Total n(n-1)/2 n(n-1)/2
Therefore Big O of simple sort algorithm is
n(n-1)/2+n(n-1)/2=O(n2)
Bubble Sort Algorithm

 It is a method which uses the interchanging of adjacent


pair of element in the array.
 After each pass through the data one element (the largest
or smallest element) will be moved down to the end of the
array in its proper place.

Implementation:
for (int i = 0; i < n-1; i++)
for (int j = n-1; j > i; j--)
if (data[j] < data[j-1])
swap(data[j],data[j-1]);
Cont’d…
Bubble sort Algorithm:
 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.
 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.
 Compare each element (except the last three) with its neighbor to the
right.
 Continue as above until you have no unsorted elements on the left.
 It 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
Analysis
Passes number of comparison number of swaps
1 n-1 n-1
2 n-2 n-2
. . .
. . .
. . .
n-1 1 1
total n(n-1)/2 n(n-1)/2
=O(n2)

Sort by using bubble sort 54 26 93 17 77 31 44 55 20


Selection Sort Algorithm
Each element is swapped directly with the element that
occupies its correct position
Basic Idea:
– Scan array to find smallest element: swap it with
element in first position
– Scan remainder of array to find smallest element:
swap it with element in second position etc. until all
elements placed in correct position
Selection sort

The selection sort algorithm is in many ways similar to simple sort
algorithms. 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.

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.
Example
1. void selectionSort(int list[ ] ) 8 7 9 11 3
2. {
3. int minIndex, temp;
4. for (int i = 0; i <= n - 2; i++)
5. {
6. minIndex = i;
7. for (int j = i + 1; j <= n-1; j++)
8. if (list[j] < list[minIndex])
9. minIndex = j;
10. if (minIndex != i)
11. {
12. temp = list[i];
13. list[i] = list[minIndex];
14. list[minIndex] = temp;
Implementation
for (i=0; i<=n-1;i++) 8 7 9 11 3
{
min_index=i;
for (j=i+1; j<=n-1;j++)
{
if (dataElement[j] < dataElement[min_index])
min_index=j;
}
swap (dataElement[i],dataElement[min_index]);
}
Analysis:

Pass number of comparison number of swaps


1 n-1 1
2 n-2 1
. . .
. . .
. . .
n-1 1 1
total n(n-1)/2 n-1
It is in O(n2)
Insertion Sort Algorithm
• As each element in the list is examined it is put into its
proper place among the elements that have been
examined ( and put in correct order).

• When the last element is put into its proper position the
list is sorted and the algorithm is done.
Algorithm
 Insertion sort 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.
Example
implementation
for (int i = 1; i <= n-1; i++)
{
for(int j = i;j>=1; j--)
{
if(list[j-1] > list[j]) example
{
int temp = list[j]; 21, 6 ,15, 23, 9
list[j] = list[j-1];
list[j-1] = temp;
}
else break;
}
}
}
Analysis

Pass number of comparison number of swaps


1 1 1
2 2 2
. . .
. . .
. . .
n-1 n-1 n-1
Total n(n-1)/2 n(n-1)/2
It is in O(n2)
Lab ()

 Write a C++ program that will implement all of the


sorting and searching algorithms. The program
should accept different unsorted data items from user
and sort using all algorithms and should tell which
algorithm is efficient for that particular unsorted data
items. (Hint use how many comparison and swaps are
done so the one with the less number of swap and
comparison number is considered as an efficient
algorithm for that particular data items.)
39

You might also like