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

AICT Lecture Notes (After Mid)

Uploaded by

paxaro2210
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

AICT Lecture Notes (After Mid)

Uploaded by

paxaro2210
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 77

Application of Information and Communication Technologies (CSC-100)

Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

(Week 09) Lecture 21 & 22


Objectives: Learning objectives of this lecture are

 Program Development Life Cycle


Text Book & Resources: Introduction to Computers 6th International Edition, Peter, N.
McGraw-Hill

Program Development Life Cycle


The overall process of creating a program is called program development
Program Development Life Cycle contains five phases

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)

 Goal: To understand the functions the software must perform

 Documentation: Includes program specifications (what it does,


timetable, programming language to be used, etc.)

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

 Pseudocode: uses natural language statements to outline the


logic of a program

 Algorithm: an unambiguous specification of how to solve a


problem

 Flowcharts: graphically show how a computer program will


process data.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

 Unified Modeling Language (UML) models: set of standard


notations for creating business models. It is widely used in
object-oriented programs, includes class diagrams, use case
diagrams, etc.

 Structure theorem: A pattern for controlling the flow of logic


in a computer program, module, or method
i. Sequence: series of statements that follow one another
ii. Selection: multiple paths, direction depends on result of
test
iii. Repetition: repeat series of steps

3. Good program design is essential as it saves a lot of time


4. Good program design principles;
 be specific
 all things the program must do, needs to be specified
 one-entry-point/one-exit-point rule
 no infinite loops or other logic errors
 design should be tested to ensure logic is correct
 desk check, tracing tables
5. Documentation: Includes design specifications

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

3. Coding standards are rules designed to standardize programming


 makes programs more readable and easier to maintain
 includes the proper use of comments to:
i. identify the programmer and last modification date
ii. explain variables used in the program
iii. identify the main parts of the program

4. Pretested error-free code segments can be re-used repeatedly with


minor modifications
 can greatly reduce development time
5. Documentation: Includes documented source code

Program Debugging and Testing


Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

1. The process of ensuring a program is free of errors (bugs).

2. Source code: coded program before it is compiled.

3. Object code: machine language version of a program.

4. Language translator: Program that converts source code to


machine language.

5. Types of language translators;


 Compilers: converts an entire program into machine language
before executing it
 Interpreters: translates one line of code at one time

6. Debugging: Finds initial errors.

7. Compiler errors: program doesn’t run typically, syntax errors that


occurs when the programmer has not followed the rules of the
programming language

8. Run time error: error that occurs when the program is running

9. Logic errors: program will run but produces incorrect results


Dummy print statements can help locate logic errors and other
run time errors.

10. Testing: Occurs after the program appears to be correct to find


any additional errors
 Should use good test data
 Tests conditions that will occur when the program is
implemented
 Should check for coding omissions (product quantity al
owed to be < 0, etc.)
Alpha test (inside organization)
Beta test (outside testers)

11. Documentation:
Completed program package (user’s manual, description of software
commands, troubleshooting guide to help with difficulties, etc.)

Program Implementation and Maintenance

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

(Week 10) Lecture 19 & 20


Objectives: Learning objectives of this lecture are

 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.

An algorithm is a list of rules to follow in order to solve a problem.

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.

How do we use algorithms in our everyday lives?

I need to make a cake


The algorithm here is a cake recipe. You can find the algorithm to solve this
problem in a cookbook!

I can't find the park


The algorithm you need is a set of directions to get to the park. There might
be different ways to the park so you can have different algorithms.

I need to build a toy


The algorithm to follow here is the list of instructions that tell you how to
make the toy.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

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:

The taxi algorithm:


1. Go to the taxi stand.
2. Get in a taxi.
3. Give the driver my address.

The call-me algorithm:


1. When your plane arrives, call my cell phone.
2. Meet me outside.

The rent-a-car algorithm:


1. Take the shuttle to the rental car place.
2. Rent a car.
3. Follow the directions to get to my house.

The bus algorithm:


1. Catch bus.
2. Get off on street.
3. Walk to my house.

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.

 In computer programming, there are often many different ways --


algorithms -- to accomplish any given task. Each algorithm has
advantages and disadvantages in different situations.

 To make a computer do anything, you have to write a computer


program. To write a computer program, you have to tell the
computer, step by step, exactly what you want it to do. The computer
then "executes" the program, following each step mechanically, to
accomplish the end goal.

 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.

 All the operations performed by computer consist of algorithms.


