Why do westudy sorting and searching
algorithms?
they are the most common & useful tasks in any
software development
they take more than 25% of running time of
computers task
Example:
Searching documents over the internet
Searching files and folders in a hard disk
Sorting students by their name, year and so on
Sorting file, search results by file name, date created and so
on
Deleting and recording data from a database
3.
Simple searching Algorithm
Searchingis a process of finding an element from a
collection of elements.
Searching algorithms to be considered:
Sequential search
Binary search
4.
reached.
Sequential Searching
It isnatural way of searching
The algorithm does not assume that the data is sorted.
Algorithm:
Search list from the beginning until key is found or end of list
Implementation:
int i;
bool found =false;
for(i=0;i<n;i++;)
if (DataElement[i]==key)
{
found=true;
break;
}
return (found);
5.
Example
List index 01 2 3
4 5 6 7
Data C A E F N Q R K
Key=Qafter six comparison the algorithm returns found
Key=Xafter 8 comparison the algorithm returns found false
6.
How it works
Listindex 0 1 2 3 4 5
6 7 8 9
Data 10 14 19 26 27 3 1 33 35 42 44
Key=33after seven comparison the algorithm returns found
7.
Binary Searching
For abinary search to work, it is mandatory for
the target array to be sorted.
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.
8.
Implementation:
int high=n-1, low=0;mid;
boolfound=false;
while (high >low)
{
Middle=(high + low)/2;
if( dataElement[mid]<key)
low=mid+1;
else
high =mid;
}
if
(dataElement[hi
gh]==key)
found =true;
return found;
9.
How it works
Assumethat we need to search the
location of value 31 using binary
search
10.
How it works
First,we shall determine half of the
array by using this formula
mid = (low + high )/ 2
11.
How it works
Nowwe 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.
12.
How it works
Wechange our low to mid + 1 and find
the new mid value again.
low = mid + 1
mid= (low + high )/ 2
Our new mid is 7 now. We compare
the value stored at location 7 with our
target value 31.
13.
How it works
Thevalue stored at location 7 is not a
match, rather it is less than what we
are looking for. So, the value must be
in the lower part from this location.
Hence, we calculate the mid again.
This time it is 5.
14.
How it works
Wecompare the value stored at
location 5 with our target value. We
find that it is a match.
Binary search halves the searchable
items and thus reduces the count of
comparisons to be made to very less
numbers.
15.
Simple (Elementary) Sorting
Algorithms
Forsearching, sorting is important
Take as an example a telephone directory
Efficiency of the algorithm should be assessed
Sorting algorithms commonly consist of two types of
operation: comparisons and data movements
A comparison is simply comparing one value in a list
with another, and a data movement (swapping) is an
assignment.
Sorting algorithms to be considered:
Simple sort, bubble sort, selection sort,
insertion sort
16.
Simple Sort Algorithm
Insimple 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.
proper position
3 1,4,3,2
inits proper place.
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
1 2 1,3,4,2
The second smallest
3 1,2,4,3
element is placed
2 3
19.
Bubble Sort Algorithm
Ita 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
] <
Selection Sort Algorithm
Itis in many ways similar to both simple and bubble
algorithm.
Rather than swapping the neighbors continuously as the
algorithm traverses the sub array to be sorted, as done in
the bubble sort case, this algorithm finds the minimum
element of the sub array and swaps it with the pivot
elements.
For the first position in the sorted list, the whole list is
scanned sequentially.
Insertion Sort Algorithm
Aseach 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.
It behaves in the worst case like the inefficient
bubble sort and selection sort algorithms.
But in many average case it performs better.