Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
15 views26 pages

Chapter 2

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)
15 views26 pages

Chapter 2

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/ 26

Chapter Two

Simple Sorting and Searching Algorithms

Course Title:- Data structure and


Algorithms
Instructor:- Feyisa K.
Email:- [email protected]
Outlines
Searching
Linear/Sequential Searching
Binary Searching
Sorting
Bubble Sort
Selection Sort
Insertion Sort

2
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

3
Simple searching Algorithm
 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.
 There are two simple searching algorithms:
 Sequential Search, and
 Binary Search

4
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 reached.
 Loop through the array starting at the first
element until the value of target matches one
of the array elements.
 If a match is not found, return –1.

5
Cont…
 Example Implementation:
int Linear_Search(int list[], int key)
{
int index=0;
int found=0;
do{
if(key==list[index])
found=1;
else
index++;
}while(found==0 && index<n);
if(found==0)
index=-1;
return index;
}
6
Cont…
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
What is the Big O of sequential searching algorithm?
 For searching algorithm the dominant factor is
comparison of keys
 How many comparisons are made in sequential
searching?
 The maximum number of comparison is n, i.e. O (n).

7
Binary Search
 This searching algorithms works only on an
ordered list.
 The basic idea is:
 Locate midpoint of array to search
 Determine if target is in lower half or upper half
of an array.
 If in lower half, make this half the array to
search
 If in the upper half, make this half the array to
search
 Loop back to step 1 until the size of the array to
search is one, and this element does not match,
in which case return –1.
8
Cont…
 Find 22 in the following array.

9
Cont…
Example Implementation:
int Binary_Search(int list[], int key)
{
int left = 0;
int right = n-1;
int found = 0;
do{
mid = (left+right)/2;
if(key == list[mid])
found = 1;
elseif(key < list[mid])
right = mid-1;
elseif(key > list[mid])
left = mid+1;
}while(found == 0 && left <= right);
if(found == 0)
index = -1;
else
index = mid;
return index;
10 }
How many comparisons are made in
binary search algorithm?
 n Max. Number of
comparison
1 1
2 2
4 3
8 4
16 5
32 16
The computational time for this algorithm
is proportional to log2 n. Therefore, for n
number of data items, logn+1 number of
comparison is needed; so, it is O(logn).

11
Sorting Algorithms
 Sorting is one of the most important operations
performed by computers.
 Sorting is a process of reordering a list of items in
either increasing or decreasing order. 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:
 bubble sort, selection sort, insertion sort
12
Bubble Sort
 The simplest sorting algorithm is bubble sort.
 The bubble sort works by iterating down an
array to be sorted from the first element to the
last, comparing each pair of elements and
switching their positions if necessary.
 This process is repeated as many times as
necessary, until the array is sorted.
 Since the worst case scenario is that the array
is in reverse order, and that the first element in
sorted array is the last element in the starting
array, the most exchanges that will be
necessary is equal to the length of the array.
13
Cont…
Basic Idea:
Loop through array from i=0 to n and swap
adjacent elements if they are out of order.
Iteration Immediate array
original 34 8 64 51 32 21
i=0 34 8 64 51 21 32
34 8 64 21 51 32
34 8 21 64 51 32
34 8 21 64 51 32
8 34 21 64 51 32
i=1 8 34 21 64 32 51
… …
14
Cont…
 Implementation:
void bubble_sort(list[])
{
int i, j, temp;
for(i = 0; i < n; i++)
{
for(j = n-1; j > i; j--)
{
if(list[j] < list[j-1])
{
temp = list[j];
list[j] = list[j-1];
list[j-1] = temp;
}
}
}
}
15
Algorithm Analysis
Passes number of comparison number of
swaps
1 n-1 n-1
2 n-2 n-2
. . .
. . .
. . .
n-1 1 1
total n(n-1)/2 n(n-1)/2
 How many comparisons?
 (n-1)+(n-2)+…+1= O(n2)
 How many swaps?
 (n-1)+(n-2)+…+1= O(n2)
 Space? In-place algorithm.