An algorithm is a well-defined procedure that allows a computer to
solve a problem. Another way to describe an algorithm is a sequence
of unambiguous instructions. The use of the term 'unambiguous'
indicates that there is no room for subjective interpretation. Every
time you ask your computer to carry out the same algorithm, it will
do it in exactly the same manner with the exact same result.

 Consider the earlier examples again. Spell checking uses algorithms.


Financial calculations use algorithms. A search engine uses
algorithms. In fact, it is difficult to think of a task performed by your
computer that does not use algorithms.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

How Do Algorithms Work?


Let's take a closer look at an example.

A very simple example of an algorithm would be to find the largest number


in an unsorted list of numbers. If you were given a list of five different
numbers, you would have this figured out in no time, no computer needed.
Now, how about five million different numbers? Clearly, you are going to
need a computer to do this, and a computer needs an algorithm.

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.

The algorithm would look something like this:

Step 1: Let Largest = L1


This means you start by assuming that the first number is the largest
number.

Step 2: For each item in the list:


This means you will go through the list of numbers one by one.

Step 3: If the item > Largest:


If you find a new largest number, move to step four. If not, go back to step
two, which means you move on to the next number in the list.

Step 4: Then Largest = the item


This replaces the old largest number with the new largest number you just
found. Once this is completed, return to step two until there are no more
numbers left in the list.

Step 5: Return Largest


This produces the desired result.

Notice that the algorithm is described as a series of logical steps in a


language that is easily understood. For a computer to actually use these
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

instructions, they need to be written in a language that a computer can


understand, known as a programming language.

Properties of Algorithms:
1. Input -An algorithm has input values from a specified set.

2. Output -From each set of input values an algorithm produces output


values from a specified set. The output values are the solution to the
problem.

3. Definiteness -The steps of an algorithm must be defined precisely.

4. Correctness -An algorithm should produce the correct output values


for each set of input values.

5. Finiteness -An algorithm should produce the desired output after a


finite (but perhaps large) number of steps for any input in the set.

6. Effectiveness -It must be possible to perform each step of an algorithm


exactly and in a finite amount of time.

7. Generality -The procedure should be applicable for all problems of the


desired form, not just for a particular set of input values.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

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.

Example from Daily life:


Let’s suppose one person have to come to house from his office, then how
will he make algorithm.
 Close the door office.
 Go to car parking.
 Ignite the car, and check to security guard.
 Drive the car to the house.
 The person has reached his destination.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

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.

Pseudo code makes creating programs easier. Programs can be complex


and long; preparation is the key. For years, flowcharts were used to map
out programs before writing one line of code in a language. However, they
were difficult to modify and with the advancement of programming
languages, it was difficult to display all parts of a program with a flowchart.
It is challenging to find a mistake without understanding the complete flow
of a program. That is where pseudo code becomes more appealing.

Pseudo code is a term which is often used in programming and algorithm


based fields. It is a methodology that allows the programmer to represent
the implementation of an algorithm. Simply, we can say that it’s the cooked
up representation of an algorithm. Often at times, algorithms are
represented with the help of pseudo codes as they can be interpreted by
programmers no matter what their programming background or knowledge
is. Pseudo code, as the name suggests, is a false code or a representation
of code which can be understood by even a layman with some school level
programming knowledge.

Pseudo code is an informal way of programming description that does not


require any strict programming language syntax or underlying technology
considerations. It is used for creating an outline or a rough draft of a
program. Pseudo code summarizes a program’s flow, but excludes
underlying details. System designers write pseudo code to ensure that
programmers understand a software project's requirements and align code
accordingly.

Description: Pseudo code is not an actual programming language. So it


cannot be compiled into an executable program. It uses short terms or
simple English language syntaxes to write code for programs before it is
actually converted into a specific programming language. This is done to
identify top level flow errors, and understand the programming data flows
that the final program is going to use. This definitely helps save time during
actual programming as conceptual errors have been already corrected.
Firstly, program description and functionality is gathered and then pseudo
code is used to create statements to achieve the required results for a
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

program. Detailed pseudo code is inspected and verified by the designer’s


team or programmers to match design specifications. Catching errors or
wrong program flow at the pseudo code stage is beneficial for development
as it is less costly than catching them later. Once the pseudo code is
accepted by the team, it is rewritten using the vocabulary and syntax of a
programming language. The purpose of using pseudo code is an efficient
key principle of an algorithm. It is used in planning an algorithm with
sketching out the structure of the program before the actual coding takes
place.

