AICT Lecture Notes (After Mid)
AICT Lecture Notes (After Mid)
1. Program Analysis
2. Program Design
3. Program coding
4. Program Testing and debugging
5. Program Implementation and maintenance
Program Analysis
The problem is considered, and the program specifications are
developed
specifications developed during this phase are reviewed by the
systems analyst and the programmer (the person who will code the
program)
Program Design
1. The program specifications are expanded into a complete design of
the new program
good program design is extremely important
2. Program design tools
Structure charts: depict the overall organization of a program
Program Coding
1. The program code (source code or the set of instructions) is written
using a programming language
2. When choosing a programming language, consider;
suitability to the application
integration with other programs
standards for the company
programmer availability
portability if being run on multiple platforms
development speed
8. Run time error: error that occurs when the program is running
11. Documentation:
Completed program package (user’s manual, description of software
commands, troubleshooting guide to help with difficulties, etc.)
1. Program implementation:
Once the system containing the program is installed (up and
running), the implementation process is complete.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
2. Program maintenance:
Process of updating software so it continues to be useful.
3. Documentation:
Amended program package.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Algorithms
Pseudo Code
Text Book & Resources: Introduction to Computers 6th International Edition, Peter, N.
McGraw-Hill
ALGORITHMS
An algorithm is step by step process to solve a problem, for obtaining
required output in a certain time.
Algorithms need to have their steps in the right order. Think about an
algorithm for getting dressed in the morning. What if you put on your coat
before your jumper? Your jumper would be on top of your coat and that
would be silly! When you write an algorithm the order of the instructions is
very important.
The algorithm is the basic technique used to get the job done. Let's follow
more example to help get an understanding of the algorithm concept.
Let's say that you have a friend arriving at the airport, and your friend
needs to get from the airport to your house. Here are four different
algorithms that you might give your friend for getting to your home:
All four of these algorithms accomplish exactly the same goal, but each
algorithm does it in completely different way. Each algorithm also has a
different cost and a different travel time. Taking a taxi, for example, is
probably the fastest way, but also the most expensive. Taking the bus is
definitely less expensive, but a whole lot slower. You choose the algorithm
based on the circumstances.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Algorithm in computer Science:
A set of steps that are followed in order to solve a mathematical
problem or to complete a computer process.
You use code to tell a computer what to do. Before you write code
you need an algorithm.
When you are telling the computer what to do, you also get to
choose how it's going to do it. That's where computer
algorithms come in.
Below is what the algorithm could look like. Let's say the input consists of
a list of numbers, and this list is called L. The number L1 would be the first
number in the list, L2 the second number, etc. And we know the list is not
sorted - otherwise, the answer would be really easy. So, the input to the
algorithm is a list of numbers, and the output should be the largest number
in the list.
Properties of Algorithms:
1. Input -An algorithm has input values from a specified set.
Main Points:
An algorithm must be
o Finite (not unlimited process and ambiguity).
o Starting point and ending point (it must stop on certain
point).
Importance:
An algorithm gives
o Basic idea of process.
o Outline of steps.
o Understanding of the process.
o Time saving for logical error correction.
o Complete sense of program.
Utilization:
Algorithms may use
o In daily life routine for planning purposes.
o In making rough ideas for solution of the problems.
o In computer science as raw understanding of program,
execution, limitation and output.
Pseudo Code
Pseudo code is an informal high-level description of a computer program or
algorithm. It is written in symbolic code which must be translated into a
programming language before it can be executed.
Advantages:
Pseudo code is understood by the programmers of all types.
The main goal of a pseudo code is to explain what exactly each line
of a program should do, hence making the code construction phase
easier for the programmer.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Limitations:
In case of pseudo code, a graphic representation of program logic is
not available.
There are no standard rules to follow in writing pseudo code. Different
programmers use their own style of writing pseudo code. Hence,
communication problem occurs due to lack of standardization.
For a beginner, it is more difficult to follow the logic of pseudo code
or write pseudo code.
Mathematical operations:
Mathematical operations are integral to solution development. They allow
us to manipulate the values we have stored. Here are common
mathematical symbols:
Keywords:
A keyword is a word that is reserved by a program because the word has a
special meaning. Keywords can be commands or parameters. Every
programming language has its own keywords (reserved words). Keywords
cannot be used as variable names.
In Pseudo code, they are used to indicate common input-output and
processing operations.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
CONDITIONALS:
During algorithm development, we need statements which evaluate
expressions and execute instructions depending on whether the expression
evaluated to True or False. Here are some common conditions used in
Pseudo code:
IF — ELSE IF — ELSE
This is a conditional that is used to provide statements to be executed if a
certain condition is met. This also applies to multiple conditions and
different variables.
Here is an if statement with one condition
ITERATION
To iterate is to repeat a set of instructions in order to generate a
sequence of outcomes. We iterate so that we can achieve a certain goal.
FOR structure
The FOR loop takes a group of elements and runs the code within the loop
for each element.
WHILE structure
Similar to the FOR loop, the while loop is a way to repeat a block of code
as long as a predefined condition remains true. Unlike the FOR loop, the
while loop evaluates based on how long the condition will remain true.
To avoid a scenario where our while loop runs infinitely, we add an
operation to manipulate the value within each iteration. This can be
through an increment, decrement, et cetera.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
FUNCTIONS
When solving advanced tasks, it is necessary to break down the concepts
in block of statements in different locations. This is especially true when
the statements in question serve a particular purpose. To reuse this code,
we create functions. We can then call these functions every-time we need
them to run.
To emulate a function call in pseudo code, we can use the Call keyword.
CONCLUSION:
There are no technical rules for Pseudo code. It is meant to be human
readable and still convey meaning and flow.
There are different guide and tutorials which lean more towards language-
specific pseudo code.
Video Link:
https://www.youtube.com/playlist?list=PLeby327DX1chhzU4z1fWz7E5yq_ksaqk0
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Searching
Linear Search
Binary Search
Text Book & Resources: Introduction to Computers 6th International Edition, Peter, N.
McGraw-Hill
Searching
Searching is a process of locating a particular element present in a given
set of elements. The element may be a record, a table, or a file.
This is why searching algorithms are important. Without them you would
have to look at each item of data – each phone number or business address
– individually, to see whether it is what you are looking for. In a large set
of data, it will take a long time to do this. Instead, a searching algorithm
can be used to help find the item of data you are looking for.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Linear Search
Linear search is used on a collection of items. It relies on the technique of
traversing a list from start to end by exploring properties of all the elements
that are found on the way.
For example, consider an array of integers of size. You should find and print
the position of all the elements with value. Here, the linear search is based
on the idea of matching each element from the beginning of the list to the
end of the list with the integer, and then printing the position of the element
if the condition is `True'.
The time complexity of the linear search is O(n) because each element in
an array is compared only once.
Example
Consider the following list of elements and the element to be searched...
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Another Example:
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Algorithm:
1. Take a value to be searched, from user
2. Set a variable ‘i’ to first index of array
3. Compare value with the element of list at index ‘i’
4. If value found display message and goto step 8
5. Increment ‘i’ by one
6. If i less than n and goto step 3
7. Display not found
8. Stop
Pseudo code:
1. Start
2. Input x
3. i := 0
4. if x = a[i] ,
display: found at index i goto 8
5. i++
6. if i < n
goto 4
7. display: value not found
8. stop
Value to search is 66
0 1 2 3 4
56 43 23 66 44
56 43 23 66 44
56 43 23 66 44
56 43 23 66 44
Web:
http://www.cs.armstrong.edu/liang/animation/web/LinearSearch.html
https://yongdanielliang.github.io/animation/web/LinearSearchNew.html
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Binary Search
Binary search is the most popular Search algorithm. It is efficient and also
one of the most commonly used techniques that is used to solve problems.
If all the names in the world are written down together in order and you
want to search for the position of a specific name, binary search will
accomplish this in a maximum of 35 iterations.
Binary search works only on a sorted set of elements. To use binary search
on a collection, the collection must first be sorted.
Binary search is an efficient algorithm for finding an item from a sorted list
of items. It works by repeatedly dividing in half the portion of the list that
could contain the item, until you've narrowed down the possible locations
to just one.
Now we compare the value stored at location 4, with the value being
searched, i.e. 31. We find that the value at location 4 is 27, which is not a
match. As the value is greater than 27 and we have a sorted array, so we
also know that the target value must be in the upper portion of the array.
We change our low to mid + 1 and find the new mid value again.
low = mid + 1
mid = low + (high - low) / 2
Our new mid is 7 now. We compare the value stored at location 7 with our
target value 31.
The value stored at location 7 is not a match, rather it is more than what
we are looking for. So, the value must be in the lower part from this
location.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
We compare the value stored at location 5 with our target value. We find
that it is a match.
Another Example:
Web:
http://www.cs.armstrong.edu/liang/animation/web/BinarySearch.html
https://yongdanielliang.github.io/animation/web/BinarySearchNew.html
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Algorithm
1. Take a value to be searched, from user
2. Set var low at start and high at end.
3. Calculate midpoint.
4. Compare value with value at mid.
5. If value found display message and go to step 9.
6. If low is equal to high display not found and go to step 9.
7. If value is greater from mid than set low equal to mid + 1 and go to step
3.
8. If value is less from mid than set high equal to mid – 1 and go to step 3.
9. Stop.
Pseudo Code
1. Start
2. Input x
3. low := 0, high := n-1
4. if high < low
display: value not found go to 9
5. mid := low + high/2
6. if x = a[mid]
display: found at index mid go to 9
7. if x > a[mid]
low := mid + 1 go to 4
8. if x < a[mid]
high := mid - 1 go to 4
9. stop
Binary Search has complexity O(log n); Linear search has complexity
O(n) as discussed earlier.
Binary Search requires random access to the data; Linear Search only
requires sequential access.
Video Link:
https://www.youtube.com/playlist?list=PLeby327DX1chatsrI4xy_7Wb1bpRCwUM0
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Sorting
Selection Sort
Text Book & Resources: Introduction to Computers 6th International Edition, Peter, N.
McGraw-Hill
Sorting
A sorting algorithm is an algorithm that puts elements of a list in a certain
order.
Data sorting is any process that involves arranging the data into some
meaningful order to make it easier to understand, analyze or visualize.
When working with research data, sorting is a common method used for
visualizing data in a form that makes it easier to comprehend the story the
data is telling. Sorting can be done with raw data (across all records) or at
an aggregated level (in a table, chart, or some other aggregated or
summarized output).
Sorting can be done on names, numbers and records. Sorting reduces the
for example, it is relatively easy to look up the phone number of a friend
from a telephone dictionary because the names in the phone book have
been sorted into alphabetical order.
This example clearly illustrates one of the main reasons that sorting large
quantities of information is desirable. That is, sorting greatly improves the
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
efficiency of searching. If we were to open a phone book, and find that the
names were not presented in any logical order, it would take an incredibly
long time to look up someone’s phone number.
A Simple Example:
To illustrate a basic sorting operation, consider the table below which has
two columns, Country and Population. The Country column is a text field
(or label), whereas the Population column contains numeric data. The table
on the left shows the original data which is not sorted in any particular
order. The table on the right has been sorted by Population in descending
order. In other words, the country with the highest population is sorted to
the first row, followed by the country with the second-highest population,
and so forth.
This allows the reader to easily understand the order of the countries,
without needing to compare all of the numbers in the table.
Selection sort
Insertion Sort
Bubble Sort
Quick Sort
Merge Sort, etc.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Selection Sort
The Selection sort algorithm is based on the idea of finding the minimum
or maximum element in an unsorted array and then putting it in its correct
position in a sorted array.
The smallest element is selected from the unsorted array and swapped with
the leftmost element, and that element becomes a part of the sorted array.
This process continues moving unsorted array boundary by one element to
the right.
This algorithm is not suitable for large data sets as its average and worst
case complexities are of Ο(n2), where n is the number of items.
The minimum element in the array i.e. 2 is searched for and then swapped
with the element that is currently located at the first position, i.e. 7. Now
the minimum element in the remaining unsorted array is searched for and
put in the second position, and so on.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Implementation:
Given array as an example.
For the first position in the sorted list, the whole list is scanned
sequentially. The first position where 14 is stored presently, we search the
whole list and find that 10 is the lowest value.
So we replace 14 with 10. After one iteration 10, which happens to be the
minimum value in the list, appears in the first position of the sorted list.
For the second position, where 33 is residing, we start scanning the rest
of the list in a linear manner.
We find that 14 is the second lowest value in the list and it should appear
at the second place. We swap these values.
After two iterations, two least values are positioned at the beginning in a
sorted manner.
The same process is applied to the rest of the items in the array.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Another Example:
Assume that the array A=[7,5,4,2] needs to be sorted in ascending order.
The minimum element in the array i.e. 2 is searched for and then
swapped with the element that is currently located at the first position,
i.e. 7. Now the minimum element in the remaining unsorted array is
searched for and put in the second position, and so on.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Algorithm:
1. Get a list of unsorted elements
2. Divide the list logically using a marker into two sub-lists: sorted and
unsorted
3. Repeat Step 4-6 until one element is remain in unsorted list
4. Select the largest element in unsorted list
5. Swap it with the element at the end of the unsorted list
6. Decrement the marker
7. Stop
Pseudo code:
Take an array ‘A’ of size ‘n’
Set a ‘k’ to last index
WHILE (k >= 1)
Max := 0
j := 1
WHILE (j <= k)
IF (A[max] < A[j])
Max := j
ENDIF
J++
ENDWHILE
Swap A[max] with A[k]
k--
ENDWHILE
Stop
Video Link:
https://www.youtube.com/playlist?list=PLeby327DX1cjkrf4pq0U1tY1K2zUVqTCr
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Insertion Sort
Bubble Sort
Text Book & Resources: Introduction to Computers 6th International Edition, Peter, N.
McGraw-Hill
Insertion Sort
This is an in-place comparison-based sorting algorithm. Here, a sub-list is
maintained which is always sorted. For example, the lower part of an array
is maintained to be sorted. An element which is to be 'inserted in this sorted
sub-list, has to find its appropriate place and then it has to be inserted
there. Hence the name, insertion sort.
Insertion sort is based on the idea that one element from the input
elements is consumed in each iteration to find its correct position i.e, the
position to which it belongs in a sorted array.
It iterates the input elements by growing the sorted array at each iteration.
It compares the current element with the largest value in the sorted array.
If the current element is greater, then it leaves the element in its place and
moves on to the next element else it finds its correct position in the sorted
array and moves it to that position. This is done by shifting all the elements,
which are larger than the current element, in the sorted array to one
position ahead.
There are many different ways to sort. As selection sort runs, the sub array
at the beginning of the array is sorted, but the sub array at the end is not.
Selection sort scans the unsorted sub array for the next element to include
in the sorted sub array.
Example:
Here's another way to think about sorting. Imagine that you are playing a
card game. You're holding the cards in your hand, and these cards are
sorted. The dealer hands you exactly one new card. You have to put it into
the correct place so that the cards you're holding are still sorted. In
selection sort, each element that you add to the sorted sub array is no
smaller than the elements already in the sorted sub array. But in our card
example, the new card could be smaller than some of the cards you're
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
already holding, and so you go down the line, comparing the new card
against each card in your hand, until you find the place to put it. You insert
the new card in the right place, and once again, your hand holds fully sorted
cards. Then the dealer gives you another card, and you repeat the same
procedure. Then another card, and another card, and so on, until the dealer
stops giving you cards.
This is the idea behind insertion sort. Loop over positions in the array,
starting with index 1. Each new position is like the new card handed to you
by the dealer, and you need to insert it into the correct place in the sorted
sub array to the left of that position.
The array is searched sequentially and unsorted items are moved and
inserted into the sorted sub-list (in the same array). This algorithm is not
suitable for large data sets as its average and worst case complexity are of
Ο(n2), where n is the number of items.
Implementation:
In terms of arrays, imagine that the sub array from index 0 through
index 5 is already sorted, and we want to insert the element currently in
index 6 into this sorted sub array, so that the sub array from index 0
through index 6 is sorted.
And here's what the sub array should look like when we're done:
To insert the element in position 6 into the sub array to its left, we
repeatedly compare it with elements to its left, going right to left. Let's call
the element in position 6 the key. Each time we find that the key is less
than an element to its left, we slide that element one position to the right,
since we know that the key will have to go to that element's left. We'll need
to do two things to make this idea work: we need to have a slide operation
that slides an element one position to the right, and we need to save the
value of the key in a separate place (so that it doesn't get overridden by
the element to its immediate left). In our example, let's pull the element at
index 6 into a variable called key:
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Now, we compare key with the element at position 5. We find that key (5)
is less than the element at position 5 (13), and so we slide this element
over to position 6:
Notice that the slide operation just copies the element one position to the
right. Next, we compare key with the element at position 4. We find
that key (5) is less than the element at position 4 (10), and we slide this
element over:
Next, we compare key with the element at position 3, and we slide this
element over:
Now we come to the element at position 1, which has the value 3. This
element is less than key, and so we do not slide it over. Instead, we
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
drop key into the position immediately to the right of this element (that is,
into position 2), whose element was most recently slid to the right. The
result is that the sub array from index 0 through index 6 has become
sorted:
Insertion sort repeatedly inserts an element in the sorted sub array to its
left. Initially, we can say that the sub array containing only index 0 is
sorted, since it contains only one element, and how can a single
element not be sorted with respect to itself? It must be sorted. Let's work
through an example. Here's our initial array:
Because the sub array containing just index 0 is our initial sorted sub array,
the first key is in index 1. (We'll show the sorted sub array in red, the key
in yellow, and the part of the array that we have yet to deal with in blue.)
We insert the key into the sorted sub array to its left:
Now the sorted sub array runs from index 0 through index 1, and the new
key is in index 2. We insert it into the sorted sub array to its left:
We keep going, considering each array element in turn as the key, and
inserting it into the sorted sub array to its left:
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Once we've inserted that rightmost element in the array, we have sorted
the entire array:
Another Example:
Algorithm:
8. Get a list of unsorted elements
9. Pick element from unsorted sub-list
10. Compare with all elements in the sorted sub-list
11. Shift all the elements in the sorted sub-list that is greater than the
value to be sorted
12. Insert the value
13. Repeat step 2-5 n times
14. Stop
Pseudo code:
Take an array ‘A’ of size ‘n’
var i, key, j
for i = 1 to n
key = a[i]
j=i-1
while (j >= 0 && a[j] > key)
a[j + 1] = a[j]
j--
a[j + 1] = key
Stop
https://yongdanielliang.github.io/animation/web/InsertionSortNew.html
https://www.geeksforgeeks.org/insertion-sort/
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Bubble Sort
Bubble sort is based on the idea of repeatedly comparing pairs of adjacent
elements and then swapping their positions if they exist in the wrong order.
If the given array has to be sorted in ascending order, then bubble sort will
start by comparing the first element of the array with the second element,
if the first element is greater than the second element, it will swap both the
elements, and then move on to compare the second and the third element,
and so on.
Sorting takes place by stepping through all the elements one-by-one and
comparing it with the adjacent element and swapping them if required.
This algorithm is not suitable for large data sets as its average and worst
case complexity are of Ο(n2) where n is the number of items.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Example:
Imagine there are five cars all travelling down a straight road. They are all
being driven on cruise control, but each of the cars’ speeds have been set
to slightly different values.
When a car is travelling faster than the car in front, it will overtake it, and
occupy the slower car’s position in the traffic. This will keep happening, with
each car switching positions with any slower car in front of it. Eventually
the cars will sort themselves according to their speeds, with the fastest car
being at the front of the queue of traffic.
Animation:
https://yongdanielliang.github.io/animation/web/BubbleSortNew.html
Implementation:
Take an unsorted array:
Bubble sort starts with very first two elements, comparing them to check
which one is greater.
We find that 27 is smaller than 33 and these two values must be swapped.
Next we compare 33 and 35. We find that both are in already sorted
positions.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
We know then that 10 is smaller 35. Hence they are not sorted.
We swap these values. We find that we have reached the end of the array.
After one iteration, the array should look like this:
Notice that after each iteration, at least one value moves at the end.
And when there's no swap required, bubble sorts learns that an array is
completely sorted.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Another Example:
In step 3, the element 4 is compared with 2. Since 4>2 and the elements
are in descending order, 4 and 2 are swapped.
The sorted array is A[]={2,4,5,7}.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Algorithm
1. Starting with the first element compare the current element with the
next element of the array.
2. If the current element is greater than the next element of the array,
swap them.
3. If the current element is less than the next element, move to the next
element.
4. The above process goes on until the last element.
Pseudo Code
9. Start
10. var i, j
11. for i = 0 to n-1
12. for j = 0 to j < n-i-1
13. if a[j] > a[j+1]
14. Swap a[j], a[j+1])
15. stop
Video Link:
https://www.youtube.com/playlist?list=PLeby327DX1chkfge2ibUcpoAdw0HYAMWh
Recursion
Recursive Linear Search
Recursive Binary Search
Text Book & Resources: Introduction to Computers 6th International Edition, Peter, N.
McGraw-Hill
Recursion
The process in which a function calls itself directly or indirectly is called
recursion and the corresponding function is called as recursive function.
Using recursive algorithm, certain problems can be solved quite easily.
Have you ever seen a set of Russian dolls? At first, you see just one figurine,
usually painted wood, which looks something like this:
You can remove the top half of the first doll, and what do you see inside?
Another, slightly smaller, Russian doll!
You can remove that doll and separate its top and bottom halves. And you
see yet another, even smaller, doll:
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
And you can keep going. Eventually you find the teeniest Russian doll. It is
just one piece, and so it does not open:
We started with one big Russian doll, and we saw smaller and smaller
Russian dolls, until we saw one that was so small that it could not contain
another.
What do Russian dolls have to do with algorithms? Just as one Russian doll
has within it a smaller Russian doll, which has an even smaller Russian doll
within it, all the way down to a tiny Russian doll that is too small to contain
another, we'll see how to design an algorithm to solve a problem by solving
a smaller instance of the same problem, unless the problem is so small that
we can just solve it directly. We call this technique recursion.
In the recursive program, the solution to the base case is provided and the
solution of the bigger problem is expressed in terms of smaller problems.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
int sum(int n) {
if (n != 0)
// sum() function calls itself
return n + sum(n-1);
else
return n;
}
Output:
Enter a positive integer:3
sum = 6
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
When n is equal to 0, the if condition fails and the else part is executed
returning the sum of integers ultimately to the main() function.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
LinearSearch(arr[], i, n, x)
{
if(n < 1 || i >= n)
return -1
if(arr[i] == x)
return i
return LinearSearch(arr[], i+ 1, n, x)
}
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
if (index != -1)
cout << "Element " << x << " is present”;
else
cout << "Element" << x << " is not present" ;
return 0;
}
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Video Link:
https://www.youtube.com/playlist?list=PLeby327DX1chSZtAIbiC4nuVFkhjLYbAH
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Merge Sort
Text Book & Resources: Introduction to Computers 6th International Edition, Peter, N.
McGraw-Hill
Merge Sort
Merge sort is a divide-and-conquer algorithm based on the idea of breaking
down a list into several sub-lists until each sub list consists of a single
element and merging those sub lists in a manner that results into a sorted
list.
Merge Sort follows the rule of Divide and Conquer to sort a given set of
numbers/elements, recursively, hence consuming less time.
When Britishers came to India, they saw a country with different religions
living in harmony, hardworking but naive citizens, and unity in diversity,
and found it difficult to establish their empire. So, they adopted the policy
of Divide and Rule. Where the population of India was collectively a one big
problem for them, they divided the problem into smaller problems, by
instigating rivalries between local kings, making them stand against each
other, and this worked very well for them.
Well that was history, and a socio-political policy (Divide and Rule), but the
idea here is, if we can somehow divide a problem into smaller sub-
problems, it becomes easier to eventually solve the whole problem.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Idea:
Divide the unsorted list into sub lists, each containing element.
Take adjacent pairs of two singleton lists and merge them to form a list of
2 elements. Will now convert into lists of size 2.
Repeat the process till a single sorted list of obtained.
While comparing two sub lists for merging, the first element of both lists is
taken into consideration. While sorting in ascending order, the element that
is of a lesser value becomes a new element of the sorted list. This procedure
is repeated until both the smaller sub lists are empty and the new combined
sub list comprises all the elements of both the sub lists.
We know that merge sort first divides the whole array iteratively into equal
halves unless the atomic values are achieved. We see here that an array
of 8 items is divided into two arrays of size 4.
This does not change the sequence of appearance of items in the original.
Now we divide these two arrays into halves.
We further divide these arrays and we achieve atomic value which can no
more be divided.
Now, we combine them in exactly the same manner as they were broken
down. Please note the color codes given to these lists.
We first compare the element for each list and then combine them into
another list in a sorted manner. We see that 14 and 33 are in sorted
positions.
In the next iteration of the combining phase, we compare lists of two data
values, and merge them into a list of found data values placing all in a
sorted order.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
After the final merging, the list should look like this:
Example:
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
From the image above, at each step a list of size M is being divided
into 2 sub lists of size M/2, until no further division can be done. To
understand better, consider a smaller array A containing the elements (9,
7, 8).
At the first step this list of size 3 is divided into 2 sub lists the first
consisting of elements (9, 7) and the second one being (8). Now, the first
list consisting of elements (9, 7) is further divided into 2 sub lists consisting
of elements (9) and (7) respectively.
As no further breakdown of this list can be done, as each sub list consists
of a maximum of 1 element, we now start to merge these lists. The 2 sub-
lists formed in the last step are then merged together in sorted order using
the procedure mentioned above leading to a new list (7, 9). Backtracking
further, we then need to merge the list consisting of element (8) too with
this list, leading to the new sorted list (7, 8, 9).
Divide:
If q is the half-way point between p and r, then we can split the sub
array A[p..r] into two arrays A[p..q] and A[q+1, r].
Conquer:
In the conquer step, we try to sort both the sub arrays A[p..q] and A[q+1,
r]. If we haven't yet reached the base case, we again divide both these sub
arrays and try to sort them.
Combine:
When the conquer step reaches the base step and we get two sorted sub
arrays A[p..q] and A[q+1, r] for array A[p..r], we combine the results by
creating a sorted array A[p..r] from two sorted sub
arrays A[p..q] and A[q+1, r].
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Example:
As we have already discussed that merge sort utilizes divide-and-conquer
rule to break the problem into sub-problems, the problem in this case
being, sorting a given array.
In merge sort, we break the given array midway, for example if the original
array had 6 elements, then merge sort will break it down into two sub
arrays with 3 elements each.
But breaking the original array into 2 smaller sub arrays is not helping us
in sorting the array.
So we will break these sub arrays into even smaller sub arrays, until we
have multiple sub arrays with single element in them. Now, the idea here
is that an array with a single element is already sorted, so once we break
the original array into sub arrays which has only a single element, we have
successfully broken down our problem into base problems.
And then we have to merge all these sorted sub arrays, step by step to
form one single sorted array.
2. Then we find the middle of the array using the formula (p + r)/2 and
mark the middle index as q, and break the array into two sub arrays,
from p to q and from q + 1 to r index.
3. Then we divide these 2 sub arrays again, just like we divided our
main array and this continues.
4. Once we have divided the main array into sub arrays with single
elements, then we start merging the sub arrays.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Algorithm:
Merge sort keeps on dividing the list into equal halves until it can no more
be divided. By definition, if it is only one element in the list, it is sorted.
Then, merge sort combines the smaller sorted lists keeping the new list
sorted too.
Step 2 − divide the list recursively into two halves until it can no more be
divided.
Step 3 − merge the smaller lists into new list in sorted order.
Code:
void mergeSort(int a[], int p, int r)
{
int q;
if(p < r)
{
q = (p + r) / 2;
mergeSort(a, p, q);
mergeSort(a, q+1, r);
merge(a, p, q, r);
}
}
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
while(i <= q)
{
b[k++] = a[i++];
}
while(j <= r)
{
b[k++] = a[j++];
}
int main()
{
int arr[] = {32, 45, 67, 2, 7};
int len = sizeof(arr)/sizeof(arr[0]);
Video Link:
https://www.youtube.com/playlist?list=PLeby327DX1ci-9YKyTbNqiCnwOf6km5o0
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Quick Sort
Text Book & Resources: Introduction to Computers 6th International Edition, Peter, N.
McGraw-Hill
Quick Sort
Quick sort is based on the divide-and-conquer approach based on the idea
of choosing one element as a pivot element and partitioning the array
around it such that: Left side of pivot contains all the elements that are less
than the pivot element Right side contains all elements greater than the
pivot.
Quick Sort is also based on the concept of Divide and Conquer, just like
merge sort. But in quick sort all the heavy lifting(major work) is done while
dividing the array into sub arrays, while in case of merge sort, all the real
work happens during merging the sub arrays. In case of quick sort, the
combine step does absolutely nothing.
It is also called partition-exchange sort. This algorithm divides the list into
three main parts:
1. Elements less than the Pivot element
2. Pivot element (Central element)
3. Elements greater than the pivot element
Pivot element can be any element from the array, it can be the first
element, the last element or any random element.
For example: In the array {52, 37, 63, 14, 17, 8, 6, 25}, we take 25 as
pivot. So after the first pass, the list will be changed like this.
{6 8 17 14 25 63 37 52}
Hence after the first pass, pivot will be set at its position, with all the
elements smaller to it on its left and all the elements larger than to its right.
Now 6 8 17 14 and 63 37 52 are considered as two separate sub arrays,
and same recursive logic will be applied on them, and we will keep doing
this until the complete array is sorted.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
2. Here, we have taken the rightmost (i.e. the last element) of the array
as the pivot element.
3. The elements smaller than the pivot element are put on the left and
the elements greater than the pivot element are put on the right.
Put all the smaller elements on the left and greater on the right of pivot
element
4. Pivot elements are again chosen for the left and the right sub-parts
separately. Within these sub-parts, the pivot elements are placed at
their right position. Then, step 2 is repeated.
Select pivot element of in each half and put at correct place using
recursion
5. The sub-parts are again divided into smaller sub-parts until each
subpart is formed of a single element.
Conquer
The left and the right subparts are again partitioned using the by
selecting pivot elements for them. This can be achieved by recursively
passing the subparts into the algorithm.
Combine
This step does not play a significant role in quicksort. The array is
already sorted at the end of the conquer step.
You can understand the working of quicksort with the help of the
illustrations below.
Example:
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Here we find the proper position of the pivot element by rearranging the
array using partition function. Then we divide the array into two halves left
side of the pivot (elements less than pivot element) and right side of the
pivot (elements greater than pivot element) and apply the same step
recursively.
Example:
Following are the steps involved in quick sort algorithm:
5. Then we pick sub arrays, elements on the left of pivot and elements
on the right of pivot, and we perform partitioning on them by
choosing a pivot in the sub arrays.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Let's consider an array with values {9, 7, 5, 11, 12, 2, 14, 3, 10, 6}
In step 1, we select the last element as the pivot, which is 6 in this case,
and call for partitioning, hence re-arranging the array in such a way
that 6 will be placed in its final position and to its left will be all the elements
less than it and to its right, we will have all the elements greater than it.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Then we pick the sub array on the left and the sub array on the right and
select a pivot for them, in the above diagram, we chose 3 as pivot for the
left sub array and 11 as pivot for the right sub array.
And we again call for partitioning.
Code:
int partition(int a[], int beg, int end);
void quickSort(int a[], int beg, int end);
void main()
{
int i;
int arr[10]={90,23,101,45,65,28,67,89,34,29};
quickSort(arr, 0, 9);
cout<<"\n The sorted array is: \n";
for(i=0;i<10;i++)
cout<<arr[i];
}
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Video Link:
https://www.youtube.com/playlist?list=PLeby327DX1ciLpswc1b38BTgVZrVohXdZ