You are on page 1of 36

Chapter III

Simple Sorting and Searching


Algorithms
Introduction
• In this chapter, we will illustrate
– the use of the data structures introduced in the preceding chapter
– how the choice of structure for the underlying data profoundly influences the
algorithms that perform a given task.
• Sorting and searching are a good example to show existence of
different algorithm on different data structure
• Each one having certain advantages and disadvantages that have to
be weighed against each other in the light of the particular
application.
• Moreover, sorting and searching are vital component of several
real world applications.
• In this chapter, we will discuss simple sorting algorithm and simple
searching algorithms

2
1. Simple Searching algorithms

Searching:- is a process of finding an element in a list of items


or determining that the item is not in the list.

• To keep things simple, we shall deal with a list of numbers.

• A search method looks for a key, arrives by parameter.

• By convention, the method will return the index of the


element corresponding to the key or, if unsuccessful, the
value -1.
3
• There are two simple searching algorithms:
a) Sequential Search, and
b) Binary Search

a). Sequential Searching (Linear)


• The most natural way of searching an item.
• Easy to understand and implement.

4
Algorithm:
• In a linear search, we start with top (beginning) of the
list, and compare the element at top with the key.

• If we have a match, the search terminates and the index


number is returned.

• If not, we go on the next element in the list.

• If we reach the end of the list without finding a match,


we return -1.

5
Implementation:
• Assume the size of the list is n.

int LinearSearch(int list[ ], int key)


{
index=-1;
for(int i=0; i<n; i++)
{
if(list[i]==key)
{
index=i;
break;
}
}
return index;
}
6
Complexity Analysis:
• Big-Oh of sequential searching  How many
comparisons are made in the worst case ? n
• O(n).

b). Binary Searching

• Assume sorted data.


• Use Divide and conquer strategy (approach).

7
Algorithm:
I. In a binary search, we look for the key in the middle
of the list. If we get a match, the search is over and
returns its index

II. If the key is greater than the element in the middle of


the list, we make the right (upper) half the list to
search.

III. If the key is smaller, we make the left (lower) half the
list to search.
8
IV. Repeat the above steps (I,II and III) until one
element remains.

• If this element matches return the index of the

element, else return -1 index. (-1 shows that the key


is not in the list).

9
Implementation: Return index;
int BinarySearch(int list[ ], int key)
Complexity Analysis:
{
int found=0,index=-1;
int top=n-1,bottom=0,middle; Example: Find Big-Oh of
do{ Binary search algorithm
middle=(top + bottom)/2; in the worst case analysis.
if(key==list[middle]){
found=1;
Index=middle;
}
else{
if(key<list[middle])
top=middle-1;
else
bottom=middle+1;
}
} 10
}while(found==0 && top>=bottom);
Simple Sorting Algorithm

• Sorting is generally understood to be the process of rearranging a


given set of objects in a specific order.

• Sorting is an almost universally performed, fundamental activity.

• Objects are sorted in telephone books, in tables of contents, in


libraries, in dictionaries and in warehouses

• Even small children are taught to put their things "in order", and
they are confronted with some sort of sorting long before they
learn anything about arithmetic.

11
Simple Sorting Algorithm
• One of the most important purpose of sorting is to facilitate later
search from the sorted set.
• Hence, sorting is a relevant and essential activity, particularly in
data processing.

• Moreover, sorting is an ideal subject to demonstrate a great


diversity of algorithms, all having the same purpose, many of
them being optimal in some sense, and most of them having
advantages over others.

• It is therefore an ideal subject to demonstrate the necessity of


performance analysis of algorithms.

12
Simple Sorting Algorithms

• Simple sorting algorithms include:


i. Simple sorting
ii. Bubble Sorting
iii. Selection Sorting
iv. Insertion Sorting

13
I. Simple sorting
Algorithm:
• In simple sort algorithm the first element is
compared with the second, third and all
subsequent elements.
• If any one of the other elements is less than
the current first element then the first
element is swapped with that element.
• Eventually, after the last element of the list is
considered and swapped, then the first
element has the smallest element in the list.
• The above steps are repeated with the
second, third and all subsequent elements.
14
`
0 1 2
Example: n-1
--------
i
j
4 2 3 1
i=0 j=1 2 4 3 1
j=2 2 4 3 1
j=3 1 4 3 2
i=1 j=2 1 3 4 2
j=3 1 2 4 3
i=2 j=3 1 2 3 4
15
`
Analysis: O(?)
1st pass----- (n-1) comparisons
2nd pass---- (n-2) comparisons
|
|
|
(n-1)th pass--- 1 comparison