Algorithm: It’s an organized logical sequence of the actions or the


approach towards a particular problem. A programmer implements an
algorithm to solve a problem. Algorithms are expressed using natural verbal
but somewhat technical annotations.

Pseudo code: It’s simply an implementation of an algorithm in the form


of annotations and informative text. It has no syntax like any of the
programming language and thus can’t be compiled or interpreted by the
computer.

Advantages:
 Pseudo code is understood by the programmers of all types.

 It enables the programmer to concentrate only on the algorithm part


of the code development.

 Improves the readability of any approach. It’s one of the best


approaches to start implementation of an algorithm.

 Acts as a bridge between the program and the algorithm or flowchart.


Also works as a rough documentation, so the program of one
developer can be understood easily when a pseudo code is written
out. In industries, the approach of documentation is essential. And
that’s where a pseudo-code proves vital.

 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

Here is an if statement with an else section. Else allows for some


statements to be executed if the “if” condition is not met.

We can add additional conditions to execute different statements if met.


Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

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

(Week 11) Lecture 21 & 22


Objectives: Learning objectives of this lecture are

 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.

Searching Algorithms are designed to check for an element or retrieve an


element from any data structure where it is stored.

A search algorithm is an algorithm that accepts an argument ‘a’ and tries


to find an element whose value is ‘a’. It is possible that the search for a
particular element in a set is unsuccessful if that element does not exist.
There are number of techniques available for searching.

Why do we need searching algorithms?


We often need to find one particular item of data amongst many hundreds,
thousands, millions or more. For example, you might need to find
someone’s phone number on your phone, or a particular business’s address
in the country.

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.

Linear search is a very simple search algorithm. In this type of search, a


sequential search is made over all items one by one. Every item is checked
and if a match is found then that particular item is returned, otherwise the
search continues till the end of the data collection.

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.

Linear search is rarely used practically because other search algorithms


such as the binary search algorithm and hash tables allow significantly
faster searching comparison to linear search.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

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.

When binary search is used to perform operations on a sorted set, the


number of iterations can always be reduced on the basis of the value that
is being searched.
Binary search looks for a particular item by comparing the middle most
item of the collection. If a match occurs, then the index of item is returned.
If the middle item is greater than the item, then the item is searched in the
sub-array to the left of the middle item. Otherwise, the item is searched for
in the sub-array to the right of the middle item. This process continues on
the sub-array as well until the size of the sub array reduces to zero.

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.

Search a sorted array by repeatedly dividing the search interval in half.


Begin with an interval covering the whole array. If the value of the search
key is less than the item in the middle of the interval, narrow the interval
to the lower half. Otherwise narrow it to the upper half. Repeatedly check
until the value is found or the interval is empty.

Binary search is a fast search algorithm with run-time complexity of Ο(log


n). This search algorithm works on the principle of divide and conquer. For
this algorithm to work properly, the data collection should be in the sorted
form.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

How Binary Search Works?


For a binary search to work, it is mandatory for the target array to be
sorted. We shall learn the process of binary search with a pictorial
example. The following is our sorted array and let us assume that we need
to search the location of value 31 using binary search.

First, we shall determine half of the array by using this formula:


mid = low + (high - low) / 2
Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is the mid of
the array.

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

Hence, we calculate the mid again. This time it is 5.

We compare the value stored at location 5 with our target value. We find
that it is a match.

We conclude that the target value 31 is stored at location 5.

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

Comparison between Binary Search and Linear Search:


 Binary Search requires the input data to be sorted; Linear Search
doesn’t.

 Binary Search requires an ordering comparison; Linear Search only


requires equality comparisons.

 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

(Week 12) Lecture 23 & 24


Objectives: Learning objectives of this lecture are

 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.

Sorting refers to ordering data in an increasing or decreasing fashion


according to some linear relationship among the data items.

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).

Data is typically sorted based on actual values, counts or percentages, in


either ascending or descending order, but can also be sorted based on the
variable value labels. Value labels are metadata found in some programs
which allow the researcher to store labels for each value option of a
categorical question. Most software applications also allow sorting by
multiple variables. This type of sorting will be executed in a predetermined
variable priority, for example, a data set containing region and country
fields can first be sorted by region as the primary sort and then by country.
The county sort will be applied within each sorted region.

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.

