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

Algorithm ASSIGNMENT 1 Group 2

The document describes an analysis and design of algorithms course taken by 12 students at Makerere University. It discusses sorting algorithms including selection sort, bubble sort, and insertion sort. Selection sort finds the minimum element and moves it to the sorted portion of the list. Bubble sort compares adjacent elements and swaps them if out of order. Insertion sort inserts elements into the sorted portion one by one. As an example, insertion sort is applied to sort the letters in "ALGORITHM" alphabetically.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

Algorithm ASSIGNMENT 1 Group 2

The document describes an analysis and design of algorithms course taken by 12 students at Makerere University. It discusses sorting algorithms including selection sort, bubble sort, and insertion sort. Selection sort finds the minimum element and moves it to the sorted portion of the list. Bubble sort compares adjacent elements and swaps them if out of order. Insertion sort inserts elements into the sorted portion one by one. As an example, insertion sort is applied to sort the letters in "ALGORITHM" alphabetically.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

MAKERERE

UNIVERSITY

COURSE: BACHELOR OF SCIENCE IN COMPUTER ENGINEERING


DEPARTMENT: ELECTRICAL AND COMPUTER ENGINEERING
COURSE UNIT: ANALYSIS AND DESIGN OF ALGORITHMS
CODE: CMP 2202

GROUP MEMBERS.
NAME
1 TWINOMUHWEZI KABANGA BILL
2 ATUHAIRE JUDE INNOCENT
3 NIMANYA FLAVIA
4 JJAGWE PAULUS
5 MEEK ANNALEAH
6 NASASIRA JUSTUS
7 MASENGERE SOLOMON
8 LUBEGA DAN
9 NAMUDDU CAROLINE
10 NAKYAZZE JULIET
11 SSEJJEMBA DANIEL
12 ASAMO ELIZABETH

REGISTRATION NUMBER
14/U/22261
14/U/5554/EVE
14/U/13209
14/U/6568/PS
14/U/9210/PS
14/U/12866/PS
13/U/7841/EVE
14/U/8647/PS
14/U/12191/PS
14/U/1867
14/U/14781/PS
14/U/22517/EVE

QUESTION 2
1. Sorting
1. Selection sorting
2. Bubble sort
3. Insertion sort
4. Apply insertion sort algorithm for the set A L G O R I T H M to sort in in ascending order

Sorting
This is arranging items in order, sorting is the most fundamental task in computation that enables
efficient searching algorithms such as binary search. Selection, insertion and Bubble sort are
easily understandable and also similar to each other. The basic ideas are as below;
Selection sort repeatedly pick the smallest element and append to the result.
Insertion sort repeatedly add new element to the sorted result.
Bubble sort repeatedly compare neighbor pairs and swap if necessary.
Some Definitions

Internal Sort
The data to be sorted is all stored in the computers main memory.
External Sort
Some of the data to be sorted might be stored in some external, slower, device.
In Place Sort
The amount of extra space required to sort the data is constant with the input
size.

SELECTION SORT
In selection sort the list is divided into two sub-lists sorted and unsorted. These two lists are
divided by an imaginary wall. We find a smallest element from unsorted sub-list and swap it to
the beginning. And the wall moves one element ahead, as the sorted list is increased and unsorted
list is decreased.
Assume that we have a list of n elements. By applying selection sort, the first element is
compared with all remaining (n-1) elements. The smallest element is placed at the first location.
Again, the second element is compared with remaining (n-1) elements. At the time of
comparison, the smaller element is swapped with larger element. Similarly, entire array is
checked for smallest element and then swapping is done accordingly. Here we need n-1 passes or
iterations to completely rearrange the data.
Idea:
Find the smallest element in the array
Exchange it with the element in the first position
Find the second smallest element and exchange it with the element in the
second position
Continue until the array is sorted
Disadvantage:
Running time depends only slightly on the amount of order in the file
Algorithm: Selection Sort (A [ ], N)
Step 1:
Step 2:
Step 3:

