KEMBAR78
Chapter 3 - Data Structure and Algorithms.pptx
Chapter Three: 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
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
Simple searching Algorithm
Searching is a process of finding an element from a
collection of elements.
Searching algorithms to be considered:
Sequential search
Binary search
reached.
Sequential Searching
It is natural 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);
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
How it works
List index 0 1 2 3 4 5
6 7 8 9
Data 10 14 19 26 27 3 1 33 35 42 44
Key=33after seven comparison the algorithm returns found
Binary Searching
For a binary 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.
Implementation:
int high=n-1, low=0;mid;
bool found=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;
How it works
Assume that we need to search the
location of value 31 using binary
search
How it works
First, we shall determine half of the
array by using this formula
mid = (low + high )/ 2
How it works
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.
How it works
We change 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.
How it works
The value 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.
How it works
We compare 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.
Simple (Elementary) Sorting
Algorithms
For searching, 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
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-
2;i++)
for(j=i+1;
j<=n-1;j+
+)
if(dataElement[i]>dataElement[j])
Swap(dataElement[i],dtataElement[j]);
proper position
3 1,4,3,2
in its 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
Bubble Sort Algorithm
It 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
] <
Example data element= 5,2,3,8,1
Selection Sort Algorithm
It is 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.
Implementation
for (i=0; i<=n-2;i++)
{
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]);
}
Example
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.
It behaves in the worst case like the inefficient
bubble sort and selection sort algorithms.
But in many average case it performs better.
Implementation
for (i=1;i<=n-1;i++)
{
inserted =false;
j=i;
while ((j>=1) && (inserted == false))
{
If (dataElement[j]<dataElement[j-1])
Swap(dataElement[j],dataElement[j-1]);
else
inserted=true;
j--
}
}
end

Chapter 3 - Data Structure and Algorithms.pptx

  • 1.
    Chapter Three: ElementarySearching and Sorting Algorithms This Chapter Covers: Sequential search Binary search Simple sort Bubble sort Selection sort Insertion sort
  • 2.
    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=Qafter six comparison the algorithm returns found Key=Xafter 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=33after 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.
  • 17.
    Implementation: void swap( dataTypex, dataType y) { dataType temp; temp=x; x=y; y=temp; } for (i=0;i<=n- 2;i++) for(j=i+1; j<=n-1;j+ +) if(dataElement[i]>dataElement[j]) Swap(dataElement[i],dtataElement[j]);
  • 18.
    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 ] <
  • 20.
  • 21.
    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.
  • 22.
    Implementation for (i=0; i<=n-2;i++) { 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]); }
  • 23.
  • 24.
    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.
  • 25.
    Implementation for (i=1;i<=n-1;i++) { inserted =false; j=i; while((j>=1) && (inserted == false)) { If (dataElement[j]<dataElement[j-1]) Swap(dataElement[j],dataElement[j-1]); else inserted=true; j-- } }
  • 26.