Sorting can be performed using several methods, they are:

 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.

Selection sort is a simple sorting algorithm. This sorting algorithm is an in-


place comparison-based algorithm in which the list is divided into two parts,
the sorted part at the left end and the unsorted part at the right end.
Initially, the sorted part is empty and the unsorted part is the entire list.

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.

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

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

Following is a pictorial representation:


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

(Week 13) Lecture 25 & 26


Objectives: Learning objectives of this lecture are

 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:

The same happens with the element at position 2:

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:

A couple of situations that came up in our example bear a little more


scrutiny. When the key being inserted is less than all elements to its left
(as when we inserted keys 2 and 3), and when it's greater than or equal to
all elements to its left (as when we inserted key 13). In the former case,
every element in the sub array to the left of the key slides one position to
the right, and we have to stop once we've run off the left end of the array.
In the latter case, the first time we compare the key with an element to its
left, we find that the key is already in its correct position relative to all
elements to its left; no elements slide over and the key drops back into the
position in which it started.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Another Example:

Since 7 is the first element has no other element to be compared with, it


remains at its position. Now when on moving towards 4, 7 is the largest
element in the sorted list and greater than 4. So, move 4 to its correct
position i.e. before 7. Similarly with 5, as 7 (largest element in the sorted
list) is greater than 5, we will move 5 to its correct position. Finally for 2,
all the elements on the left side of 2 (sorted list) are moved one position
forward as all are greater than 2 and then 2 is placed in the first position.
Finally, the given array will result in a sorted array.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

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.

Bubble sort is a simple sorting algorithm. This sorting algorithm is


comparison-based algorithm in which each pair of adjacent elements is
compared and the elements are swapped if they are not in order.

Bubble Sort is a simple algorithm which is used to sort a given set


of n elements provided in form of an array with n number of elements.
Bubble Sort compares all the element one by one and sort them based on
their values.

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.

If we have total n elements, then we need to repeat this process for n-


1 times.
It is known as bubble sort, because with every complete iteration the
largest element in the given array, bubbles up towards the last place or the
highest index, just like a water bubble rises up to the water surface.

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.

In this case, value 33 is greater than 14, so it is already in sorted locations.


Next, we compare 33 with 27.

We find that 27 is smaller than 33 and these two values must be swapped.

The new array should look like this

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

Then we move to the next two values, 35 and 10.

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:

After the second iteration, it 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 1, 7 is compared with 4. Since 7>4, 7 is moved ahead of 4. Since


all the other elements are of a lesser value than 7, 7 is moved to the end
of the array.
Now the array is A[]={4,5,2,7}.

In step 2, 4 is compared with 5. Since 5>4 and both 4 and 5 are in


ascending order, these elements are not swapped. However, when 5 is
compared with 2, 5>2 and these elements are in descending order.
Therefore, 5 and 2 are swapped.
Now the array is A[]={4,2,5,7}.

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

(Week 14) Lecture 27 & 28


Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Objectives: Learning objectives of this lecture are

 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 once more:

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

A function that calls itself is known as a recursive function. And, this


technique is known as recursion.

Example: Sum of Natural Numbers Using Recursion


int main() {
int number, result;
cout<<"Enter a positive integer: ;
cin>>number;
result = sum(number);
cout<<"sum = "<<result;
return 0;
}

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

Initially, the sum() is called from the main() function


with number passed as an argument.
Suppose, the value of n inside sum() is 3 initially. During the next function
call, 2 is passed to the sum() function. This process continues until n is
equal to 0.

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

Difference between recursion and iteration:


Recursion and Iteration are both used for a similar objective: to execute a
sequential set of instructions repeatedly.

However, there are some major differences between them functionally.


They are outlined below:
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Linear Search: (Algorithm)

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

Linear Search: (C++)