Repeat For K = 0 to N 2
Set POS = K
Repeat for J = K + 1 to N 1
If A [J] < A [POS]
Set POS = J
End For

Begin
Begin

Step 5:
Step 6:

Swap A [K] with A [POS]


End For
Exit

Example: - A list of unsorted elements are: 23 78 45 8 32 56

A list of sorted elements after: 8 23 32 45 56 78


BUBBLE SORT
In bubble sort method the list is divided into two sub-lists sorted and unsorted. The smallest
element is bubbled from unsorted sub-list. After moving the smallest element the imaginary wall
moves one element ahead. The bubble sort was originally written to bubble up the highest
element in the list. But there is no difference whether highest / lowest element is bubbled.
This method is easy to understand but time consuming.
In this type, two successive elements are compared and swapping is done. Thus,
step-by-step entire array elements are checked. Given a list of n elements the bubble sort
requires up to n-1 passes to sort the data.

Algorithm for Bubble Sort:


Bubble Sort (A [ ], N)
Step 1: Repeat For P = 1 to N 1
Begin
Step 2: Repeat For J = 1 to N P
Begin
Step 3: If (A [J] < A [J 1])
Swap (A [J], A [J 1])
End For
End For
Step 4: Exit

Example:
Ex:- A list of unsorted elements are: 10 47 12 54 19 23

A list of sorted elements now : 54 47 23 19 12 10

Complexity
Clearly for an array of size n, the outside loop repeats n-1 times.
To begin with the inside loop does n-1 comparisons, next time n-2 and so on. Finally
on the last iteration of the outside loop, the inside loop does 1 comparison. So on
average the inside loop does ((n-1) + 1) / 2 n/2 comparisons.
Therefore, the overall number of computation steps is n * n/2 = n2 /2
Complexity of bubble sort = O(n2)
INSERTION SORT.
Both the selection and bubble sorts exchange elements. But insertion sort does not exchange
elements. In insertion sort the element is inserted at an appropriate place similar to card
insertion. Here the list is divided into two parts sorted and unsorted sub-lists. In each pass, the
first element of unsorted sub list is picked up and moved into the sorted sub list by inserting it
in suitable position. Suppose we have n elements, we need n-1 passes to sort the elements.
Insertion sort works this way:
It works the way you might sort a hand of playing cards:
1. We start with an empty left hand [sorted array] and the cards face down on the table [unsorted
array].
2. Then remove one card [key] at a time from the table [unsorted array], and insert it into the
correct position in the left hand [sorted array].
3. To find the correct position for the card, we compare it with each of the cards already in the
hand, from right to left.

Example: INSERTION_SORT (A)


1. FOR j 2 TO length [A]
2.
DO key A[j]

3.
4.
5.
6.
7.
8.

{Put A[j] into the sorted sequence A [1 . . j - 1]}


ij-1
WHILE i > 0 and A[i] > key
DO A[i +1] A[i]
ii-1
A[i + 1] key

Example: Following figure (from CLRS) shows the operation of INSERTION-SORT on the
array
A= (5, 2, 4, 6, 1, 3). Each part shows what happens for a particular iteration with the value
of j indicated. j indexes the "current card" being inserted into the hand.

Read the figure row by row. Elements to the left of A[j] that are greater than A[j] move one
position to
the right, and A[j] moves into the evacuated position.
Ex:- A list of unsorted elements are: 78 23 45 8 32 36 . The results of insertion sort for
each pass is as follows:-

A list of sorted elements now: 8 23 32 36 45 78

Advantages
Good running time for almost sorted arrays (n)
Disadvantages
(n2) running time in worst and average case

n2/2 comparisons and exchanges

Applying the insertion sort algorithm to a set A L G O R I T H M.

This insertion algorithm follows the alphabetical order. We have the sorted and unsorted sides
separated by an imaginary wall. Pairs of letters are compared at a go and each letter is placed in
the best position according to the alphabetic order in the sorted side.This goes on till the last
position. Hence we have A G H I L M O R T.

You might also like