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

Lect 4 Sorting and Searching Algorithm

Basic description of sorting and searching algorithm

Uploaded by

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

Lect 4 Sorting and Searching Algorithm

Basic description of sorting and searching algorithm

Uploaded by

Yohans Brhanu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Searching Algorithms

• 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
Simple Sorting and • There are two simple searching algorithms:

Searching Algorithms – Linear (Sequential ) Search, and


– Binary Search

Fitsum Admasu
Department of Computer Science
Addis Ababa University

Linear Search (Sequential Search) Linear Search (Sequential Search)

• Main Idea: • Example Implementation:


int Linear_Search(int list[], int key)
– Loop through the array starting at the first element until the {
value of target matches one of the array elements. If a int index=0;
match is not found, return –1. int found=0;
do{
if(key==list[index])
found=1;
else
index++;
}while(found==0 & index<n);
if (found==0)
index=-1;
return index;
}

Linear Search (Sequential Search) Binary Search


• Example Implementation: • Assumes that input list is already sorted (ordered).
int Linear_Search(int list[], int key)
{
• Main idea:
int index=0; 1. Locate midpoint of array to search
int found=0; 2. Determine if target is in lower half or upper half of an array.
do{ Time complexity O(n) • If in lower half, make this half the array to search
if(key==list[index]) Unsuccessful search --- n times • If in the upper half, make this half the array to search
Successful search (worst) --- n times
found=1; Successful search (average) --- n/2 times 3. Loop back to step 1 until the size of the array to search is one, and this
else element does not match, in which case return –1.
index++; • The computational time for this algorithm is proportional to
}while(found==0 & index<n);
log2 n
if (found==0)
index=-1; • Therefore, the time complexity is O(log n)
return index;
}

1
Binary Search Sorting Algorithms
Example Implementation: • Sorting is one of the most important operations performed by
int Binary_Search(int list[],int k) { computers.
int left = 0; else • Sorting is a process of reordering a list of items in either
int right= n - 1; left = mid + 1; increasing or decreasing order.
int found = 0; }
• The following are simple sorting algorithms used to sort small-
do{ }
sized lists.
mid = (left + right) / 2; while(found = = 0&& left <= right);
– Insertion Sort
if(key == list[mid])
– Selection Sort
found=1; if(found == 0)
– Bubble Sort
else{ index = -1;
if(key < list[mid]) else
right = mid - 1; index = mid;
return index;
}

Insertion Sort Insertion Sort


• The insertion sort works just like its name suggests - it inserts
• The simplest implementation of this requires two list structures
each item into its proper place in the final list.
- the source list and the list into which sorted items are
• Basic Idea: inserted.
– Find the location for an element and move all others up, and insert the element. – To save memory, most implementations use an in-place sort that works
• The process involved in insertion sort is as follows: by moving the current item past the already sorted items and repeatedly
– The left most value can be said to be sorted relative to itself. Thus, we don’t swapping it with the preceding item until it is in place.
need to do anything.
– Check to see if the second value is smaller than the first one. If it is, swap these
two values. The first two values are now relatively sorted.
– Next, we need to insert the third value in to the relatively sorted portion so that
after insertion, the portion will still be relatively sorted.
– Remove the third value first. Slide the second value to make room for insertion.
Insert the value in the appropriate position. Now the first three are relatively
sorted.
– Do the same for the remaining items in the list.

Insertion Sort Insertion Sort