#include "iostream"
using namespace std;
void main()
{
int* number = new int[5];
for (int i = 0; i<5; i++)
{
cout << "Enter number " << i + 1 << endl;
cin >> number[i];
}
cout << "Enter number to search ";
int searchnum;
cin >> searchnum;

bool found = false;


for (int i = 0; i < 5; i++)
{
if (number[i] == searchnum)
{
found = true;
break;
}
}
if (found)
cout << "Found " << endl;
else
cout << "Not Found " << endl;

Recursive Linear Search:


int recSearch(int arr[], int l, int n, int x)
{
if (n < l)
return -1;
if (arr[l] == x)
return l
return recSearch(arr, l + 1, n, x);
}
int main()
{
int arr[] = {4, 6, 2, 7, 1, 3};
int x = 4;
int index = recSearch(arr, 0, n - 1, x);

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

Search 7 from the list 4 , 6 , 2 , 7 , 1 , 3 using Recursive Linear search Algorithm


Search(0,6, 4,6,2,7,1,3
7)
A0≠7 and
1≠6
Search(1,6,7) 4,6,2,7,1,3
A1≠7 and 2≠6
Search(2,6,7) 4,6,2,7,1,3
A2≠7 and 3≠6
Search(3,6,7) 4,6,2,7,1,3
A3=7 so Element
return 3 found

Search 5 from the list 4 , 6 , 2 , 7 , 1 , 3 using Recursive Linear search Algorithm


Search(0,6,5) 4,6,2,7,1,3
A0≠5 and 1≠6
Search(1,6,5) 4,6,2,7,1,3
A1≠5and 2≠6
Search(2,6,5) 4,6,2,7,1,3
A2≠5 and 3≠6
Search(3,6,5) 4,6,2,7,1,3
A3≠5and 4≠6
Search(4,6,5) 4,6,2,7,1,3
A4≠5 and 5≠6
4,6,2,7,1,3 Search(5,6,5)
Element not A5≠5 and 6=6
found So return -1

Search 10 from the list 9 , 16 , 7 , 12 , 10 , 32 using Recursive Linear search Algorithm


Search(0,6, 10)
a1≠10 and 1≠6
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Recursive Binary Search:

Binary Search: (Algorithm)

BinarySearch(arr[], low, high, x)


{
if(high < low)
return -1
mid = low + high / 2
if(x == arr[mid])
return mid
if(x > arr[mid])
return BinarySearch(arr[], mid + 1, high, x)
if(x < arr[mid])
return BinarySearch(arr[], low, mid - 1, x)
}

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

(Week 15) Lecture 29 & 30


Objectives: Learning objectives of this lecture are

 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.

Divide and Conquer:


If we can break a single big problem into smaller sub-problems, solve the
smaller sub-problems and combine their solutions to find the solution for
the original big problem, it becomes easier to solve the whole problem.

Let's take an example, Divide and Rule.

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.

In Merge Sort, the given unsorted array with n elements, is divided


into n sub arrays, each having one element, because a single element is
always sorted in itself. Then, it repeatedly merges these sub arrays, to
produce new sorted sub arrays, and in the end, one complete sorted array
is produced.

The concept of Divide and Conquer involves three steps:


1. Divide the problem into multiple small problems.
2. Conquer the sub problems by solving them. The idea is to break down
the problem into atomic sub problems, where they are actually
solved.
3. Combine the solutions of the sub problems to find the solution of the
actual problem.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

How Merge Sort Works?


To understand merge sort, we take an unsorted array as the following:

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.

We compare 27 and 10 and in the target list of 2 values we put 10 first,


followed by 27. We change the order of 19 and 35 whereas 42 and 44 are
placed sequentially.

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 and Conquer Strategy:


Using the Divide and Conquer technique, we divide a problem into sub
problems. When the solution to each sub problem is ready, we 'combine'
the results from the sub problems to solve the main problem.

Suppose we had to sort an array A. A sub problem would be to sort a sub-


section of this array starting at index p and ending at index r, denoted
as A[p..r].

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.

Let's consider an array with values {14, 7, 3, 12, 9, 11, 6, 12}

In merge sort we follow the following steps:


1. We take a variable p and store the starting index of our array in this.
And we take another variable r and store the last index of array in it.

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 1 − if it is only one element in the list it is already sorted, return.

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

// function to merge the subarrays


void merge(int a[], int p, int q, int r)
{
int b[5]; //same size of a[]
int i, j, k;
k = 0;
i = p;
j = q + 1;
while(i <= q && j <= r)
{
if(a[i] < a[j])
{
b[k++] = a[i++]; // same as b[k]=a[i]; k++; i++;
}
else
{
b[k++] = a[j++];
}
}

while(i <= q)
{
b[k++] = a[i++];
}

while(j <= r)
{
b[k++] = a[j++];
}

for(i=r; i >= p; i--)


{
a[i] = b[--k]; // copying back the sorted list to a[]
}
}
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

// function to print the array


void printArray(int a[], int size)
{
int i;
for (i=0; i < size; i++)
{
cout<<a[i];
}
cout<<"\n";
}

int main()
{
int arr[] = {32, 45, 67, 2, 7};
int len = sizeof(arr)/sizeof(arr[0]);

cout<<"Given array: \n";


printArray(arr, len);

// calling merge sort


mergeSort(arr, 0, len - 1);

cout<<"\nSorted array: \n";


printArray(arr, len);
return 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

(Week 16) Lecture 31 & 32


Objectives: Learning objectives of this lecture are

 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

How Quick Sort Works?


1. A pivot element is chosen from the array. You can choose any
element from the array as the pivot element.

2. Here, we have taken the rightmost (i.e. the last element) of the array
as the pivot element.

Select a 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

The above arrangement is achieved by the following steps.


a. A pointer is fixed at the pivot element. The pivot element is
compared with the elements beginning from the first index. If
the element greater than the pivot element is reached, a
second pointer is set for that element.

b. Now, the pivot element is compared with the other elements (a


third pointer). If an element smaller than the pivot element is
reached, the smaller element is swapped with the greater
element found earlier.
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Comparison of pivot element with other elements


Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

c. The process goes on until the second last element is reached.


Finally, the pivot element is swapped with the second pointer.

d. Swap pivot element with the second pointer


Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

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.

6. At this point, the array is already sorted.


Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Quicksort uses recursion for sorting the sub-parts:


On the basis of Divide and conquer approach, quicksort algorithm can be
explained as:
 Divide
The array is divided into subparts taking pivot as the partitioning
point. The elements smaller than the pivot are placed to the left of
the pivot and the elements greater than the pivot are placed to the
right.

 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.

Sorting the elements on the left of pivot using recursion


Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Sorting the elements on the right of pivot using recursion


Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

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: You have an array A=[9,7,8,3,2,1] Observe in the diagram, that


the randpartition() function chooses pivot randomly as 7 and then swaps
it with the first element of the array and then the partition() function call
takes place, which divides the array into two halves. The first half has
elements less than 7 and the other half has elements greater than 7.

For elements less than 7, in 5th call, randpartition() function


chooses 2 as pivot element randomly and then swap it with first element
and call to the partition() function takes place. After the 7th and 8th call,
no further calls can take place as only one element left in both the calls.
Similarly, you can observe the order of calls for the elements greater
than 7.

Example:
Following are the steps involved in quick sort algorithm:

1. After selecting an element as pivot, which is the last index of the


array in our case, we divide the array for the first time.

2. In quick sort, we call this partitioning. It is not simple breaking


down of array into 2 sub arrays, but in case of partitioning, the array
elements are so positioned that all the elements smaller than
the pivot will be on the left side of the pivot and all the elements
greater than the pivot will be on the right side of it.

3. And the pivot element will be at its final sorted position.

4. The elements to the left and right, may not be sorted.

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.

Quick Sort Algorithm:


Using pivot algorithm recursively, we end up with smaller possible
partitions. Each partition is then processed for quick sort. We define
recursive algorithm for quicksort as follows:

Step 1 − Make any index value as pivot


Step 2 − partition the array using pivot value
Step 3 − quicksort left partition recursively
Step 4 − quicksort right partition recursively

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

int partition(int a[], int beg, int end)


{
int left, right, temp, loc, flag;
loc = left = beg;
right = end;
flag = 0;
while(flag != 1)
{
while((a[loc] <= a[right]) && (loc!=right))
right--;
if(loc==right)
flag =1;
else if(a[loc]>a[right])
{
temp = a[loc];
a[loc] = a[right];
a[right] = temp;
loc = right;
}
if(flag!=1)
{
while((a[loc] >= a[left]) && (loc!=left))
left++;
if(loc==left)
flag =1;
else if(a[loc] < a[left])
{
temp = a[loc];
a[loc] = a[left];
a[left] = temp;
loc = left;
}
}
}
return loc;
}
Application of Information and Communication Technologies (CSC-100)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

void quickSort(int a[], int beg, int end)


{
int loc;
if(beg<end)
{
loc = partition(a, beg, end);
quickSort(a, beg, loc-1);
quickSort(a, loc+1, end);
}
}

Video Link:

https://www.youtube.com/playlist?list=PLeby327DX1ciLpswc1b38BTgVZrVohXdZ

You might also like