CHAPTER-2
Simple Sorting and
Searching
Algorithms
By: Tigabu Y.
CHAPTER OVERVIEW
Simple Sorting algorithms
o Selection sort
UoG Institute of Technology
o Bubble sort
o Insertion sort
Simple searching algorithms
o Linear/sequential searching
o Binary searching
UoG Institute of Technology 20 October 2019
SORTING ALGORITHMS
Sorting
o process of reordering a list of items in either increasing or
UoG Institute of Technology
decreasing order.
o efficiency of sorting algorithm is measured using
o the number of comparisons and
o the number of data movements made by the algorithms.
o Sorting algorithms are categorized as:
o simple/elementary and
o advanced.
o Simple sorting algorithms, like Selection sort, Bubble sort and
Insertion sort, are only used to sort small-sized list of items.
UoG Institute of Technology 20 October 2019
1. 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
4
UoG Institute of Technology 20 October 2019
SELECTION SORT …CONT’D
i.e. the basic idea is
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.
we repeatedly find the next largest (or smallest)
element in the array and move it to its final
position in the sorted array.
Note: the list/array is divided into two parts:
the sub-list of items already sorted and
the sub-list of items remaining to be sorted
UoG Institute of Technology 20 October 2019
SELECTION SORT EXAMPLE …CONT’D
7 2 8 5 4
Analysis:
The outer loop executes n-1 times
The inner loop executes about n(n-1)/2
2 7 8 5 4
times on average (from n to 2 times)
Work done in the inner loop is constant
2 4 8 5 7 (swap two array elements)
Time required is roughly (n-1)*[n(n-1)/2]
2 4 5 8 7 You should recognize this as O(n2)
i.e.
2 4 5 7 8 How many comparisons?
(n-1)+(n-2)+…+1 = O(n2)
How many swaps?
n = O(n)
6
UoG Institute of Technology 20 October 2019
CODE FOR SELECTION SORT …CONT’D
void selectionSort(int[] a)
{
int outer, inner, min;
for (outer = 0; outer < a.length - 1; outer++)
{
min = outer;
for (inner = outer + 1; inner < a.length; inner++)
{
if (a[inner] < a[min])
{
min = inner;
}
}
int temp = a[outer];
a[outer] = a[min];
a[min] = temp;
} 7
}//end of function
UoG Institute of Technology 20 October 2019
2. BUBBLE SORT
Also called Exchange sort
simplest algorithm to implement and the slowest
algorithm on very large inputs.
Basic Idea:
Loop through array from i=0 to n and swap adjacent
elements if they are out of order.
repeatedly compares adjacent elements of an array.
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 8
UoG Institute of Technology 20 October 2019
BUBBLE SORT EXAMPLE ...CONT’D
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
2 7 5 8 4 2 5 4 7 8
2 7 5 4 8
UoG Institute of Technology 20 October 2019
CODE FOR BUBBLE SORT ...CONT’D
void bubbleSort(int[] a)
{
int outer, inner;
for (outer = a.length - 1; outer > 0; outer--) //counts down
{
for (inner = 0; inner < outer; inner++)
{
if (a[inner] > a[inner + 1])
{
int temp = a[inner];
a[inner] = a[inner + 1];
a[inner + 1] = temp;
}
}
}
}
10
UoG Institute of Technology 20 October 2019
ANALYSIS FOR BUBBLE SORT ...CONT’D
Let n = a.length = size of the array
The outer loop is executed n-1 times
Each time the outer loop is executed, the inner loop is
executed
Inner loop executes n-1 times at first, linearly dropping to
just once
On average, inner loop executes about n(n-1)/2 times for
each execution of the outer loop
In the inner loop, the comparison is always done (constant
time), the swap might be done (also constant time)
Result is (n-1) * [ n(n-1)/2 ] + k, that is, O(n2)
i.e.
How many comparisons?
(n-1)+(n-2)+…+1= O(n2)
How many swaps?
(n-1)+(n-2)+…+1= O(n2)
11
UoG Institute of Technology 20 October 2019
3. INSERTION SORT
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.
Basic Idea:
Find the location for an element and move all others up, and
insert the element.
The approach is the same approach that we use
for sorting a set of cards in our hand.
While playing cards, we pick up a card, start at the
beginning of our hand and find the place to insert the
new card, insert it and move all the others up one
place. 12
UoG Institute of Technology 20 October 2019
INSERTION SORT ...CONT’D
The algorithm 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.
13
UoG Institute of Technology 20 October 2019
INSERTION SORT EXAMPLE ...CONT’D
Sort: 34 8 64 51 32 21
34 8 64 51 32 21
The algorithm sees that 8 is smaller than 34 so it swaps.
8 34 64 51 32 21
51 is smaller than 64, so they swap.
8 34 51 64 32 21
The algorithm sees 32 as another smaller number and
moves it to its appropriate location between 8 and 34.
8 32 34 51 64 21
The algorithm sees 21 as another smaller number and
moves into between 8 and 32.
Final sorted numbers:
8 21 32 34 51 64 14
UoG Institute of Technology 20 October 2019
CODE FOR INSERTION SORT ...CONT’D
void insertionSort(int[] array)
{
int inner, outer;
for (outer = 1; outer < array.length; outer++)
{
int temp = array[outer];
inner = outer;
while (inner > 0 && array[inner - 1] >= temp)
{
array[inner] = array[inner - 1];
inner--;
}
array[inner] = temp;
}
} 15
UoG Institute of Technology 20 October 2019
ANALYSIS OF INSERTION SORT ...CONT’D
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)
i.e.
How many comparisons?
1+2+3+…+(n-1)= O(n2)
How many swaps?
1+2+3+…+(n-1)= O(n2)
16
UoG Institute of Technology 20 October 2019
SUMMARY OF SORTING ALGORITHMS
Bubble Sort, Selection Sort, and Insertion Sort
are all O(n2)
Within O(n2),
Bubble Sort is very slow, and should probably never be
used for anything
Selection Sort is intermediate in speed
Insertion Sort is usually the fastest of the three--in fact,
for small arrays (like 10 or 15 elements), insertion sort
is faster than more complicated sorting algorithms
Selection Sort and Insertion Sort are “good
enough” for small arrays
17
UoG Institute of Technology 20 October 2019
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.
Two simple searching algorithms:
Sequential / Linear Search, and
Binary Search
18
UoG Institute of Technology 20 October 2019
1. LINEAR SEARCHING
Also called sequential searching
Simplest type of searching process
Easy to implement
Can be used on very small data sets
Not practical for searching large collections
The idea is:
Loop through the array starting at the first/last element
until the value of target matches one of the array elements
or until all elements are visited.
If a match is not found, return –1
Analysis:
Time is proportional to the size of input n
time complexity O(n)
19
UoG Institute of Technology 20 October 2019
LINEAR SEARCHING …CONT’D
Algorithm for Sequential/Linear Search
1. Initialize searcharray, key/number to be searched,
length
2. Initialize index=0,
3. Repeat step 4 till index<=length.
4. if searcharray[index]=key
return index
else
increment index by 1.
20
UoG Institute of Technology 20 October 2019
IMPLEMENTATION OF LINEAR SEARCHING …CONT’D
int linearSearch(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;
}
21
UoG Institute of Technology 20 October 2019
2. BINARY SEARCHING
This searching algorithms works only on an ordered
list.
It uses principle of divide and conquer
Though additional cost has to do with keeping list in order,
it is more efficient than linear search
The basic idea is:
1. Locate midpoint of array to search
2. 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
3. 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.
22
UoG Institute of Technology 20 October 2019
BINARY SEARCHING …CONT’D
Analysis:
computational time for this algorithm is proportional to log2n
Therefore the time complexity is O(log n)
Algorithm for Binary Search
1. Initialize an ordered array, searcharray, key, length.
2. Initialize left=0 and right=length
3. Repeat step 4 till left<=right
4. Middle =(left + right) / 2
5. if searcharray[middle]=key
Search is successful
return middle.
else
if key<searcharray[middle]
right=middle - 1
else
left=middle + 1.
23
UoG Institute of Technology 20 October 2019
IMPLEMENTATION OF BINARY SEARCHING …CONT’D
int Binary_Search(int list[ ],int k)
{
int left=0;
int right=n-1;
int found=0;
do{
mid=(left+right)/2;
if(key==list[mid])
found=1;
else{
if(key<list[mid])
right=mid-1;
else
left=mid+1;
}
}while(found==0&&left<right);
if(found==0)
index=-1;
else
index=mid;
return index;
} 24
UoG Institute of Technology 20 October 2019
ANALYSIS OF GROWTH FUNCTIONS
nlogn
25
UoG Institute of Technology
n 20 October 2019
THANK YOU
ANY Q?
26
UoG Institute of Technology 20 October 2019
27
UoG Institute of Technology 20 October 2019