16
Selection Sort
 Selection sort is to repetitively pick up the smallest
element and put it into the right position:
Basic Idea
 Find the smallest element, and put it to the first position.
 Find the next smallest element, and put it to the second
position.
 Repeat until all elements are in the right positions.
 A loop through the array finds the smallest element easily.
After the smallest element is put in the first position, it is
fixed and then we can deal with the rest of the array.
 The following implementation uses a nested loop to
repetitively pick up the smallest element and swap it to its
final position.
 Loop through the array from i=0 to n-1.
 Select the smallest element in the array from i to n
 Swap this value with value at position i.
17
Cont…
 Given array abc = 34 8 64 51 32 21
No of passes the array positions moved

Original 34 8 64 51 32 21 -

Pass=1 8 34 64 51 32 21 1

Pass=2 8 21 64 51 32 34 1

Pass=3 8 21 32 51 64 34 1

Pass=4 8 21 32 34 64 51 1

Pass=5 8 21 32 34 51 64 1

18
Cont…

Implementation:
void selection_sort(int list[])
{
int i, j, smallest;
for(i = 0; i < n; i++)
{
smallest = i;
for(j = i+1; j < n; j++)
{
if(list[j] < list[smallest])
smallest = j;
}
temp = list[smallest];
list[smallest] = list[i];
list[i] = temp;
}
}
19
Algorithm Analysis
Pass number of comparison number of swaps
1 n-1 1
2 n-2 1
. . .
. . .
. . .
n-1 1 1
total n(n-1)/2 n-1
 How many comparisons?

(n-1)+(n-2)+…+1= O(n2)
 How many swaps?

n=O(n)
 How much space?

In-place algorithm
20
Insertion Sort
 The insertion sort works just like its name suggests - it
inserts each item into its proper place in the final list.
 The simplest implementation of this requires two list
structures - the source list and the list into which sorted
items are inserted.
 To save memory, most implementations use an in-place
sort that works by moving the current item past the
already sorted items and repeatedly swapping it with the
preceding item until it is in place.
 It's the most instinctive type of sorting algorithm.
 The approach is the same approach that you use for
sorting a set of cards in your hand.
 While playing cards, you pick up a card, start at the
beginning of your hand and find the place to insert the
new card, insert it and move all the others up one place.
21
Cont…
Basic Idea:
 Find the location for an element and move all others up,
and insert the element.
 The process involved in insertion sort is as follows:
1.The left most value can be said to be sorted relative to itself.
Thus, we don’t need to do anything.
2.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.
3.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.
4.Remove the third value first. Slide the second value to make
room for insertion. Insert the value in the appropriate position.
5.Now the first three are relatively sorted.
6. Do the same for the remaining items in the list.

22
Cont…
 Given array abc = 34 8 64 51 32 21

No of Immediate array positions


passes moved
original 34 8 64 51 32 21 -
after p = 2 8 34 64 51 32 21 1
after p = 3 8 34 64 51 32 21 0
after p = 4 8 34 51 64 32 21 1
after p = 5 8 32 34 51 64 21 3
8 34 51 32 64 21
8 34 32 51 64 21
8 32 34 51 64 21
after p = 6 8 21 32 34 51 64 4

23
Cont…
Implementation
void insertion_sort(int list[])
{
int temp;
for(int i = 1; i < n; i++)
{
temp = list[i];
for(int j = i; j > 0 && temp < list[j-1]; j--) //work backwards
to find where temp should go
{
list[j] = list[j-1];
list[j-1] = temp;
}
}
}
24
Algorithm Analysis
Pass number of comparison number of
swaps
1 1 1
2 2 2
. . .
. . .
. . .
n-1 n-1 n-1
Total n(n-1)/2 n(n-1)/2
 How many comparisons?

1+2+3+…+(n-1)= O(n2)
 How many swaps?

1+2+3+…+(n-1)= O(n2)
 How much space?

In-place algorithm
25
Any Question

26

You might also like