Implementation Implementation
void insertion_sort(int list[]){ void insertion_sort(int list[]){
int temp; int temp;
Analysis
for(int i = 1; i < n; i++){ for(int i = 1; i < n; i++){
How many comparisons?
for(int j = i; j > 0 && temp < list[j - 1]; j--) for(int j = i; j > 0 && temp < list[j - 1]; j--) 1 + 2 + 3 +…+ (n-1) = O(n2)
{ //work backwards through the array { //work backwards through the array How many swaps?
finding where temp should go 1 + 2 + 3 +…+ (n-1) = O(n2)
// finding where temp should go
How much space?
list[j] = list[j - 1]; list[j] = list[j - 1]; In-place algorithm
list[j - 1] = temp; list[j - 1] = temp;
}//end of inner loop }//end of inner loop
}//end of outer loop }//end of outer loop
}//end of insertion_sort }//end of insertion_sort
temp = list[i]; temp = list[i];

2
Selection Sort Selection Sort
Implementation:
• Basic Idea: void selection_sort(int list[])
– Loop through the array from i= 0 to n - 1. {
– Select the smallest element in the array from i to n int i, j, smallest;
– Swap this value with value at position i. for(i = 0; i < n; i++){
smallest = i;
for(j = i + 1; j < n; j++){
if(list[j] < list[smallest])
smallest = j;
}//end of inner loop
temp = list[smallest];
list[smallest] = list[i];
list[i] = temp;
} //end of outer loop
}//end of selection_sort

Selection Sort Bubble Sort


Implementation:
void selection_sort(int list[]) • Bubble sort is the simplest algorithm to implement and the
{ slowest algorithm on very large inputs.
int i, j, smallest; • Basic Idea:
for(i = 0; i < n; i++){ – Loop through array from i = 0 to n and swap adjacent elements if they
Analysis
smallest = i; How many comparisons? are out of order.
for(j = i + 1; j < n; j++){ (n-1) + (n-2) +…+ 1 = O(n2) – Continue with the above step till no more swaps are possible.
if(list[j] < list[smallest]) How many swaps? n = O(n)
smallest = j; How much space?
In-place algorithm
}//end of inner loop
temp = list[smallest];
list[smallest] = list[i];
list[i] = temp;
} //end of outer loop
}//end of selection_sort

Bubble Sort General Comments


Implementation:
void bubble_sort(list[]) • Each of these algorithms requires n-1 passes: each pass
{ places one item in its correct place. The ith pass makes either
int i, j, temp; Analysis of Bubble Sort
i or n - i comparisons and moves. So:
for(i = 0; i < n; i++){ How many comparisons?
for(j = n-1; j > i; j--){ (n-1) + (n-2) +…+ 1 = O(n2)
How many swaps?
if(list[j] < list[j-1]){ (n-1) + (n-2) +…+ 1 = O(n2)
temp = list[j]; How much Space?
list[j] = list[j-1]; In-place algorithm.

list[j-1] = temp; • Therefore, these algorithms have O(n2) time complexities.


}//swap adjacent elements – Thus these algorithms are only suitable for small problems where
their simple code makes them faster than the more complex code of
}//end of inner loop
the O(n logn) algorithm.
}//end of outer loop
}//end of bubble_sort

3
Basic characteristics of sorting algorithms: Basic characteristics of sorting algorithms:

• Insertions sort • Bubble Sort


– Reduce the number of comparisons in each pass. Because it stops – Involves sequential data movement.
doing so the moment the exact location is found. – Continuous working even after the elements have been sorted i.e.,
– Thus, it runs in a linear time for an almost sorted input data. many comparisons
– Involves frequent data movement. – Although it is the least efficient algorithm it may be fast on an input
– Appropriate for input whose size is small and almost sorted. data that is small in size and almost sorted.
• Selection sorting – Easy to develop and implement
– Least movement data (swap)
– Continuous working even after the list is sorted i.e. many comparisons.
– Appropriate for applications whose comparisons are cheap but swaps are
expensive.

Measures of Times Comparisons of sorting algorithms

• Average Case (Tavg): The amount of time the algorithm takes


on an "average" set of inputs.
• Worst Case (Tworst): The amount of time the algorithm takes Algorithms Best Case Average Case Worst Case
on the worst possible set of inputs. Insertion O(n) 2
O(n )
2
O(n )
2 2 2
– “Big-Oh” estimate. Selection O(n ) O(n ) O(n )
2 2 2
Bubble
• Best Case (Tbest): The amount of time the algorithm takes on O(n ) O(n ) O(n )
the smallest possible set of inputs.

You might also like