Algorithm ASSIGNMENT 1 Group 2
Algorithm ASSIGNMENT 1 Group 2
UNIVERSITY
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:
Example:
Ex:- A list of unsorted elements are: 10 47 12 54 19 23
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.
3.
4.
5.
6.
7.
8.
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:-
Advantages
Good running time for almost sorted arrays (n)
Disadvantages
(n2) running time in worst and average case
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.