Dsa Reviewer

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

CHAPTER 1: INTRO TO DSA

*Data Search − Consider an inventory of 1 million (106) items of a store. If the application is to
search an item, it has to search an item in 1 million (106) items every time slowing down the
search. As data grows, search will become slower.
*Processor speed − Processor speed although being very high, falls limited if the data grows
to billion records.
*Multiple requests − As thousands of users can search data simultaneously on a web server,
even the fast server fails while searching the data.
What is Data Structure?
*Data structure is a representation of data and the operations allowed on that data.
*A data structure is a way to store and organize data in order to facilitate the access and
Modifications.
*Data Structure are the method of representing of logical relationships, between individual
data elements related to the solution of a given problem.
*A method of storage which provides through a set of operations functionality to manipulate
the data in some useful way.
What is an Abstract Data Type?
Abstract Data Type (ADT) -
1) An opportunity for an acronym.
2) Mathematical description of an object and the set of operations on the
Object.
Data Structures : Algorithms
Algorithm - A high level, language independent description of a step-by-step process for
solving a problem.
Data Structure - A set of algorithms which implement an ADTv.
Dictionary ADT
*list
*binary search tree
*AVL tree
*Splay tree
*Red-Black tree
*hash table
Generates tensions:
*time vs. space
*performance vs. elegance
*generality vs. simplicity
*one operation’s performance vs. another’s
What is an Algorithm ?
*An algorithm is a finite set of instructions or logic, written in order, to accomplish a certain
predefined task.
*Algorithm is not the complete code or program, it is just the core logic(solution) of a
problem, which can be expressed either as an informal high level description as pseudocode or
using a flowchart.
Every Algorithm must satisfy the following properties:
*Input- There should be 0 or more inputs supplied externally to the algorithm.
*Output- There should be at least 1 output obtained.
*Definiteness- Every step of the algorithm should be clear and well defined.
*Finiteness- The algorithm should have finite number of steps.
*Correctness- Every step of the algorithm must generate a correctoutput.
An algorithm is said to be efficient and fast, if it takes less time to execute and consumes less
memory space. The performance of an algorithm is measured on the basis of following
properties:
1. Time Complexity
2. Space Complexity

Space Complexity
*It’s the amount of memory space required by the algorithm, during the course of its
execution. Space complexity must be taken seriously for multi-user systems and in situations
where limited memory is available.
*Instruction Space: It’s the space required to store the executable version of the program.
This space is fixed, but varies depending upon the number of lines of code in the program.
*Data Space: It’s the space required to store all the constants and Variables (including
temporary variables) value.
*Environment Space: It’s the space required to store the environment information needed to
resume the suspended function.
Time Complexity
*Time Complexity is a way to represent the amount of time required by the program to run till
its completion. It's generally a good practice to try to keep the time required minimum, so that
our algorithm completes its execution in the minimum time possible.

CHAPTER 2: cs 131
What is ALGORITHM?
*“A step by step problem solving procedure, especially on established, recursive computation
procedure for solving a problem in finite number of steps.”
*Finite sequence of steps for solving a logical or mathematical problem.
*A specific set of well- defined simple mathematical and logical procedures that can be
followed to solve a problem in finite number of steps.
* Instead of being limited to the built – in operators of programming language (addition,
subtraction, etc.), a programmer, by using procedures, is free to define his own operators and
apply them to operands that need not be basics.
Characteristics of an Algorithm
1. Specify each step or instructions exactly.
• There must be no ambiguity
• The instructions must be clear
2. There must be a finite number of steps.
• The algorithms must terminate
• There must be stopping point
3. There must be an output.
• The Algorithm must produce the correct result.

Criteria for Algorithm


 Input
• There are zero or more quantities which are externally supplied.
 Output
• At least one quantity is produced.
 Definiteness
• Each instruction must be clear and precise.
 Finiteness
• If we trace out the instructions of an algorithm, then for all cases, the algorithm will
terminate after a finite # of steps.
 Effectiveness
• Every instruction must be sufficiently basic that it can, in principle, be carried out by a person
using only pencil and paper.
• It must be feasible.
FLOWCHARTING
*A chart that contains symbols referring to computer operations, describing how the program
performs.
*Is the traditional graphical tool using standardized Symbols?
Types of flowcharts
There are four types of flowchart.
Namely:
• Document Flowchart
• Data Flowchart
• System Flowchart
• Program Flowchart
Document Flowchart
*A document flowchart traces the movement of a document, such as internal memos, payroll
information and interoffice mail, through a system.
* Usually, document flowcharts contain minimal detail, just the route the document takes
from one place to another.
Data Flowchart
*A data flowchart illustrates how data pass through a system. Symbols connote operations
involved in the flow of data and the storage, input and output materials needed to keep the
flow going.
System Flowchart
* A system flowchart shows how an entire system works by demonstrating how data flows and
what decisions are made to control this event.

*Symbols that connote decisions, processes, inputs and outputs and data flow are the most
important elements of a system flowchart. These differ from data flowcharts because they
show decisions, which are more detailed.
Program Flowchart
*A program flowchart demonstrates how a program works within a system.
*These flowcharts show any and all user-interaction pathways by using mboxes and arrows.
These arrows and boxes form hierarchical menus.
*Program charts can be large and complex. However, they are useful for mapping an entire
program.
Flowlines

Are indicated by straight lines w/ optional arrows to show the direction of the dataflow. The
arrowhead is necessary when the flow direction might be in doubt. Flowlines are used to
connect blocks by existing from one and entering another.
Terminal Box
Flattened Ellipses indicated the start and the end of the module. An Ellipse uses the name of
the module at the start. The End is indicated by the word ‘End’ or ‘Stop’ for the top or control
module. A start has no flowlines entering it and only one existing it; an end or exit has one
flowline entering it but none existing it.

