0% found this document useful (0 votes)
3 views

Simple Searching and Sorting (Slide) (1)

The document discusses searching algorithms, specifically sequential and binary search, along with their time complexities. It also covers simple sorting algorithms including bubble sort, selection sort, and insertion sort, detailing their implementations and analyses. The average and worst-case time complexities for each algorithm are summarized, highlighting their efficiency in handling data.

Uploaded by

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

Simple Searching and Sorting (Slide) (1)

The document discusses searching algorithms, specifically sequential and binary search, along with their time complexities. It also covers simple sorting algorithms including bubble sort, selection sort, and insertion sort, detailing their implementations and analyses. The average and worst-case time complexities for each algorithm are summarized, highlighting their efficiency in handling data.

Uploaded by

mulugetaliul73
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Adama Science and Technology University

Data structures and Algorithms


(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
[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 (Linear) Search
• 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 && !found;i++)
{
if(arr[i]==key)
found=1;
}
return found;
Sequential Search Analysis
• What are the worst and average case running times for sequential
(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 Complexity of Sequential 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 Time Complexity of 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 Complexity of 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
• Perhaps we can do better than O(n) in the average case?
• Assume that we are give 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 && !found)
{
int mid=(first+last)/2;
if(arr[mid]==key)
{
found=1;
}
else if(key < arr[mid])
{
last=mid-1;
}
else if(key > arr[mid])
{
first=mid+1;
}
}
return found;
Simple Sorting
Algorithms
Bubble sort
Compare each element (except the last one) with its neighbor to the right
(or to the left)
 If they are out of order, swap them
 This puts the largest element at the very end (or puts the smallest element at top)
 The last (or the first) element is now in the correct and final place
Compare each element (except the last two) with its neighbor to the right
(or to the left)
 If they are out of order, swap them
 This puts the second largest element next to last (or puts the second smallest
element next to the first one)
 The last two (or the first two) elements are now in their correct and final places
Compare each element (except the last three) with its neighbor to the
right (or to the left)
 Continue as above until you have no unsorted elements on the left (or on the right)
Example of bubble sort
72854 27548 25478 24578
27854 27548 25478 24578
27854 25748 24578 (done)
27584 25478

27548
Implementation of 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
• 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
72854
array element with itself--this is
harmless, and not worth checking for
27854
Analysis:
The outer loop executes n-1 times
24857 The inner loop executes about n/2
times on average (from n to 2 times)
24587 Work done in the inner loop is constant
(swap two array elements)
24578 Time required is roughly (n-1)*(n/2)
You should recognize this as O(n2)
Implementation of 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
Implementation of 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 && arr[j]<arr[j-1];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)
A comparison of the asymptotic
complexities for three simple
sorting algorithms.
Bubble Selection Insertion
Comparison :
Best case O(n2) O(n2) O(n)
Average case O(n2) O(n2) O(n2)
Worst case O(n2) O(n2) O(n2)
Swap:
Best case 0 0 0
Average case O(n2) O(n) O(n2)
Worst case O(n2) O(n) O(n2)
THE END

38

You might also like