0% found this document useful (0 votes)
39 views41 pages

Adama Science and Technology University: Data Structures and Algorithm

The document discusses different searching and sorting algorithms. It begins by defining the problem of searching through a list of records to find one with a particular key. It then describes and analyzes the running times of sequential search and binary search algorithms. For sorting, it outlines bubble sort and selection sort, providing examples, pseudocode, and analyzing their time complexities as O(n^2).

Uploaded by

Temesgen workiye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views41 pages

Adama Science and Technology University: Data Structures and Algorithm

The document discusses different searching and sorting algorithms. It begins by defining the problem of searching through a list of records to find one with a particular key. It then describes and analyzes the running times of sequential search and binary search algorithms. For sorting, it outlines bubble sort and selection sort, providing examples, pseudocode, and analyzing their time complexities as O(n^2).

Uploaded by

Temesgen workiye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Adama Science and Technology University

Data structures and Algorithm


(week 3-4)
Searching
Problem: Search
• We are given a list of records.
• Each record has an associated key.
• Give efficient algorithm for searching for a record containing a
particular key.
Search
searching is looking for some particular data element from data structure.

[0] [1] [2] [3] [4] [ 700 ]


Number 281942902 Number 233667136 Number 580625685
Number 701466868 Number 506643548 Number 155778322

Each record in list has an associated key. Number 580625685


In this example, the keys are ID numbers.

Given a particular key, how can we


efficiently retrieve the record from the list?
Sequential Search
• In this, the list or array is traversed sequentially and every element is
checked.
• Step through array of records, one at a time.
• Look for record with matching key.
• Search stops when
• record with matching key is found
• or when search has examined all records without success.
Pseudocode for Seq. Search

int arr[]={89,2,67,37,72,17,4};
int n=sizeof(arr)/sizeof(arr[0]);
int found=0;
int key=37;
for(int i=0;i<n;i++)
{
if(arr[i]==key)
found=1;
}
return found;
Serial Search Analysis
• What are the worst and average case running times for serial search?
• We must determine the O-notation for the number of operations
required in search.
• Number of operations depends on n, the number of entries in the list.
Worst Case Time for Serial Search
• For an array of n elements, the worst case time
for serial search requires n array accesses: O(n).
• Consider cases where we must loop over all n
records:
• desired record appears in the last position of the array
• desired record does not appear in the array at all
Average Case for Seq. Search
Assumptions:
1. All keys are equally likely in a search
2. We always search for a key that is in the array
Example:
• We have an array of 10 records.
• If search for the first record, then it requires 1 array
access; if the second, then 2 array accesses. etc.
The average of all these searches is:
(1+2+3+4+5+6+7+8+9+10)/10 = 5.5
Average Case Time for Seq. Search

Generalize for array size n.

Expression for average-case running time:

(1+2+…+n)/n = n(n+1)/2n = (n+1)/2

Therefore, average case time complexity for serial search is O(n).


Binary Search
• These algorithm are specifically designed for searching in sorted data
element.
• It assumes the data is sorted and it also uses divide and conquer strategy.
• Binary search algorithm worst case O(log n) and best case O(1) when the
central index would directly match the desired value.
• Assume that we are given an array of records that is sorted. For instance:
• an array of records with integer keys sorted from smallest to largest (e.g., ID
numbers), or
• an array of records with string keys sorted in alphabetical order (e.g., names).
Binary Search Pseudocode

if(size == 0)
found = false;
else {
middle = index of approximate midpoint of array segment;
if(target == a[middle])
target has been found!
else if(target < a[middle])
search for target in area before midpoint;
else
search for target in area after midpoint;
}

Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is 7 = midpoint key? NO.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is 7 < midpoint key? YES.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the target in the area before midpoint.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target = key of midpoint? NO.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target < key of midpoint? NO.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target > key of midpoint? YES.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the target in the area after midpoint.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint.


Is target = midpoint key? YES.
Binary Search Implementation
int arr[] = { 1, 3, 4, 6, 7, 8, 10, 13, 14, 18, 19, 21, 24, 37, 40, 45, 71 };
int n=sizeof(arr)/sizeof(arr[0]);
int first=0;
int last=n-1;
int key=32;
int found=0;
while(first<=last)
{
int mid=(first+last)/2;
if(arr[mid]==key)
{
found=1;
break;
}
else if(key < arr[mid])
{
last=mid-1;
}
else if(key > arr[mid])
{
first=mid+1;
}
}
return found;
Simple Sorting Algorithms
Bubble sort
This sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements
if they are in wrong order.
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 next to 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
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
Code for bubble sort
int arr[]={89,2,67,37,72,17,4};
int n=sizeof(arr)/sizeof(arr[0]);
for(int i=n-1;i>=0;i--)
{
for(int j=0;j<=i;j++)
{
if(arr[j+1]<arr[j])
{
int temp =arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
}
}
}
Analysis of bubble sort

• 
Selection sort
This sort is a sorting algorithm, specifically an in-place comparison sort
and it divides the input list in two parts sorted and unsorted.
• Given an array of length n,
• Search elements 0 through n-1 and select the smallest
• Swap it with the element in location 0
• Search elements 1 through n-1 and select the smallest
• Swap it with the element in location 1
• Search elements 2 through n-1 and select the smallest
• Swap it with the element in location 2
• Search elements 3 through n-1 and select the smallest
• Swap it with the element in location 3
• Continue in this fashion until there’s nothing left to search
Example and analysis of selection sort
The selection sort might swap an
7 2 8 5 4
array element with itself--this is
harmless, and not worth checking for
2 7 8 5 4
Analysis:
The outer loop executes n-1 times
2 4 8 5 7
The inner loop executes about n/2
times on average (from n to 2 times)
2 4 5 8 7 Work done in the inner loop is constant
(swap two array elements)
2 4 5 7 8 Time required is roughly (n-1)*(n/2)
You should recognize this as O(n2)
Code for selection sort
int arr[]={89,2,67,37,72,17,4};
int n=sizeof(arr)/sizeof(arr[0]);
for(int i=0;i<n;i++)
{
int small=i;
for(int j=i+1;j<n;j++)
{
if(arr[j]<arr[small])
{
small=j;
}
}
int temp =arr[i];
arr[i]=arr[small];
arr[small]=temp;
}
Insertion sort
• The outer loop of insertion sort is:
for (outer = 1; outer < n; outer++) {...}
• The invariant is that all the elements to the left of outer
are sorted with respect to one another
• For all i < outer, j < outer, if i < j then a[i] <= a[j]
• This does not mean they are all in their final correct place; the
remaining array elements may need to be inserted
• When we increase outer, a[outer-1] becomes to its left; we must
keep the invariant true by inserting a[outer-1] into its proper
place
• This means:
• Finding the element’s proper place
• Making room for the inserted element (by shifting over other
elements)
• Inserting the element
Example insertion sort
Code for Insertion Sort
//Insertion sort
int arr[]={89,2,67,37,72,17,4};
int n=sizeof(arr)/sizeof(arr[0]);
for(int i=1;i<n;i++)
{
for(int j=i;j>0;j--)
{
if(arr[j]<arr[j-1])
{
int temp =arr[j];
arr[j]=arr[j-1];
arr[j-1]=temp;
}
}
}
Analysis of insertion sort
We run once through the outer loop, inserting each of n elements;
this is a factor of n
On average, there are n/2 elements already sorted
The inner loop looks at (and moves) half of these
This gives a second factor of n/4
Hence, the time required for an insertion sort of an array of n
elements is proportional to n2/4
Discarding constants, we find that insertion sort is O(n2)
THE END

41

You might also like