0% found this document useful (0 votes)
37 views27 pages

Ch2-3 Simple Sorting and Searching Algs

This document summarizes three simple sorting algorithms: selection sort, bubble sort, and insertion sort. Selection sort works by selecting the smallest element and swapping it into the first position, then selecting the next smallest and swapping it into the second position, and so on. Bubble sort repeatedly compares adjacent elements and swaps them if they are out of order. Insertion sort inserts each element into its sorted position by shifting other items over as needed. All three algorithms have an time complexity of O(n2).

Uploaded by

anteneh mekonen
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)
37 views27 pages

Ch2-3 Simple Sorting and Searching Algs

This document summarizes three simple sorting algorithms: selection sort, bubble sort, and insertion sort. Selection sort works by selecting the smallest element and swapping it into the first position, then selecting the next smallest and swapping it into the second position, and so on. Bubble sort repeatedly compares adjacent elements and swaps them if they are out of order. Insertion sort inserts each element into its sorted position by shifting other items over as needed. All three algorithms have an time complexity of O(n2).

Uploaded by

anteneh mekonen
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/ 27

CHAPTER-2

Simple Sorting and


Searching Algorithms

By:
Umar H. (Bsc. Software Enginnering)
CHAPTER OVERVIEW
 Simple Sorting algorithms
o Selection sort
o Bubble sort
o Insertion sort

 Simple searching algorithms


o Linear/sequential searching
o Binary searching

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


SORTING ALGORITHMS
 Sorting
o process of reordering a list of items in either increasing or
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.

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


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

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


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

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


SELECTION SORT EXAMPLE …
CONT’D
7 2 8 5 4
 Analysis:
 How many comparisons?
 (n-1)+(n-2)+…+1 = O(n2)
2 7 8 5 4  How many swaps?
 n = O(n)
2 4 8 5 7

2 4 5 8 7

2 4 5 7 8

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


CODE FOR SELECTION SORT …
CONT’D
void selectionSort(int[] a)
{
int i, j, min;
for (i = 0; i < a.length - 1; i++)
{
min = i;
for (j = i + 1; j < a.length; j++)
{
if (a[j] < a[min])
{
min = j;
}
}
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
7
}//end of function
IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021
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

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


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

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


CODE FOR BUBBLE SORT ...CONT’D
void bubbleSort(int[] a)
{
int i, j;
for (i = a.length - 1; i > 0; outer--) //counts down
{
for (j = 0; j < i; j++)
{
if (a[j] > a[j + 1])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
} 10

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


ANALYSIS FOR BUBBLE SORT
...CONT’D
 Let n = a.length = size of the array
 How many comparisons?
 (n-1)+(n-2)+…+1= O(n2)

 How many swaps?


 (n-1)+(n-2)+…+1= O(n2)

11

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


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

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


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

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


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

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


CODE FOR INSERTION SORT ...CONT’D

void insertionSort(int[] array)


{
int j, i;

for (i = 1; i < array.length; i++)


{
int temp = array[i];
j = i;
while (j > 0 && array[j - 1] >= temp)
{
array[j] = array[j - 1];
j--;
}
array[j] = temp;
} 15
}
IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021
ANALYSIS OF INSERTION SORT ...CONT’D

 How many comparisons?


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

16

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


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

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


CHAPTER-3
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

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


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

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


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

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


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

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


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

Saturday, July 31, 2021


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

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


IMPLEMENTATION OF BINARY SEARCHING …CONT’D
int binarySearch(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
}
IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021
ANALYSIS OF GROWTH FUNCTIONS

25

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


THANK YOU

ANY Q?
26

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021


END OF CHAPTER-2

27

IS(2008) ROYAL U.COLLEGE Saturday, July 31, 2021

You might also like