T(n)=1+2+3+4+-------+(n-2)+(n-1)
= (n*(n-1))/2
=n2/2-n/2
=O(n2)

16
Complexity Analysis:

• Analysis involves number of comparisons


and swaps.

• How many comparisons?


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

17
II. Selection sort
Selection sort Algorithm
• It is the most natural and easiest sorting algorithm
• 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
18
Elementary Sorting Algorithm
Selection sort algorithm

SelectionSort (A, n)
for j  0 to n-1
min  A[j]
index  j
//find the index of the jth min value
for i j+1 to n
if(A[i] < min)
index  i
min A[i]
//swap A[j]and A[index]
temp  min
A[index]  A[j]
A[j]  temp

19
Elementary Sorting Algorithm
Selection sort example

7 2 8 5 4
• Selection Sort might swap an array
element with itself--this is harmless,
2 7 8 5 4
and not worth checking for

2 4 8 5 7

2 4 5 8 7

2 4 5 7 8

20
Elementary Sorting Algorithm
SelectionSort (A, n)
for j  0 to n-1
min  A[j]
index  j
//find the index of the jth min value
for i j+1 to n-1
if(A[i] < min)
index  i
min A[i]
//swap A[j]and A[index]
temp  min
A[index]  A[j]
A[j]  temp

Note:
Both worst and best case has the same growth rate

21
III. Insertion sort
• Given an array of length n,
– Take the element at index j and call is key
– Let the element at index i is greater than key and the element
at index i-1 is less than the key
– Push all the element from index j-1 to i one step to the right
– Insert the key into index I

– Repeat this for all j from index 1 to n

22
Elementary Sorting Algorithm
Insertion sort algorithm

InsertionSort (A, n)
for j  1 to n-1
key A[j]
//Insert key into the list A[0…j-1] so that A[0..j] becomes sorted
i  j-1
while (i >= 0 and A[i] > key)// i is valid index and A[i] > the key
A[i+1]  A[i]
i  i-1
A[i+1]  key

23
Elementary Sorting Algorithm
One step of insertion sort
sorted next to be inserted

3 4 7 12 14 14 20 21 33 38 10 55 9 23 28 16
temp
less than 10 10

3 4 7 10 12 14 20
12 14 14 21 21 38
20 33 33 10
38 55 9 23 28 16

sorted

24
Elementary Sorting Algorithm
Insertion sort algorithm analysis (worst case)
• In this case the inner loop will iterate j times for every j.
• This is the case where the data is sorted in descending order

InsertionSort (A, n)
for j  1 to n-1
key A[j]
i  j-1
while (i >= 0 and A[i] > key)
A[i+1]  A[i]
i  i-1
A[i+1]  key

25
Elementary Sorting Algorithm
Insertion sort algorithm analysis (best case)
• In this case the inner loop will iterate one times for every j.
• This is the case where the data is sorted in ascending order

InsertionSort (A, n)
for j  1 to n-1
key A[j]
i  j-1
while (i >= 0 and A[i] > key)
A[i+1]  A[i]
i  i-1
A[i+1]  key

26
IV. Bubble sort
• 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

27
Elementary Sorting Algorithm
3. Bubble sort algorithm

BubbleSort (A, n)
for j  0 to n-2
for i 1 to n -1 -j
if( A[i-1] > A[i])
//Swap A[i] and A[i-1]
temp  A[i-1]
A[i-1]A[i]
A[i]  temp

28
Elementary Sorting Algorithm

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

29
Elementary Sorting Algorithm
Bubble sort algorithm analysis

BubbleSort (A, n)
for j  0 to n-2
for i 1 to n -1 -j
if( A[i-1] > A[i])
temp  A[i-1]
A[i-1]A[i]
A[i]  temp

• Note: both worst and best case have the


same complexity
30
THE END
Implementation:
Void SimpleSort(int list[])
{
for(int i=0; i<=n-2;i++)
for(int j=i+1; j<=n-1; j++)
if(list[i] > list[j])
{
int temp;
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
36

You might also like