Decision Box
* the diamond indicates a decision. It has one entrance and Two exits from the block. One exit
is the action when the resultant is true and other exit is the action when resultant is false.
Flowlines Convention:
True T
False F
Yes Y
No N
Decision Table:
True + True = True
True + False = False
False + True = False
False + False = False
What is a condition? Conditions are statements that result a Boolean value. The Boolean value
may either be true or false, 0 or 1.
Conditions are categorized into two:
* Logical Operators
* Relational Operators
Conditional Statements:
* In programming, there are three basic operators that you could use for conditional
statements: And, Not, Or.
And (&&)
Not (!=)
Or (||)
Relational Operators:
Relational Operators are used in order to get the Boolean result of certain condition.
*< Less Than
* > Greater Than
* <= Less Than Equal
* >= Greater Than Equal
* == Is Equal To
* != Not Equal To
* <> Not Equal To
Boolean Result:
The result may be a True or False value, depending on the condition that being evaluated.
IF STATEMENT:
The IF statement is conditional statement. The Figure illustrates how the IF Statement may
appear in the flowchart.

IF ELSE STATEMENT:
*The If – Else Statement is a conditional statement that provides the computer with a choice
between two options.
SWITCH STATEMENT:
The SWITCH Statement is basically an expansion of the If – Else statement. It provides the
mechanism for choosing an option among several possibilities.
PSEUDOCODES
*is the textual representation of an Algorithm’s Flowchart.
*It is a kind of stylized English that could be translated more or less readily into program.
*Pseudocode language closely resembles the C programming language.
*Using Pseudocodes emphasizes the difference between algorithms and programs.
*Pseudocodes emerge from flowcharts.
--The pseudocode of a procedure begins with procedure name followed by open and close
parenthesis ().
sum(a,b){ int a, int b }
*Words enclosed in open and close parenthesis, (), represent parameters.
*An open and close curly bracket, {}, encloses the pseudocode statements within a procedure.
*Words written in Italics represent user defined entries such as variables.

TOPIC 3
ALGORITHM ANALYSIS AND TIME COMPLEXITY
THE BASICS OF ALGORITHM
Search − Algorithm to search an item in a data structure.
Sort − Algorithm to sort items in a certain order.
Insert − Algorithm to insert item in a data structure.
Update − Algorithm to update an existing item in a data structure.
Delete − Algorithm to delete an existing item from a data structure.
ASYMPTOTIC ANALYSIS
Asymptotic analysis of an algorithm refers to defining the mathematical foundation/framing of
its run-time performance.
ALGORITHM COMPLEXITY
*Suppose X is an algorithm and n is the size of input data, the time and space used by the
algorithm X are the two main factors, which decide the efficiency of X.
*Time Factor − Time is measured by counting the number of key operations such as
comparisons in the sorting algorithm.
*Space Factor − Space is measured by counting the maximum memory space required by the
algorithm.
*The complexity of an algorithm f(n) gives the running time and/or the storage space required
by the algorithm in terms of n as the size of input data.
SPACE COMPLEXITY
Space complexity of an algorithm represents the amount of memory space required by the
algorithm in its life cycle. The space required by an algorithm is equal to the sum of the
following two components:
A fixed part that is a space required to store certain data and variables, that are independent
of the size of the problem. For example, simple variables and constants used, program size,
etc.
A variable part is a space required by variables, whose size depends on the size of the
problem. For example, dynamic memory allocation, recursion stack space, etc.
Space complexity S(P) of any algorithm P is S(P) = C + SP(I), where C is the fixed part and S(I) is
the variable part of the algorithm, which depends on instance characteristic I.

CHAPTER 4: ARRAY
DIMENSIONS OF ARRAY
One-Dimensional Array (Single-Dimension Array or Vector): A list of related Values
- All items in list have same data type
- All list members stored using single group name.
Example: a list of grades
98, 86, 88, 93, 84
- All grades are integers and must be declared
* can be declared as single unit under a common name (the array name).
One Dimensional Array
Below are examples of valid array element references:
n1[5]
num[5]
doub[1000]
Below are examples of invalid array element references:
n2[-1]
num[5.5]
doub[10.00]
Two Dimensional (2D) Array:
How do we declare a 2D array?
Similar to the 1D array, we must specify the data type, the name, and the size of
the array. But the size of the array is described as the number of rows and number of
columns. For example:

int a[MAX_ROWS][MAX_COLS];

BASIC OPERATIONS
The data in the data structures are processed by certain operations. The particular data
structure chosen largely depends on the frequency of the operation that needs to be
performed on the data structure.
Traversing - print all the array elements one by one.
Searching - Searches an element using the given index or by the value.
Insertion - Adds an element at the given index.
Deletion - Deletes an element at the given index.
Update - Updates an element at the given index.
Insertion
- Insert operation is to insert one or more data elements into an array. Based on the
requirement, a new element can be added at the beginning, end, or any given index of array.
Algorithm: Let Array be a linear unordered array of MAX elements.

Declaring Arrays
*To declare an array in C++, the programmer specifies the type of the elements and the
number of elements required by an array as follows −
*This is called a single-dimension array. The arraySize must be an integer constant greater
than zero and type can be any valid C++ data type. For example, to declare a 10-element array
called balance of type double, use this statement −
type arrayName [ arraySize ];
double balance[10];
Initializing Arrays
you can initialize C++ array elements either one by one or using a single statement as follows −
The number of values between braces { } can not be larger than the number of elements that
we declare for
the array between square brackets [ ]. Following is an example to assign a single element of
the array −

If you omit the size of the array, an array just big enough to hold the initialization is created.
Therefore, if
you write −
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};

You might also like