Data Structures and Algorithms
Data Structures and Algorithms
P
R
E
P
R
E
D
N
E
P
A
L
2
P
R
E Data
P
A
R
E ⚫ Data are the raw facts that needs to be processed to form
D
an information.
B
Y
S AND
U
L
A
V ⚫ Computeris an electronic devicethat storesand processdata in the
N
formof binary digits.
E
P
A
L
3 11/14/2022 10:59AM
P
R
E How Computer Stores Data?
P
A
R
E ⚫ Bit (i.e. Binary Digit) is the smallest unit of computer storage.
D
B
Y ⚫ It contains either 0 or 1.
S
U
L ⚫ 1 𝑏𝑖𝑡 × 8 = 1 𝑏𝑦𝑡𝑒
A
V
N
E
P ⚫ 1 𝑏𝑦𝑡𝑒 × 1024 = 1 𝐾𝑖𝑙𝑜𝐵𝑦𝑡𝑒
A
L
B
Y
S
U
L
A
V
N
E
P
A
L Imagine 1 squareis equal to 1 byte
5 11/14/2022 10:59AM
P
R
E Computer Storage
P
A
R
E
D
B
Y
S
U
L
A
V
N
E
P
A
L Imagine if there arethousands of numbers stored in computer
6 11/14/2022 10:59AM
P
R
E Data Structure
P
A
R
E
D
B
Y
N
E
P
A
L
7 11/14/2022 10:59AM
P
R
E Computer Storage
P
A
R
E
D
B
Y
S
U
L
A
V
N
E
P
A
L This is howdata in an array is stored in computer.
8 11/14/2022 10:59AM
P
R
E Data Structure
P
A
R
⚫ Data aresimply value or set of values.
E
D
B
⚫ Adata itemrefersto a single unit of values.
Y
B
Y ⚫ What operations we can performon that data?
S
U ⚫ The algorithms for all those operations.
L
A
V
⚫ Howtime and space efficient those algorithms are?
N
E
P
A
L
11 11/14/2022 10:59AM
P
R
E Classification of Data Structure
P
A
R
E
D
B
Y
S
U
L
A
V
N
E
P
A
L
12 11/14/2022 10:59AM
P
R
E Classification of Data Structure
P
A
R
⚫ Primitivedata structures
E ⚫ Fundamental (built-in) data types which are supported by a
D
programming language.
B
Y
⚫ Somebasicdata types are integer,character,Boolean,real,etc.
S
U
L
A
⚫ Theterms‘data type’, ‘basicdatatypes’, and‘primitive datatypes’are
V often usedinterchangeably.
N
E
P
A
L
13 11/14/2022 10:59AM
P
R
E Classification of Data Structure
P
A
R
⚫ Non-primitive data structures
E ⚫ Those data structures which are created using primitive data structures;
D
i.e. user-defined data type.
B
Y
⚫ Somenon-primitive data types are linked list,stack,tree,graph,etc.
S
U
L
A
⚫ Non-primitive data structures can be further classified into
V two categories:
N ⚫ Lineardata structure
E ⚫ Non-lineardata structure
P
A
L
14 11/14/2022 10:59AM
P
R
E Classification of Data Structure
P
A
R
⚫ Linear data structure
E ⚫ Elements of a data structure arestored in a linear or sequential order.
D
B
Y ⚫ Forexample:array,linked list,stack,queue,etc.
S
U ⚫ Linear data structures can berepresented in memoryin two different
L
A
ways:
V ⚫ Onewayis to havealinear relationship betweenelementsbymeansof sequential
memory locations (static memory location).
N
E
P ⚫ Theotherwayistohavealinearrelationshipbetweenelementsbymeansoflinks
A
L
(dynamic memory location).
15 11/14/2022 10:59AM
P
R
E Classification of Data Structure
P
A
R
⚫ Non-linear data structure
E ⚫ Elements of a data structure are not stored in a linear or sequential
D
order.
B
Y
⚫ The relationship of adjacencyis not maintained betweenelementsof a
S
U non-linear data structure.
L
A
V ⚫ Examples includetreeand graph.
N
E
P
A
L
16 11/14/2022 10:59AM
P
R
E Classification of Data Structure
P
A ⚫ Array
R ⚫ Lineardatastructurethatcollectselementsofthesamedata type and stores them in contiguous and adjacent
E memorylocations.
D
⚫ Linked list
B ⚫ Asequenceof data structureswhichareconnectedtogethervia links.Eachlink containsa connection to another
link.
Y
S ⚫ Stack
U ⚫ Linear data structurethat follows the principle of Last In First Out (LIFO).
L
A ⚫ Queue
V ⚫ Linear data structurethat follows the principle of First In First Out (FIFO).
N ⚫ Tree
E ⚫ Non-linear hierarchical data structuredefined as a collectionof nodes whichare connectedbyedges.
P
A ⚫ Graph
L ⚫ Non-linear hierarchical data structurethat consistsof a finite set of nodes and a set of edgesconnectingthem.
17 11/14/2022 10:59AM
P
R
E Operations on Data Structure
P
A ⚫ Mandatory Operations
R
⚫ Creating
E
D
B
⚫ Inserting
Y
S
U ⚫ Traversing
L
A
V
⚫ Updating
N
E
P
A ⚫ Deleting
L
18 11/14/2022 10:59AM
P
R
E Operations on Data Structure
P
A ⚫ Non-mandatory Operations
R
E ⚫ Searching
D ⚫ Used to find locationof one ormoredata items that satisfy the given constraint.
B
Y ⚫ Sorting
S ⚫ Used to arrangedata in someorderlike ascending ordescending.
U
L
A ⚫ Merging
V ⚫ Usedto combinelistsoftwosorteddataitemstoform a single list of sorted data
item.
N
E
P
⚫ Splitting
A
L ⚫ Used to separateasinglelist ofsorted data itemto formalists oftwodifferent
sorted data items.
19 11/14/2022 10:59AM
P
R
E Data Types
P
A
R
⚫ A data type, in programming, is a classification that specifies which
E type of value a variable has and what type of mathematical,
D
relational or logical operations can be applied to it without causing
B an error.
Y
S
U ⚫ An integer,for example,is a data type used to classify whole numbers
L
A and a string is a data type that is used to classify text.
V
N
E ⚫ For example,
P
⚫ Csupports int, float, char, double,etc.
A
L ⚫ Pythonsupports String,List,Tuple,etc.
21 ⚫ . . . and soon. 11/14/2022 10:59AM
P
R
E Data Types
P
A
R
⚫ Primitive
E ⚫ Basic building block (int, float,char, Boolean,etc.)
D
B
Y ⚫ Composite
S
⚫ Any data type (array, string, struct, etc.) composed of primitives or
U composite types.
L
A
V
⚫ Abstract
N ⚫ Data type that is defined by its behavior (tuple,set,stack,queue,graph,
E
P etc.)
A
L
22 11/14/2022 10:59AM
P
R
E Data Types
P
A ⚫ In a broad sense,there are three types of data types:
R
E ⚫ Fundamental (Basics) data types
D ⚫ Predefineddata types whichareusedbythe programmerdirectly to storeonly one
B
value asper requirement;i.e.integer type,charactertype,etc.
Y
S
⚫ Derived data types
U ⚫ Derivedusing built in datatypewhich aredesignedbythe programmer to store
L multiple valuesof sametypeas per their requirement.
A
⚫ Forexample:array,functions,pointers,etc.
V
N
⚫ User-defined data types
E
P ⚫ Derived using built-in data types which are wrapped into a single data type to
A store multiple values of either same type or different type or both as per their
L requirement.
⚫ For example:class, structure,etc.
23 11/14/2022 10:59AM
P
R
E Data Types
P
A
R
E
D
B
Y
S
U
L
A
V
N
E
P
A
L
23 11/14/2022 10:59AM
P
R
E Abstract Data Types
P
A ⚫ AbstractDataTypes(ADT)areentities that aredefinitions ofdata
R
E and operations but do not have implementation details.
D
N
E what to do by hiding how to do it !!
P
A
L
25 11/14/2022 10:59AM
P
R
E Abstract Data Types
P
A
R
E
D
B
Y
S
U
L
A
V
N
E
P
A
L
26 11/14/2022 10:59AM
P
R
E Abstract Data Types
P
A
R
E
D
B
Y
S
U
L
A
V
N
E
P
A
L
27 11/14/2022 10:59AM
P
R
E Dynamic Memory Allocation in C
P
A ⚫ The process of allocating and freeing memory at run time is known as
R
E dynamic memory allocation.
D
N
E
P
A
L
30 11/14/2022 10:59AM
P
R
E Dynamic Memory Allocation in C
P
A ⚫ Since an array name is actually a pointer to the first element within
R
E the array, it is possible to define the array as a pointer variable rather
D than as a conventional array.
B
Y
⚫ While defining conventional array, system reserves fixed block of
S memory at the beginning of program execution which is inefficient
U
L but this does not occur if the array is represented in terms of a
A pointer variable.
V
N
E ⚫ The use of pointer variable to represent an array requires sometype of
P initial memory assignment before the array elements are processed.
A
L
31
⚫ This is known as DynamicMemoryAllocation (DMA). 11/14/2022 10:59AM
P
R
E Dynamic Memory Allocation in C
P
A ⚫ At execution time, a program can request more memory froma free memory
R
E pool and freesif not required usingDMA.
D
N
E ⚫ A memory space equivalent to 100 times the size of an integer is
P
A
reserved and the address of the first byte of the memory allocated is
L assigned to the pointer x of type int. (i.e. x refers to the first address
34
of allocated memory). 11/14/2022 10:59AM
P
R
E DMA in C – malloc()
P
A void main()
R
#include<stdio.h> {
E #include<stdlib.h> struct Emp *ptr;
D ptr=(struct Emp*)malloc(sizeof(struct Emp));
struct Emp if(ptr==NULL)
B {
Y { printf("Memory allocation failed\n");
}
S int eno; else
U {
L char ename[20]; printf("Enter Employee Number:\ n");
A scanf("%d",&ptr->eno);
V float esal; printf("Enter Employee Name:\ n");
scanf("%s",ptr->ename);
N }; printf("Enter Employee Salary:\ n");
E scanf("%f",&ptr->esal);
P printf("\nEmployee Number = %d\nEmployee Name =
%s\nEmployee Salary = %f",ptr->eno,ptr-
A >ename,ptr->esal);
L }
}
35 11/14/2022 10:59AM
P
R
E DMA in C – calloc()
P
A ⚫ Thefunction providesaccessto the Cmemory heap, whichisavailablefordynamicallocationof
R variable-sized blocks of memory.
E
D ⚫ Unlike malloc(),it accepts two arguments:no_of_blocks and size_of_each_block specifies the size of
each item.
B
Y
⚫ Thefunction callocallocatesmultiple blocks of storage, each of the same size and then sets all bytes
to zero.
S
U
L ⚫ Calloc initializes all bytes in the allocatedblock to zero.
A ptr=(data_type*) calloc(no_of_blocks,size_of_each_block);
V
⚫ For example:
N
x=(int*) calloc(5,10* sizeof(int)); OR
E
x=(int*) calloc(5,20);
P
A
L ⚫ The above statement allocatescontiguous space for 5 blocks,each of size 20 bytes.i.e.wecan store 5
arrays, each of 10 elements of integertypes.
36 11/14/2022 10:59AM
P
R
E DMA in C – calloc()
P
A #include<stdio.h>
R #include<stdlib.h>
E void main()
D {
int n,*arr, i;
B printf("Entersizeof anarray:\ n");
Y
scanf("%d",&n);
arr=(int*)calloc(n,sizeof(int));
S
U if(arr==NULL)
L printf("MemoryallocationFAILED!!");
A else
V {
printf("Array elementsare:\ n");
N for(i=0;i<n;i++)
E {
P
printf("%d\n",*(arr+i));
A
L }
}
37
} 11/14/2022 10:59AM
P
R
E DMA in C – realloc()
P
A ⚫ This function is used to modify the size of previouslyallocated space.
R
E
D ⚫ Sometimesthepreviouslyallocatedmemoryisnotsufficient; weneed additional
space and sometimethe allocated memoryis much largerthan necessary.
B
Y
⚫ Wecanchange memorysize alreadyallocated with the help of function realloc().
S
U
L ⚫ If the original allocation is done bythe statement: ptr=malloc(size);
A
V
⚫ Then, reallocation of space may be done by the statement:
N ptr=realloc(ptr,newsize);
E
P ⚫ This function allocates a new memory space of size newsize to the pointer variable
A
L
ptr and returns a pointer to the first byte of the new memory block and on failure
the function return NULL.
38 11/14/2022 10:59AM
P
R
E DMA in C – realloc()
P
A #include<stdio.h>
R #include<stdlib.h>
E void main()
D {
int n, *arr,i;
B printf("Enter size:\n");
Y scanf("%d",&n);
arr=(int*)calloc(n,sizeof(int));
S n++;
U arr=(int*)realloc(arr,n*sizeof(int));
L if(arr==NULL)
A printf("Memory allocation FAILED!!");
V else
{
N printf("Array elements are:\n");
E for(i=0;i<n;i++)
P {
A printf("%d\n",*(arr+i));
L }
}
39 } 11/14/2022 10:59AM
P
R
E DMA in C – free()
P
A
R
⚫ Freespreviously allocated space by calloc,malloc or realloc functions.
E
D
⚫ The memory dynamically allocated is not returned to the system
B
Y until the programmer returns the memory explicitly. This can be done
S using free() function.
U
L
A
V
⚫ This function is used to releasethe space when not required.
N
E ⚫ Syntax:
P
A free(ptr);
L
40 11/14/2022 10:59AM
P
R
E DMA in C – free()
P
A #include<stdlib.h>
R
E #include <stdio.h>
D int main()
B {
Y int* ptr = malloc(10 * sizeof(*ptr));
S if (ptr != NULL)
U {
L
A *(ptr + 2) = 50;
V printf("Valueof the 3rd integer is %d\ n",*(ptr + 2));
N }
E //*(ptr + 2) = 50;
P
A free(ptr);
L printf("Valueof the 3rdinteger is %d",*(ptr + 2));
41
} 11/14/2022 10:59AM
P
R
E Algorithm
P
A ⚫ Algorithm is a finite set of instructions or logic, written in order,to
R
E accomplish a certain predefined task.
D
B ⚫ Algorithm is not the complete code or program, it is just the core logic
Y (solution) of a problem, which can be expressed either as an informal high
S level description as pseudocode or usinga flowchart.
U
L
A ⚫ Every algorithmmust satisfy the following properties:
V
⚫ Input
N ⚫ Output
E ⚫ Definiteness
P
A ⚫ Finiteness
L ⚫ Correctness
⚫ Effectiveness 11/14/2022 10:59AM
42
P
R
E Algorithm
P
A ⚫ Input
R ⚫ There should be 0 or more inputs suppliedexternally to the algorithm.
E
D
⚫ Output
B ⚫ There should beat least 1 output obtained.
Y
S ⚫ Definiteness
U ⚫ Every step of the algorithmshould be clearand well defined.
L
A ⚫ Finiteness
V
⚫ The algorithm should havefinite numberof steps.
N
E ⚫ Correctness
P ⚫ Every step of the algorithmmust generate a correct output.
A
L
⚫ Effectiveness
43 ⚫ Each step must be carried out in a finite time. 11/14/2022 10:59AM
P
R
E Algorithm
P
A
R
⚫ Design an algorithm to add two numbers and display the result:
E ⚫ Start
D
⚫ Declare three integers num1,num2,and num3
B
Y ⚫ Define valuesof num1 and num2
⚫ Add values of num1 and num2 and store the value to num3
S
U ⚫ Print num3
L
A ⚫ Stop
V
N
E ⚫ An algorithm is said to beefficient and fast, it takes less time to
P execute and consumes less memory space.
A
L
44 11/14/2022 10:59AM
P
R
E Algorithm Complexity
P
A ⚫ Analyzing analgorithm meansdetermining the amountofresources
R
E (such as time and memory) neededto execute it.
D
B
Y 𝑇 𝑃 = 𝑐 + 𝑡𝑝 𝑖𝑛𝑠𝑡𝑎𝑛𝑐𝑒
S
U Where, 𝑇 𝑃 is the time complexity of the program 𝑃, 𝑐 is the compile
L
A time, and 𝑡𝑝 𝑖𝑛𝑠𝑡𝑎𝑛𝑐𝑒 is the run time.
V
N
E
P
A
L
45 11/14/2022 10:59AM
P
R
E Time Complexity of an Algorithm
P
A
R
⚫ Algorithm:
E
D
sum(a[],n) // 0
B
Y { // 0
s=0; // 1
S
U for i=1 to n // n+1
L
A s=s+a[i]; // n
V // 1
return s;
N } // 0
E
P
A
L Time Complexity = 1+n+1+n+1 = 2n+3
46 11/14/2022 10:59AM
P
R
E Space Complexity of an Algorithm
P
A
R
⚫ Space complexity is defined as the amount of space and memory
E requiredby an algorithm to solve the problem.
D
B
Y 𝑆 𝑃 = 𝑐 + 𝑠𝑝 𝑖𝑛𝑠𝑡𝑎𝑛𝑐𝑒
S
U Where, 𝑆 𝑃 is the spacecomplexityof the program 𝑃, 𝑐 is the fixed
L
A part,and 𝑠𝑝 𝑖𝑛𝑠𝑡𝑎𝑛𝑐𝑒 is the variable part.
V
N
E
P
A
L
47 11/14/2022 10:59AM
P
R
E Space Complexity of an Algorithm
P
A
R
⚫ Algorithm:
E
D
abc(a,b,c)
B
Y {
return a+b+b*c+(a+b+c)/(a+b)+40;
S
U }
L
A
V
Forevery instance,only 3 variables are required to store variables a,b,and
N c.
E
P
A Total Space Complexity = 1+1+1 = 3
L
48 11/14/2022 10:59AM
P
R
E Space Complexity of an Algorithm
P
A
R
⚫ Algorithm:
E
D
sum(a[],n)
B
Y {
s=0;
S
U for i=1 to n
L
A s=s+a[i];
V
return s;
N }
E
P Tostorea[] = n space; to storei, s, &n = 3 space
A
L
Space Complexity = n+3
50 11/14/2022 10:59AM
P
R
E Algorithm Complexity
P
A
R
⚫ Worst-case running time
E ⚫ The worst case running time of an algorithm is an upper bound on the
D
running time for any input.
B
Y
⚫ Therefore, havingthe knowledgeofworstcaserunning timegivesusan
S
U assurance that the algorithm will never gobeyond this time limit.
L
A
V
N
E
P
A
L
50 11/14/2022 10:59AM
P
R
E Algorithm Complexity
P
A
R
⚫ Average-case running time
E ⚫ The average case running time of an algorithm is an estimate of the
D
running time for an‘average’input.
B
Y
⚫ It specifiestheexpectedbehaviorofthealgorithmwhentheinput is
S
U randomly drawnfrom a given distribution.
L
A
V ⚫ Averagecase running time assumes that all inputsofagivensizeare
N
equally likely.
E
P
A
L
51 11/14/2022 10:59AM
P
R
E Algorithm Complexity
P
A
R
⚫ Best-case running time
E ⚫ The term best case performance is used to analyze an algorithm under
D
optimal conditions.
B
Y
⚫ Forexample,the best casefor a simple linear searchon an arrayoccurs
S
U when the desired element is the first in the list.
L
A
V
N
E
P
A
L
52 11/14/2022 10:59AM
P
R
E Asymptotic Notation
P
A ⚫ Asymptotic analysis of an algorithm refers defining the
R
E to mathematical framing of its run-time
D performance.
B ⚫ Asymptotic analysis is input bound; i.e. if there is no input to the
Y
algorithm, it is concluded to work in a constraint time. Other than
S the“input”all other factorsareconsidered constant.
U
L
A
V ⚫ Asymptotic analysis refers to computing the running time of any
operation in mathematical units of computation.
N
E
P
A ⚫ Therunning timeofalgorithm isdefinedasthetimeneededbyan
L algorithm in orderto deliver its output whenpresentedwith logical
54
input. 11/14/2022 10:59AM
P
R
E Asymptotic Notation
P
A
R
⚫ In asymptotic notation,algorithm is treated as a function.
E
D
B
⚫ Usually,the time required by an algorithm falls under three types:
Y
N
E ⚫ Worst Case – maximum timerequired for programexecution
P
A
L
54 11/14/2022 10:59AM
P
R
E Types of Asymptotic Notation
P
A
R
⚫ Followingarethe commonly usedasymptoticnotationsto calculate
E the running time complexity of an algorithm:
D
B
Y ⚫ 𝑂 Notation (Big – ONotation)
S
U ⚫ Ω Notation (Big – Omega Notation)
L
A
V
⚫ 𝜃 Notation (Theta Notation)
N
E
P
A
L
55 11/14/2022 10:59AM
P
R
E Types of Asymptotic Notation
P
A
R
Big – O Notation (𝑶 Notation)
E
D
B
⚫ To analyzecode performance
Y
56 11/14/2022 10:59AM
P
R
E Types of Asymptotic Notation
P
A Big – O Notation (𝑶 Notation)
R
E
D
⚫ Forexample:writeafunction to calculatesumof all numbersfrom1 to
B that number (n) including itself.
Y addUpTo(n) addUpTo(n)
S { {
U
L let sum = 0; return 0.5 * n * (n+1) ;
A
V
for (i = 1;i <= n;i++) }
sum += i;
N
E return sum;
P
A }
L ⚫ Now,which one is better in terms of execution speed (time complexity) and
58
memory efficient (space complexity)? 11/14/2022 10:59AM
P
R
E Types of Asymptotic Notation
P
A
R
Big – O Notation (𝑶 Notation)
E
D
⚫ Number of Operations: addUpTo(n)
B
Y ⚫ Assignment = 1 + 1 + n + n {
S ⚫ Addition = n + n let sum = 0;
U
L
⚫ Comparison = n for (i = 1;i <= n;i++)
A sum += i;
V
⚫ Total = 5n + 2 return sum;
N
E }
P
A
L
58 11/14/2022 10:59AM
P
R
E Types of Asymptotic Notation
P
A
R
Big – O Notation (𝑶 Notation)
E
D
⚫ Number of Operations: addUpTo(n)
B
Y ⚫ Addition= 1 {
S ⚫ Multiplication = 2 return 0.5 * n * (n+1) ;
U
L }
A ⚫ Total = 0n + 3 (only 3)
V
N
E
P
A
L
59 11/14/2022 10:59AM
P
R
E Types of Asymptotic Notation
P
A
R
Big – O Notation (𝑶 Notation)
E
D
B
⚫ It formalizes the fuzzy counting method.
Y
N
E
P
A
L
61 11/14/2022 10:59AM
P
R
E Types of Asymptotic Notation
P
A
R
Big – O Notation (𝑶 Notation)
E
D
⚫ Number of Operations: addUpTo(n)
B
Y ⚫ Assignment = 1 + 1 + n + n {
S ⚫ Addition = n + n let sum = 0;
U
L
⚫ Comparison = n for (i = 1;i <= n;i++)
A sum += i;
V
⚫ Total = 5n + 2 return sum;
N
E }
P
A
L
TIME COMPLEXITY = 𝑶 𝒏
62 11/14/2022 10:59AM
P
R
E Types of Asymptotic Notation
P
A
R
Big – O Notation (𝑶 Notation)
E
D
⚫ Number of Operations: addUpTo(n)
B
Y ⚫ Addition= 1 {
S ⚫ Multiplication = 2 return 0.5 * n * (n+1) ;
U
L }
A ⚫ Total = 0n + 3 (only 3)
V
N
E
P
A TIME COMPLEXITY = 𝑶 𝟏
L
63 11/14/2022 10:59AM
P
R
E Types of Asymptotic Notation
P
A
R
Big – O Notation (𝑶 Notation)
E
D ⚫ Big – O notation represents the upper
bound of the running time of an
B algorithm.
Y
B
⚫ Example:Suppose 𝑓 𝑛 = 4𝑛2 + 5𝑛 + 3. Is 𝑓 𝑛 is 𝑂 𝑛2 ?
Y
S ⚫ Solution:
U
L 𝑓 𝑛 = 4𝑛2 + 5𝑛 + 3
A
V
≤ 4𝑛2 + 5𝑛 + 3, 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑛 > 0
≤ 4𝑛2 + 5𝑛2 + 3𝑛2 , 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑛 > 1
N
E
≤ 12𝑛2 , 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑛 > 1
P 4𝑛2 + 5𝑛 + 3 ≤ 12𝑛2
A
L
Hence,𝒇 𝒏 = 𝑶 𝒏𝟐
65 11/14/2022 10:59AM
P
R
E Types of Asymptotic Notation
P
A
R
Big – Omega Notation (Big Ω Notation)
E
D ⚫ Big – Omega notation represents the
lower bound of the running time of an
B algorithm.
Y
B
Y
S
U
L
A
V
N
E
P
A
L
68 11/14/2022 10:59AM
P
R
E
P
A
R
E
D
B
Y
S STACK
U
L
A
V
N
E
P
A
L
69 11/14/2022 10:59AM
P
R
E Stack
P
A ⚫ Stack is a simple algorithm that works based on a rule Last In First Out
R (LIFO).
E
D
⚫ Elements are added to and removedfrom the top of the stack calledTOP.
B
Y
⚫ The last element addedis the first to beremoved.
S
U
L
⚫ Push() function is used to insertnew elements into the stack and Pop()
A
V function is usedto remove an element from the stack.
N
E ⚫ Both insertion and removal are allowed at only one end of stack.i.e.Top.
P
A
L ⚫ Stack is said to bein Overflowstate whenit is completely full and is said
to bein Underflowstate if it is completely empty.
70 11/14/2022 10:59AM
P
R
E Stack
P
A ⚫ Consider an example of plates stacked overoneanother in the reception party.
R
E
D ⚫ In suchastack, only the top plate is visible andaccessibleto the user,all other
plates remain hidden.
B
Y
⚫ As new plates are added (pushed), each new plate becomes the top of the stack,
S and hideseachplate below.
U
L
A ⚫ Asplates areremoved(popped) from the stack,they can beused,and secondplate
V becomesthe topof the stack.
N
E ⚫ Twoimportant principles areillustrated bythis metaphor.
P ⚫ Thelast in first out principle
A ⚫ Contents of the stack arehidden,only the top plate is visible – to seewhat is onthe
L third plate, the first and secondplateswill haveto beremoved.
71 11/14/2022 10:59AM
P
R
E Stack
P
A
R
E
D
B
Y
S
U
L
A
V
N
E
P
A
L
72 11/14/2022 10:59AM
P
R
E Stack Operations
P
A
R
⚫ Creation Operation ⚫ isEmpty Operation
E ⚫ To create an empty stack ⚫ To check whether the stack is
D
empty or not
B
Y ⚫ Push Operation
⚫ Toaddelements at the top of a ⚫ isFull Operation
S
stack
U ⚫ To check whether the stack is
L
A full or not
V ⚫ Pop Operation
N ⚫ To removeor delete the top element
E fromthe stack ⚫ Size Operation
P
A
⚫ To check the sizeof stack
L
⚫ Traverse Operation
76 ⚫ To display all the elements of stack 11/14/2022 10:59AM
P
R
E Stack as an ADT
P
A ⚫ Astack of elements of type T is a finite sequence of elements of T togetherwith the
R operations:
E ⚫ createEmptyStack(S)
D ⚫ Createor makestack S asan empty stack.
B
⚫ push(S,x)
Y
⚫ Insert x at oneendof thestack S,calledtop.
S
U ⚫ top(S)
L ⚫ If stack S is notempty;thenretrieve the element at its top.
A
V ⚫ pop(S)
⚫ If stack S is not empty;thendeletethe element at its top.
N
E
⚫ isFull(S)
P
⚫ Determine if S is full ornot.Return true if S is full;return falseotherwise.
A
L
⚫ isEmpty(S)
77 ⚫ Determine if S is empty or not.Return true if S is empty;return falseotherwise. 11/14/2022 10:59AM
P
R
E Stack Implementation
P
A
R
Creation of Stack
E
D
B
Y #define CAPACITY 5
S
U
L int top = -1;
A
V
N int initStack[CAPACITY];
E
P
A
L
75 11/14/2022 10:59AM
P
R
E Stack Implementation
P
A
R
push operation
E
D void push(int element)
B {
Y if(isFull())
{
S
U
printf(“Stack is full”);
L }
A else
V
{
N top++;
E initStack[top]=element;
P printf(“%d”inserted\ n”,element);
A }
L
}
76 11/14/2022 10:59AM
P
R
E Stack Implementation
P
A
R
isFull Operation
E
D
int isFull()
B
Y {
if(top == CAPACITY- 1)
S
U {
L return 1;
A
V }
else
N
E {
P return 0;
A
L }
} 11/14/2022 10:59AM
80
P
R
E Stack Implementation
P
A
R
pop operation
E
D int pop()
B {
Y int element;
if(isEmpty())
S
U
{
L return 0;
A }
V else
N {
E return initStack[top--];
P }
A
L
return top;
}
81 11/14/2022 10:59AM
P
R
E Stack Implementation
P
A
R
isEmpty Operation
E
D
int isEmpty()
B
Y {
if(top == -1)
S
U {
L return 1;
A
V }
else
N
E {
P return 0;
A
L }
} 11/14/2022 10:59AM
79
P
R
E Stack Implementation
P
A
R
peek operation
E
D
int peek()
B
Y {
if(isEmpty())
S
U {
L return 0;
A
V }
else
N
E {
P return initStack[top];
A
L }
} 11/14/2022 10:59AM
80
P
R
E Stack Implementation
P
A
R
traverse operation
E
D void traverse()
{
B
Y
int i;
if(isEmpty())
S {
U printf(“NoElements\ n”);
L }
A else
V
{
N printf(“Stack Elements are:\n”);
E for(i=0;i<=top;i++)
P {
A printf(“%d\n”,initStack[i]);
L }
}
84 11/14/2022 10:59AM
}
P
R
E Stack Application
P
A ⚫ Can beused forexpressionevaluation. ⚫ To keep the page-visited history in a
R webbrowser.
E
D ⚫ Can be used to check parenthesis
⚫ To perform the undo sequence in
B matching in an expression.
a text editor.
Y
S
⚫ Can be used for conversion from one ⚫ Usedin recursion
U formof expressionto another.
L
A ⚫ To pass the parameters between
V ⚫ Can beused for memorymanagement. the functionsin a Cprogram.
N
E ⚫ Stack data structures are used in ⚫ Can be used as an auxiliary data
P backtracking problems. structure for implementing algorithms
A
L
⚫ Can be used as a component of other
⚫ To evaluate the expressions (prefix, data structures.
86 11/14/2022 10:59AM
postfix).
P
R
E Arithmetic Expression
P
A
R
⚫ Infix Notation
E ⚫ The expression which is in normal format; i.e. the operator lies in
D
between the operand.
B
⚫ For example:𝐴 + 𝐵, 𝐴 ∗ 𝐵, 𝐴/𝐵, etc.
Y
S
U ⚫ Prefix Notation or Polish Notation
L
A ⚫ The operatorcomesfirst followedbythe operands.
V
⚫ For example:+𝐴𝐵,∗ 𝐴𝐵,/𝐴𝐵, etc.
N
E
P
A
⚫ Postfix (Suffix) Notation or Reverse Polish Notation
L ⚫ The operand comesfirst followedby the operator.
87 ⚫ For example:𝐴𝐵+, 𝐴𝐵 ∗, 𝐴𝐵/, etc. 11/14/2022 10:59AM
P
R
E Precedence Level (Priorities)
P
A
R
⚫ Highest – Exponentiation (^)
E
D
B
⚫ Next Highest – Multiplication (*) and Division (/)
Y
N
E
P
A
L
84 11/14/2022 10:59AM
P
R
E
Algorithm – Infix to Postfix Conversion
P
A ⚫ Scaninput string from left to right character by character.
R
E ⚫ If the characteris an operand,put it into output stack.
D
⚫ If the character is an operator and operator'sstack is empty,pushoperator into operators' stack.
B
Y
⚫ If the operator'sstack is not empty,there maybefollowingpossibilities:
⚫ If the precedenceof scanned operator is greater than the top most operator of operator's stack,push this operator
S intooperand's stack.
U
L ⚫ If the precedence of scanned operator is less than or equal to the top most operator of operator's stack, pop the
A operators from operand's stack until we find a low precedence operator than the scanned character. Never pop out (
V '(' ) or( ')' ) whatevermaybethe precedencelevel of scannedcharacter.
⚫ If the characteris opening round bracket( '(' ),push it into operator's stack.
N
E
⚫ If the character is closing round bracket ( ')' ), pop out operators from operator's stack untill we find an opening
P bracket('(' ).
A
L ⚫ Nowpop out all the remainingoperatorsfromthe operator's stack and push into output stack.
85 11/14/2022 10:59AM
P
R
E Infix to Postfix Conversion
P
A
R
E
D
B
Y
S
U
L
A
V
N
E
P
A
L
86 11/14/2022 10:59AM
P
R
E
Algorithm – Infix to Prefix Conversion
P
A
R
⚫ Reversethe infix expression.i.e.A+B*C will becomeC*B+A
E
D
⚫ Obtain the “nearly”postfix expressionof the modified expression.i.e.
B
Y CB*A+
S
U
L ⚫ Reverse the postfix expression. Hence in our example, prefix is
A
V
+A*BC
N
E
P
A
L
87 11/14/2022 10:59AM
P
R
E Infix to Prefix Conversion
P
A
R
E
D
B
Y
S
U
L
A
V
N
E
P
A
L
88 11/14/2022 10:59AM
P
R
E
Algorithm – Postfix to Infix Conversion
P
A ⚫ Scan the given postfix expressionfrom left to right character by character.
R
E
D
⚫ If the character is an operand,push it into the stack.
B
Y
⚫ But if the character is an operator,popthe top two values from stack.
S
U
L ⚫ Concatenate this operator with these two values (2nd
A
V top value+operator+1st top value) to get a new string.
N
E ⚫ Nowpushthis resulting string back into the stack.
P
A
L ⚫ Repeat this processuntil the end of postfix expression.Now the value in the
93
stack is the infixexpression. 11/14/2022 10:59AM
P
R
E Postfix to Infix Conversion
P
A
R
E
D
B
Y
S
U
L
A
V
N
E
P
A
L
94 11/14/2022 10:59AM
P
R
E
Algorithm – Prefix to Infix Conversion
P
A ⚫ Scan the given prefix expressionfrom right to left character by character.
R
E
D
⚫ If the character is an operand,push it into the stack.
B
Y
⚫ But if the character is an operator,popthe top two values from stack.
S
U
L ⚫ Concatenate this operator with these two values (1st
A
V top value+operator+2nd top value) to get a new string.
N
E ⚫ Nowpushthis resulting string back into the stack.
P
A
L ⚫ Repeat this process until the end of prefix expression.Now the value in the
95
stack is the desired infix expression. 11/14/2022 10:59AM
P
R
E Prefix to Infix Conversion
P
A
R
E
D
B
Y
S
U
L
A
V
N
E
P
A
L
92 11/14/2022 10:59AM
P
R
E Evaluation of Postfix Expression
P
A
R
E
D
B
Y
S
U
L
A
V
N
E
P
A
L
93 11/14/2022 10:59AM
P
R
E Evaluation of Prefix Expression
P
A
R
E
D
B
Y
S
U
L
A
V
N
E
P
A
L
94 11/14/2022 10:59AM
P
R
E
P
A
R
E
D
B
Y
S QUEUE
U
L
A
V
N
E
P
A
L
95 11/14/2022 10:59AM
P
R
E Queue
P
A ⚫ Queue is a linear list of elements in which deletion of an element can
R
E take place at one end, called the front and insertion can take place at
D the other end,called the rear.
B
Y
⚫ The first element in a queue will bethe first oneto beremovedfrom
S the list.
U
L
A
V ⚫ QueuesarealsocalledFIFO(First In FirstOut), i.e. the data item
stored first will beaccessedfirst.
N
E
P
A ⚫ The process to add an element into queue is called enqueue and the
L process of removal of an element from queue is called dequeue.
96 11/14/2022 10:59AM
P
R
E Queue Representation
P
A
R
⚫ In queue,weaccess both ends for different reasons.
E
D
B
Y
S
U
L
A
V
N
E
P
A
L
97 11/14/2022 10:59AM
P
R
E Stack vs. Queue Representation
P
A
R
E
D
B
Y
S
U
L
A
V
N
E
P
A
L
98 11/14/2022 10:59AM
P
R
E Queue Operations
P
A ⚫ enqueue operation ⚫ isFull operation
R ⚫ It inserts the element at the end
E ⚫ Return true when the queueis full.
D (rear) of the queue.
B
Y
⚫ dequeue operation ⚫ isEmpty operation
⚫ It deletes the element from the ⚫ Return true when the queueis empty.
S front of the queue.
U
L ⚫ viewQueue operation
A ⚫ getFront operation
V ⚫ It returns the element at the front ⚫ Viewall the elements in queue.
N
of the queue.
E
P
⚫ getRear operation
A
L ⚫ It returnsthe element at the end of
the queue.
99 11/14/2022 10:59AM
P
R
E Queue as an ADT
P
A ⚫ Aqueueq of type T is a finite sequenceof elements with the operations:
R ⚫ makeEmpty(q)
E
⚫ To make q as an empty queue
D
B ⚫ isEmpty(q)
Y ⚫ Tocheckwhether the queue qis empty ornot. Return true if qis empty and return false
otherwise.
S
U ⚫ isFull(q)
L ⚫ Tocheck whether the queueq is full ornot.Return true if q is full and returnfalse otherwise.
A
V
⚫ enqueue(q,x)
N ⚫ Toinsert an item x at the rearof the queue,if and only if q is not full.
E
P ⚫ dequeue(q)
A ⚫ Todelete an item from the front of the queueq if and only if q is not empty.
L
⚫ traverse(q)
106 11/14/2022 10:59AM
⚫ Toreadentire queue;i.e.display the content of the queue.
P
R
E Queue Implementation
P
A
R
Creation of Queue
E
D
B
#define CAPACITY 5
Y
S int queue[CAPACITY];
U
L
A
V int front = 0;
N
E
P int rear = 0;
A
L
110
} 11/14/2022 10:59AM
P
R
E Why Circular Queue?
P
A ⚫ Implementation of a linear queuebringsthe drawbackof memorywastage.
R
E
D ⚫ However, memory is a crucial resource that you should always protect
by analyzing all the implications while designing algorithms orsolutions.
B
Y
⚫ In the case of a linear queue, when the rear pointer reaches the maxSize of a
S queue, there might be a possibility that after certain number of dequeue()
U operations,it will createempty space at the start of a queue.
L
A
V ⚫ Toutilize the empty space,eachdequeue() operation needsto befollowed by
shifting all elementsoncetowardsfront.
N
E
P ⚫ This processwill result in moretimecomplexity.
A
L
⚫ Hence, experts introduced the concept of the circular queue to overcome this
112
limitation. 11/14/2022 10:59AM
P
R
E Circular Queue
P
A
R
⚫ Acircular queue is an extended version of a linear queue as it follows
E the First In First Out (FIFO) principle with the exception that it
D
connects the last node of a queue to its first by forming a circular
B link.
Y
S
U ⚫ It is also called a Ring Buffer.
L
A
V
N
E
P
A
L
B
⚫ Rear – used to get the end element of the circular queue
Y
S
⚫ Step 3:Checkif the queueis empty,and set both front and rearto 0
U
L
⚫ Step 4: If 𝑟𝑒𝑎𝑟 = 𝑆𝐼𝑍𝐸 – 1 and 𝑓𝑟𝑜𝑛𝑡 ! = 0 (rearis at the endof
A
V the queue and front is not at 0th index),then set 𝑟𝑒𝑎𝑟 = 0
N
E ⚫ Step 5:Otherwise,set 𝑟𝑒𝑎𝑟 = (𝑟𝑒𝑎𝑟 + 1)%𝑆𝐼𝑍𝐸
P
A
⚫ Step 6:Insert the element into the queue(𝑞𝑢𝑒𝑢𝑒[𝑟𝑒𝑎𝑟] = 𝑥)
L
B
Y
S
U
L
A
V
N
E
P
A
L
B
Y
S
U
L
A
V
N
E
P
A
L
S
⚫ Step 3:Set 𝑒𝑙𝑒𝑚𝑒𝑛𝑡 = 𝑞𝑢𝑒𝑢𝑒[𝑓𝑟𝑜𝑛𝑡]
U
L ⚫ Step 4:If there is only one element in a queue,set both front and rear to -1 (if
A
V 𝑓𝑟𝑜𝑛𝑡 = 𝑟𝑒𝑎𝑟, set 𝑓𝑟𝑜𝑛𝑡 = 𝑟𝑒𝑎𝑟 = −1)
N
E ⚫ Step 5:And if 𝑓𝑟𝑜𝑛𝑡 = 𝑆𝐼𝑍𝐸 – 1 set 𝑓𝑟𝑜𝑛𝑡 = 0
P
A
⚫ Step 6:Otherwise,set 𝑓𝑟𝑜𝑛𝑡 = 𝑓𝑟𝑜𝑛𝑡 + 1
L
B
Y
S
U
L
A
V
N
E
P
A
L
B
Y
S
U
L
A
V
N
E
P
A
L
S
U ⚫ Some of the applications of queuesrelated to computer science are:
L ⚫ Data getting transferredbetweenthe IO buffers (Input Output Buffers)
A
V ⚫ CPUscheduling and Disk scheduling
⚫ Managing shared resources between various processes
N
E ⚫ Jobscheduling algorithms
P ⚫ Round Robin scheduling
A
L ⚫ Recognizinga palindrome
⚫ Traffic system
123 11/14/2022 10:59AM
P
R
E Applications of Queue
P
A ⚫ Buffer in Computer systems
R
E ⚫ Computer systems supply a holding area for maintaining communication
D between two processes,two programs,oreven two systems.
⚫ This memoryareais also knownas a ring buffer.
B
Y
S
⚫ CPU Scheduling
U ⚫ In the Round-Robin Scheduling algorithm, a circular queue is utilized to
L maintain processesthat arein a ready state.
A
V
N
⚫ Traffic System
E ⚫ Circular queue is also utilized in traffic systems that are controlled
P by computers.
A
L ⚫ Each traffic light turns ON in a circular incrementation pattern in a constant
interval of time.
124 11/14/2022 10:59AM
P
R
E Priority Queue
P
A ⚫ Priority Queue is an ADT that
R
E
performs operations on data elements
D per their priority.
B
Y ⚫ The hospital emergency queue is an
ideal real-life example of a priority
S queue.
U
L
A ⚫ In this queue of patients, the patient
V
with the most critical situation is the
N first in a queue, and the patient who
E doesn’t need immediate medical
P attention will beat the last.
A
L
⚫ In this queue, the priority depends on
125 11/14/2022 10:59AM
the medical conditionof the patients.
P
R
E Priority Queue
P
A ⚫ It is highly used in
R
E sorting algorithms.
D
B
⚫ Every element of this queue must be comparable.
Y
S ⚫ It will delete the element with higher priority before the element with
U
L lower priority.
A
V
B
Y
S
U
L
A
V
N
E
P
A
L
B
Y ⚫ If the element with the smallest value has the highest priority,then that
priority queue is called the min priority queue (ascending).
S
U
L
A ⚫ If the element with the higher value hasthe highest priority,then that
V priority queue is called the max priority queue (descending).
N
E
P
A
L
N
E
P
A
L
B
Y
S Linear List
U
L
A
V
N
E
P
A
L
B
Y ⚫ List is a collection of nodes.
S
U
L ⚫ The list is an:
A
V
⚫ Orderedsequenceof data items called elements
⚫ A1,A2,A3, … ,ANis a list of sizeN
N
E ⚫ Sizeof an empty list is 0
P
A ⚫ First elementA1 is called head
L ⚫ Last element AN is called tail
128 11/14/2022 10:59AM
P
R
E List Operations
P
A ⚫ Add – adds a new node
R
E
D ⚫ Set – update the contents of a node
B
Y ⚫ Remove– removesa node
S
U ⚫ isEmpty – reports whether the list is empty
L
A
V ⚫ isFull – reports whether the list is full
N
E ⚫ Initialize – creates/initializes the list
P
A
L ⚫ Destroy – deletes the contents of the list (may be implemented by re-
initializing the list) 11/14/2022 10:59AM
129
P
R
E List Operations
P
A
R
⚫ Initialize(L)
E ⚫ Creates a new empty list named L.
D
B
Y
S ⚫ Add(1, X, L)
U
L ⚫ Addsthe value X to list Lat position 1 (the start of the list is position
A 0),shifting subsequent elements up.
V
N
E ⚫ Set(2, Z, L)
P
A ⚫ Updates the values at position 2 to beZ.
L
B
Y
S ⚫ Get(2, L)
U
L ⚫ Returns the valueof the third node. i.e. C
A
V
N
E
P
⚫ IndexOf(X, L)
A ⚫ Returns the index of the nodewith value X. i.e.1
L
B
Y ⚫ Weshould beable to write/modify elements at any position.
S
U
L ⚫ Weshould beable to readelements at any position.
A
V
N
E
P
A
L
S ⚫ Toadd an element at the end of the list,it canalso bedone at constant time.
U
L
A ⚫ Toaddanelementat anyother position involves shifting up one position of all
V
the subsequentelements,sothe time complexity depends on the size of the list.
N
E
P ⚫ To remove an element from the list, all the subsequent elements should be
A shifted down one position, so the time complexity depends on the size of the
L
list.
136 11/14/2022 10:59AM
P
R
E Dynamic List Structure
P
A
R
⚫ In dynamicdatastructure, thesizeofthestructure is not fixed and
E can bemodified during the operations performedon it.
D
B
Y ⚫ Dynamic data structures are designed to facilitate change of data
S structures in the run time.
U
L
A
V
⚫ Example of dynamic data structure is a Linked List.
N
E
P
A
L
B
Y ⚫ The secondpart contains the addressof the next nodein the list.
S
U
L
A
V
N
E
P
A
L
B
Y ⚫ The first node is called head.
S
U
L ⚫ If the linked list is empty, then value of head is NULL.
A
V
N
E
P
A
L
B
Y
B
Y
S
U
L ⚫ Linked Lists can grow and shrink as needed, unlike arrays, which
A
V
have a fixed size. Linked List can insert a node between other nodes
easily.
N
E
P
A
L
B
Y ⚫ Address of next node can be stored in pointer type variable.
S
U
L ⚫ For example:
A
V
struct node
{
N
E int data;
P
A struct node*next;
L } nodes;
144 11/14/2022 10:59AM
P
R
E Implementation of Linked List
P
A
R
⚫ Memory allocation for node using malloc( ) function:
E node*p;
D
p = (node *) malloc(sizeof(node));
B
Y p -> data = 40;
p ->next = NULL;
S
U
L
A
V
N
E ⚫ It is assumedthat,type of data stored in eachnode is of integer type.
P
A
L
B
Y
⚫ The size of the arrays is fixed. So, we must know the upper limit on the
number of elements in advance. Also, generally, the allocated memory is
S
U
equal to the upper limit irrespectiveof the usage.
L
A
V ⚫ Inserting new element in an array of elements is expensive, because room
N
has to be created for the new elements and to create room existing
E elements haveto beshifted.
P
A
L
B
Y ⚫ Linked list can beused to create dynamic stack and queue which can
S growand shrink at run time.
U
L
A
V
⚫ Linked list can beused to store and process the polynomials
⚫ Coefficientand powerstored as data; and link pointer points to next
N
E termin the polynomial.
P
A
L
B ⚫ Printing LinkedList
Y
R void delete_pos()
E { else
D int i,pos;
{
struct node*temp,*ptr;
B if(start==NULL) ptr=start;
Y { for(i=0;i<pos;i++) { temp=ptr;ptr=ptr->next ;
printf("nThe List is Empty:n"); if(ptr==NULL)
S exit(0);
{
U }
printf("nPosition not Found:n");
else
L
{ return;
A printf("nEnter the positionof the node to bedeleted:t");
V scanf("%d",&pos);
}
if(pos==0) }
N { temp->next =ptr->next ;
E ptr=start;
printf("nThe deletedelement is:%dt",ptr->info );
P start=start->next ;
free(ptr);
A printf("nThe deleted elementis:%dt",ptr->info);
L free(ptr); }
} }
190 } 11/14/2022 10:59AM
P
R
E Types of Linked List
P
A
R
⚫ Singly Linked List
E
D
B
⚫ Doubly Linked List
Y
N
E
P
A
L
B
Y ⚫ The data field is used to storeactual value of that node and the next field is used
to storethe addressof the next node in the sequence.
S
U
L ⚫ In this type of linked list,the link points to the next node in the list and the last
A nodehasa NULLpointer.
V
N
E
P
A typedef struct node
L { int data;
struct node*next;
193 11/14/2022 10:59AM
} node;
P
R
E Operation of Singly Linked List
P
A ⚫ To create a new node, we use the malloc function to dynamically
R
E allocate memory for the newnode.
D
B ⚫ After creating the node,wecan store the newitem in the node using
Y
a pointer to that node.
S
U
L ⚫ Steps requiredto create a node and storing an item:
A
V NodeType *p;
N
p = (NodeType*) malloc(sizeof(NodeType));
E p -> info= 50;
P
A p -> next = NULL;
L
194
Note that p is not a node;instead it is a pointer to a node.11/14/2022 10:59AM
P
R
E Operation of Singly Linked List
P
A
R
⚫ The getNode function:
E ⚫ We can define a function getNode() to allocate the memory for a node
D
dynamically.
B
Y
⚫ It is user-definedfunctionthat return apointer tothenewlycreated
S
U node.
L
A
V NodeType *getNode()
{ NodeType *p;
N
E
p = (NodeType*) malloc(sizeof(NodeType));
P return (p);
A
}
L
N
E
P
A
L
B
Y
S
U
L
A
V
N
E
P Inserting node at the beginning
A
L
B
Y
S
U
L
A
V
N
E
P Inserting node at the end
A
L
B
⚫ Checkwhether list is Empty(head == NULL)
Y
B
Y
S
U
L
A
V
N
E
P Inserting node after a node
A
L
B
Y
S
U
L
A
V
N
E
P Deleting nodefrom the beginning
A
L
N ⚫ If it isTRUE,then set head = NULLand delete temp1 and terminate the function
E ⚫ If it is FALSE, then set temp2 = temp1, and move temp1 to its next node. Repeat
P the same until it reaches to the last node in the list (until temp1 -> next ==
A
L
NULL)
⚫ Finally, set temp2->next = NULLand delete temp1
169 11/14/2022 10:59AM
P
R
E Operation of Singly Linked List
P
A
R
⚫ Deleting Node:
E
D
B
Y
S
U
L
A
V
N
E
P Deleting nodefromthe end
A
L
B
Y
S
U
L
A
V
N
E
P Deleting nodefrom a specific position
A
L
N ⚫ Usually two pointers are maintained to keeptrackof the list,head and tail.
E
P struct node
A { int data;
L struct node*next;
struct node*prev;
211 11/14/2022 10:59AM
};
P
R
E Doubly Linked List
P
A ⚫ In doubly linked list,the first nodemust always bepointed byhead.
R
E
D ⚫ Alwaysthe previous field of the first nodemust beNULL.
B
Y ⚫ Always the next field of the last nodemust beNULL
S
U
L
A
V
N ⚫ Node representation
E typedef struct node
P { int data;
A
L struct node*next;
struct node*prev;
212 }dnode; 11/14/2022 10:59AM
P
R
E Doubly Linked List
P
A
R
⚫ Advantages of DLL over SLL
E ⚫ ADLLcan betraversedin both forwardand backward direction.
D
⚫ The delete operation in DLLis moreefficient if pointer to the node to be
B deleted is given.
Y
⚫ Because we can traverse either way in DLL, we can get the previous node using
S previous pointer
U
L
A
V ⚫ Disadvantages of DLL over SLL
N ⚫ Every nodeof DLLrequire extra spacefor a previous pointer.
E ⚫ All operations require an extra pointer to be maintained. Forexample, in
P
A insertion, we need to modify previous pointers together with next
L pointers.
175 11/14/2022 10:59AM
P
R
E Operation of Doubly Linked List
P
A
R
⚫ Inserting Nodes:
E ⚫ We can use the following steps to insert a new node at beginning of the
D
doubly linked list.
B ⚫ Create a newNode with given valueand newNode -> previous as NULL
Y
B
Y
S
U
L
A
V
N
E
P Inserting node at the beginning
A
L
S
⚫ If it is Not Empty,thendefine twonode pointerstemp1and temp2and initialize temp1 with head.
U
L
⚫ Keep moving the temp1 to its next node until it reaches to the node after which we want to insert the
A newNode (until temp1 -> data is equal to location, here location is the node value after which wewant
V to insert the newNode)
N ⚫ Every time check whether temp1 is reached to last node or not. If it is reached to last node then display
E “Given node is not found in the list. Insertion not possible.” and terminate the function; otherwise, move
P the temp1 to next node.
A
L ⚫ Assign temp1 -> next to temp2, newNode to temp1 -> next, temp1 to newNode -> previous, temp2 to
newNode-> next and newNodeto temp2-> previous
B
Y
S
U
L
A
V
N
E
P
A
L
Inserting node after a node
217 11/14/2022 10:59AM
P
R
E Operation of Doubly Linked List
P
A ⚫ Inserting Nodes:
R
E ⚫ We can use the following steps to insert a new node at the end of the
D doubly linked list.
⚫ Create a newNode with given valueand newNode -> next as NULL
B
Y
⚫ Check whether list is Empty (head == NULL)
S
U
L ⚫ If it is Empty,then assignNULLto newNode -> previous and newNode to head
A
V
⚫ If it is Not Empty,then definea nodepointer temp and initialize with head.
N
E ⚫ Keepmovingthe temp to its next nodeuntil it reachesto the last nodein the list
P
A
(until temp -> next is equal to NULL)
L
⚫ AssignnewNode to temp ->next and temp to newNode -> previous
180 11/14/2022 10:59AM
P
R
E Operation of Doubly Linked List
P
A
R
⚫ Inserting Nodes:
E
D
B
Y
S
U
L
A
V
N
E
P Inserting node at the end
A
L
B
Y
S
U
L
A
V
N
E
P Deleting nodefrom the beginning
A
L
B
Y
S
U
L
A
V
N
E
P Deleting nodefromthe end
A
L
B
Y
S
U
L
A
V
B
⚫ Forwardand backwardnavigation in browsers
Y
N
E
P
A
L
B
Y ⚫ The only difference is that there is noany NULLvalue terminating
S the list.
U
L
A
V
⚫ In fact in the list, everynodepoints to the next nodeand last node
points to the first node,thus forming a circle.
N
E
P
A ⚫ Since it forms a circle with no endto stop it is called as circular
L
linked list.
190 11/14/2022 10:59AM
P
R
E Circular Linked List
P
A
R
⚫ In circularlinked list therecanbenostarting orendingnode, whole
E node can be traversed fromany node.
D
B
Y ⚫ In order to traverse the circular linked list, only once we need
S to traverseentire list until the starting node is not traversedagain.
U
L
A
V
⚫ Acircularlinked list canbeimplemented using both singly linked
list and doubly linked list.
N
E
P
A
L
B
Y Basic structure of singlycircularlinked list
S
U
L
A
V
B
Y ⚫ Circular lists arethe required data structure whenwewant a list to be
accessed in a circle or loop.
S
U
L
A
⚫ Despiteof beingsingly circular linked list wecaneasily traverseto its
V previous node,which is not possiblein singly linked list.
N
E
P
A
L
B
Y ⚫ Reversingof circular list is acomplexascompared to singly ordoubly
lists.
S
U
L
A
⚫ If not traversedcarefully,then wecould end up in an infinite loop.
V
N ⚫ Like singly and doubly lists, circular linked lists also doesn’t support
E
P
direct accessing of elements.
A
L
B
Y ⚫ delete – deletes an element from the start of the list
S
U ⚫ display – displays the list
L
A
V
N
E
P
A
L
B
Y ⚫ That means the amount of data must bespecified at the beginning of
S the implementation itself.
U
L
A
V
⚫ Stack implemented using an array is not suitable, when we don’t
knowthe size of data which we aregoing to use.
N
E
P
A ⚫ Astack data structure can beimplemented by using a linked list data
L
structure.
196 11/14/2022 10:59AM
P
R
E STACK as LINKED LIST
P
A
R
⚫ Thestackimplementedusinglinked list canworkforanunlimited
E number of values.
D
B
Y ⚫ That means, stack implemented using linked list works for
S the variable size of data.
U
L
A
V
⚫ So, there is no need to fix the size at the beginning of
the implementation.
N
E
P
A ⚫ The stack implemented using linked list can organize as many data
L
values as we want.
197 11/14/2022 10:59AM
P
R
E STACK as LINKED LIST
P
A
R
⚫ In linked list implementation of a stack, every new element is inserted
E as ‘top’element; That means every newly inserted element is pointed
D
by‘top’.
B
Y
N
E ⚫ The next field of the first element must bealwaysNULL.
P
A
L
B
Y ⚫ The first inserted nodeis 25
S
U ⚫ The order of elements inserted is:
L
A ⚫ 25
V ⚫ 32
⚫ 50
N
E 99
P
A
L
S
U ⚫ Define a‘Node’structure with two members;data and next.
L
A
V ⚫ Define a Node pointer‘top’and set it to NULL.
N
E
P ⚫ ImplementthemainmethodbydisplayingMenuwith list of operations
A and make suitable function calls in the main method.
L
B
Y ⚫ That means,theamountofdatamustbespecified at thebeginning
S itself.
U
L
A
V
⚫ Queueusing an array is not suitable when wedon’t knowthe size of
data which we aregoing to use.
N
E
P
A ⚫ Aqueuedatastructurecanbeimplementedusingalinked list data
L
structure.
204 11/14/2022 10:59AM
P
R
E QUEUE as LINKED LIST
P
A
R
⚫ The queue which is implemented using a linked list canworkfor an
E unlimited number of values.
D
B
Y ⚫ That means,queue using linked list can work for the variable size of
S data (no need to fix the size at the beginning of the
U implementation).
L
A
V
⚫ The queue implemented using linked list can organize asmanydata
N
E values as we want.
P
A
L
S
U
L
A
V
⚫ In above figure,
N
E ⚫ The last inserted nodeis 50 and it is pointed by‘rear’
P
A ⚫ The first inserted nodeis 10 and it is pointed by‘front’
L
⚫ The order of elements inserted is 10, 15, 22, and 50.
206 11/14/2022 10:59AM
P
R
E QUEUE as LINKED LIST
P
A
R
⚫ Toimplementqueueusinglinked list, weneed to setthefollowing
E things before implementing actual operations:
D
⚫ Include all the header files which are used in the program. And declare
B all the user defined functions.
Y
S
U ⚫ Define a‘Node’structure with two members;data and next.
L
A
V ⚫ Define two Node pointers‘front’and‘rear’and set both to NULL.
N
E
P ⚫ Implement the main methodby displayingMenu of list of operations
A andmake suitablefunction callsin the mainmethodto performuser
L
selected operation.
207 11/14/2022 10:59AM
P
R
E QUEUE as LINKED LIST
P
A
R
⚫ To insert a new node into the queue – enQueue(value)
E
D
⚫ Create a newNode with given value and set 𝑛𝑒𝑤𝑁𝑜𝑑𝑒 → 𝑛𝑒𝑥𝑡 to
B
Y
NULL.
S
U ⚫ Check whether queue is Empty (𝑟𝑒𝑎𝑟 == 𝑁𝑈𝐿𝐿)
L
A
V ⚫ If it is Empty, then set 𝑓𝑟𝑜𝑛𝑡 = 𝑛𝑒𝑤𝑁𝑜𝑑𝑒 and 𝑟𝑒𝑎𝑟 =
N 𝑛𝑒𝑤𝑁𝑜𝑑𝑒
E
P
A ⚫ If it is not Empty, then set 𝑟𝑒𝑎𝑟 → 𝑛𝑒𝑥𝑡 = 𝑛𝑒𝑤𝑁𝑜𝑑𝑒 and
L
𝑟𝑒𝑎𝑟 = 𝑛𝑒𝑤𝑁𝑜𝑑𝑒
208 11/14/2022 10:59AM
P
R
E QUEUE as LINKED LIST
P
A
R
⚫ To delete a node from the queue – deQueue()
E
D
⚫ Check whether queue is Empty (𝑓𝑟𝑜𝑛𝑡 == 𝑁𝑈𝐿𝐿)
B
Y
⚫ If it isEmpty, then display“QueueisEmpty. Deletionisnot possible.”
S
U and terminate from the function.
L
A
V ⚫ If it is not Empty,then define a Node pointer‘temp’and set it to‘front’.
N
E
P ⚫ Then set 𝑓𝑟𝑜𝑛𝑡 = 𝑓𝑟𝑜𝑛𝑡 → 𝑛𝑒𝑥𝑡 and delete ‘temp’
A (𝑓𝑟𝑒𝑒 𝑡𝑒𝑚𝑝 )
L
B
Y
S RECURSION
U
L
A
V
N
E
P
A
L
B
⚫ Theprocessisusedforrepetitivecomputationsin whicheachaction is stated in
Y terms of a previous result.
S
U ⚫ In orderto solvea problemrecursively,two conditions must besatisfied:
L ⚫ First,the problemmust bewritten in a recursiveform.
A
V ⚫ Second,the problem statement must includea stopping condition.
N
E ⚫ Weuserecursionto solvebiggerproblemconverting it into smallersub-problems.
P
A
L ⚫ Onething wehaveto keep in mind is that if eachsub-problem is following same
kind of patterns,then only wecan usethe recursive approach.
212 11/14/2022 10:59AM
P
R
E Recursion
P
A
R
⚫ Advantages of Recursion:
E ⚫ The codemay bemuch easier to write.
D
⚫ Tosolvesomeproblemswhichare naturallyrecursivesuchastower of
B Hanoi.
Y
S
U ⚫ Disadvantages of Recursion:
L
A ⚫ Recursive functions are generally slowerthan non-recursivefunctions.
V
⚫ Mayrequire a lot of memoryto hold intermediate results onthe system
N stack.
E
P ⚫ It isdifficult tothink recursivelysoone must bevery careful when
A
L
writing recursivefunctions.
B
Y ⚫ Otherwise,it’s known as head-recursive.
S
U
L
A
V
N
E
P Head-recursivemethod Tail-recursivemethod
A
L
B
⚫ These compilers execute recursive calls with the help of stack.
Y
B
{
Y if(x==0)
S return 0;
U
L else if(x==1)
A
V return 1;
N else
E
P return (fibo(x-1) + fibo(x-2));
A
L }
220 11/14/2022 10:59AM
P
R
E Greatest Common Divisor (GCD)
P
A
R
⚫ GreatestCommonDivisor(GCD) or Highest Common Factor (HCF)
E of two numbers is the largest number that divides both of them.
D
B
Y ⚫ For example:let us assume twonumbers 63 and 42
S ⚫ 63 = 7 ∗ 3 ∗ 3
U
⚫ 42 = 7 ∗ 3 ∗ 2
L
A ⚫ So, the GCDof 63 and 42 is 21.
V
N
E
P
A
L
B
Y ⚫ Theseringsareof different sizesandstackeduponin an ascending
S order.i.e.the smaller ring sits over the larger one.
U
L
A
V
⚫ Thereareothervariationsofthepuzzle wherethenumberofdisks
increase,but the tower count remains the same.
N
E
P
A
L
B
Y ⚫ Only one disk can bemovedamong the towers at any given time.
S
U ⚫ Only the“top”disk can beremoved.
L
A
V
⚫ No largedisk can sit over a small disk.
N
E
P ⚫ Only 3 stacks can beused for solvingthe puzzle.
A
L
B
Y
S
U
L
A
V
N
E
P
A
L
B
Y
S
U
L
A
V
N
E
P
A
L
S ⚫ Step 2:
U
L ⚫ Move𝑛𝑡ℎ disk from source to destination
A
V
N ⚫ Step 3:
E ⚫ Move𝑛 − 1 disks fromauxiliary to destination
P
A
L
Number of steps required = 𝟐𝒏 − 𝟏
227 11/14/2022 10:59AM
P
R
E Tower of Hanoi – Algorithm
P
A
R
E
D
voidTOH(int n,int a,int b,int c) ⚫ Move n-1 disks from A to B
B
Y { using C
S
if(n>0)
U {
L ⚫ Move 𝒏𝒕𝒉 disk fromAto C
A TOH(n-1,A, C,B);
V print (A, C);
N TOH(n-1, B,A,C); ⚫ Move n-1 disks from B to C
E }
P using A
A }
L
B
Y ⚫ Wecan solve that in an iterative wayalso but that will bea very tough
task.
S
U
L
A
⚫ For smaller problems the iterative way will work fine but for the larger
V tree the iterative way will become the headache to use because as the tree
N
sizewill increase the sizeof the code too will increase.
E
P
A ⚫ But with the help of recursion the size of the code will become
L
drastically small.
232 11/14/2022 10:59AM
P
R
E Applications of Recursion
P
A
R
⚫ Sorting Algorithms
E ⚫ Most of the sorting algorithms uses the concept of recursion to sort the
D
data according to the given condition.
B
Y
⚫ Manyof the sorting algorithms like quick sort,mergesort,etc.usesthe
S
U recursion to sort the data.
L
A
V ⚫ Some searching algorithms like Binary search uses the concept of
N
recursion.
E
P
A
L
B
Y
S SORTING
U
L
A
V
N
E
P
A
L
N ⚫ If a sorting algorithm, after sorting the contents, does not change the
E sequence of similar content in which they appear;it is called stable sorting.
P
A
L ⚫ If asortingalgorithm, aftersortingthecontents, changesthesequenceof
similar content in which they appear;it is called unstable sorting.
11/14/2022 10:59AM
237
P
R
E Sorting
P
A
R
⚫ Stable sorting
E
D
B
Y
S
U
L ⚫ Unstable sorting
A
V
N
E
P
A
L
B
Y ⚫ This is possible wheneverthe data to besorted is small enough to all
S be held in the main memory.
U
L
A
V
⚫ There are3 types of internal sort:
⚫ Selection sort – for example,HeapSortAlgorithm
N
E ⚫ Insertion sort – for example,Shell Sort Algorithm
P
A ⚫ Exchange sort – for example,BubbleSort Algorithm, Quick Sort
L Algorithm
239 11/14/2022 10:59AM
P
R
E Internal & External Sorting
P
A
R
⚫ Sorting large amount of data requires external or secondarymemory.
E
D
⚫ This processuses external memory such as HDDto store the data
B
Y which is not fit into the main memory.
S
U
L ⚫ All external sorts arebasedon processof merging.
A
V
N ⚫ For example,MergeSort.
E
P
A
L
B
Y ⚫ The array is virtually split into a sorted and an unsorted part.
S
U
L ⚫ Values fromthe unsorted part arepickedandplacedat the correct
A
V
position in the sorted part.
N
E
P
A
L
B
Y
S
U
L
A
V
N
E
P
A
L
B
Y
S
U
L
A
V
N
E
P
A
L
S ⚫ Algorithm:
U ⚫ Step 1 − Set MIN to location 0
L
A ⚫ Step 2 − Search the minimum element in the list
V
⚫ Step 3 − Swap with valueat location MIN
N
⚫ Step 4 − Increment MIN to point to next element
E
P ⚫ Step 5 − Repeat until list is sorted
A
L
B
Y
S
U
L
A
V
N
E
P
A
L
N
E 53 is the smallest number in the data that remains and it is swappedto
P theappropriateposition.
A
L Ofthetworemainingdataitems,77 isthe smaller. The items areswapped
and the selectionsortis completed.
266 11/14/2022 10:59AM
P
R
E Selection Sort: Analysis
P
A ⚫ Selecting the lowest element requires scanning all 𝑛 elements (this takes
R
E 𝑛 − 1 comparisons)and then swappingit into the first position.
D
B ⚫ Finding the next lowest element requires scanning all 𝑛 − 1 elements and
Y
so on; for (𝑛 − 1) + (𝑛 − 2) + ⋯ + 2 + 1 𝑂 𝑛2
S comparisons.
U
L
A ⚫ Each of these scansrequires oneswapfor 𝑛 − 1 elements.
V
N
E
⚫ Therefore,
P ⚫ Best caseperformance:𝑂 𝑛 2
A
⚫ Worst case performance:𝑂 𝑛2
L
⚫ Averagecaseperformance:𝑂 𝑛2
250 11/14/2022 10:59AM
P
R
E Bubble Sort
P
A
R
⚫ Bubble sort is the simplest sorting algorithm that works by repeatedly
E swapping the adjacent elementsif they are in wrong order.
D
B
Y ⚫ Algorithm:
S ⚫ Step 1 – Compare each pair of adjacent elements in the list
U ⚫ Step 2 – Swap twoelement if necessary
L
A ⚫ Step 3 – Repeat this processfor all the elements until the entire array
V
is sorted
N ⚫ Step 4 – Reduce n by 1 and go to Step 1
E
P
A
L
B
Y
S
U
L
A
V
N
E
P
A
L
B
Y ⚫ This algorithm avoids large shifts as in case of insertion sort, if the
S smaller value is to the far right and has to be movedto the far left.
U
L
A
V
⚫ Thisalgorithm uses insertion sort on a widely spread elements, first
to sort them and then sorts the less widely spacedelements.
N
E
P
A ⚫ This spacing is termed as interval.
L
B
⚫ Step 2 – Divide the array into sub-arrays of equal gap size
Y
N
E
P
A
L
B
Y ⚫ In the second loop, comparing and swapping (if necessary) of the
S elements is done at the interval of 2 (𝑛/4). i.e. 0𝑡ℎ position and
U
L
2𝑛𝑑 position.
A
V
N
E
P
A
L
B
Y ⚫ In the third loop, comparing and swapping (if necessary) of the
S elements is done at the interval of 1 (𝑛/8). i.e. 0𝑡ℎ position and
U
L
1𝑠𝑡 position.
A
V
N
E
P
A
L
B
Y
⚫ Example 2:
35 33 42 10 14 19 27 44
S
U
L ⚫ Example 3:
A 12 34 54 2 3
V
N ⚫ Example 4:
E
P 80 50 60 30 10 20 40 70
A
L
⚫ Example 5:
276 60 40 10 30 50 20 11/14/2022 10:59AM
P
R
E Divide and Conquer Algorithm
P
A ⚫ Divide and Conquer is an algorithmic pattern where the designs is to take
R
E a dispute on a huge input, break the input into minor pieces, decide the
D problem on each of the small pieces, and then merge the piecewise solutions
into a global solution.
B
Y
S
⚫ This mechanism of solving the problem is called the Divide and Conquer
U Strategy.
L
A
V ⚫ Divide andConqueralgorithm consists of a disputeusingthe following
N threesteps:
E ⚫ Divide the original probleminto a set of subproblems.
P
A ⚫ Conquer– Solve every subproblemindividually,recursively.
L ⚫ Combine– Put together the solutions of the subproblemsto get the solution to
the whole problem.
260 11/14/2022 10:59AM
P
R
E Divide and Conquer Algorithm
P
A
R
E
D
B
Y
S
U
L
A
V
N
E
P
A
L
N
⚫ It is more proficient than that of its counterpart Brute Forcetechnique.
E
P
A ⚫ Since these algorithms inhibit parallelism, it does not involve any
L
modification and is handled by systems incorporating parallel
279 processing. 11/14/2022 10:59AM
P
R
E Divide and Conquer Algorithm
P
A
R
⚫ Disadvantages:
E ⚫ Since most of its algorithms are designed by incorporating recursion, it
D
necessitates high memory management.
B
Y
⚫ An explicit stack mayoverusethe space.
S
U
L
A
⚫ It may even crash the system if the recursion is performed rigorously
V greater than the stack present in the CPU.
N
E
P
A
L
N ⚫ Towerof Hanoi
E
P
A
L
B
Y ⚫ Step 2 − Divide the list recursively into two halves until it can no
S morebe divided.
U
L
A
V
⚫ Step 3 − Mergethe smaller lists into new list in sorted order.
N
E
P
A
L
B
Y
S
U
L
A
V
N
E
P
A
L
B
Y
S
U
L
A
V
N
E
P
A
L
B
Y
S
U
L
A
V
N
E
P
A
L
B
Y
S
U
L
A
V
N
E
P
A
L 𝑇𝑜𝑡𝑎𝑙 𝑡𝑖𝑚𝑒 𝑐𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 = 𝑂 𝑛 log 𝑛
270 11/14/2022 10:59AM
P
R
E Quick Sort
P
A ⚫ Quick sort is one of the different sorting technique which is basedon
R
E the concept of Divide and Conquer.
D
S
U ⚫ This algorithm divides the list into three main parts:
L
A
⚫ Elements less than the Pivot element
V ⚫ Pivot element (Central element)
N ⚫ Elements greater than the pivot element
E
P
A ⚫ Pivotelementcanbeanyelementfromthearray; it canbethefirst
L
element, the last element, or any randomelement.
271 11/14/2022 10:59AM
P
R
E Quick Sort – Algorithm
P
A ⚫ Step 1 – Select a random element as a pivot and define two variables i
R
E and j as first and last elementsof the list respectively.
D
N
E ⚫ Step 5 – Repeat steps 2, 3, and 4 until i>j.
P
A
L ⚫ Step 6 – Exchange the pivot element with list[j] element.
289 11/14/2022 10:59AM
P
R
E Quick Sort – Example
P
A ⚫ Example 1:
R
E
35 50 15 25 80 20 90 45
D
⚫ Example 2:
B
Y 7 1 3 5 2 6 4
S
U ⚫ Example 3:
L 7 6 10 5 9 2 1 15 7
A
V
⚫ Example 4:
N
E
10 15 1 2 9 6 11
P
A
⚫ Example 5:
L
60 40 10 30 50 20
273 11/14/2022 10:59AM
P
R
E Quick Sort – Analysis
P
A
R
⚫ Best Case – Pivot is alwaysthe median.
E ⚫ 𝑂 𝑛 log 𝑛
D
B
Y ⚫ Worst Case – Pivot is alwayssmallest or largest element.
S ⚫ 𝑂 𝑛2
U
L
A
V ⚫ Average Case – Pivot is random.
⚫ 𝑂 𝑛 log 𝑛
N
E
P
A
L
S
U ⚫ The heap sort algorithmhas twomajor steps:
L
A ⚫ The first major step involvestransforming the complete tree into a heap.
V
⚫ The second major step is to perform the actual sort by extracting the
N largest or lowest element from the root and transforming the remaining
E
P treeinto a heap.
A
L
B
Y ⚫ The Complete Binary Tree is a binary tree which is completely filled
S (means all the nodes has 2 children) except the last level which might
U
L
not becompletely full.
A
V
N
E
P
A
L
N
E
P
A
L
N
E ⚫ If 𝛼 has child node 𝛽, then – 𝑘𝑒𝑦 𝛼 ≤ 𝑘𝑒𝑦 𝛽
P
A ⚫ Min-Heap – wherethe value of the root node is less than orequal to
L either of its children.
278 11/14/2022 10:59AM
P
R
E Max – Heap Algorithm
P
A
R
⚫ Step 1 – Create a new node at the end of heap
E
D
B
⚫ Step 2 – Assign new value to the node
Y
B
Y
S
U
L
A
V
N
E
P
A
L Treeconstructed for above input and order of arrival
280 11/14/2022 10:59AM
P
R
E Min – Heap Algorithm
P
A
R
⚫ Step 1 – Create a new node at the end of heap
E
D
B
⚫ Step 2 – Assign new value to the node
Y
B
Y
S
U
L
A
V
N
E
P
A
L Treeconstructed for above input and order of arrival
282 11/14/2022 10:59AM
P
R
E Heap Sort – Example
P
A
R
𝑰𝒏𝒑𝒖𝒕 → 𝟖𝟐, 𝟗𝟎, 𝟏𝟎, 𝟏𝟐, 𝟏𝟓, 𝟕𝟕, 𝟓𝟓, 𝟐𝟑
E
D
B
Y
S
U
L
A
V
N
E
P
A
L
B
Y
S
U
L
A
V
N
E
P
A
L
B
Y
S
U
L
A
V
N
E
P
A
L
B
Y
S
U
L
A
V
N
E
P
A
L
B
Y
S
U
L
A
V
N
E
P
A
L
B
Y
S
U
L
A
V
N
E
P
A
L
B
Y
S
U
L
A
V
N
E
P
A
L
B
Y
S
U
L
A
V
N
E
P
A
L
B
Y
S
U
L
A
V
N
E
P
A
L
B
Y
S
U
L
A
V
N
E
P
A
L
B
Y
S
U
L
A
V
N
E
P
A
L 𝑺𝒐𝒓𝒕𝒆𝒅 𝑨𝒓𝒓𝒂𝒚 − 𝟏, 𝟑, 𝟒, 𝟓, 𝟕, 𝟖
293 11/14/2022 10:59AM
P
R
E Efficiency of Sorting Algorithm
P
A ⚫ The complexity of a sorting algorithm measuresthe running time of
R
E a function in which 𝑛 number of items are to besorted.
D
B
Y ⚫ The choice of sorting method depends on efficiency considerations for
S different problems.
U
L
A
V
⚫ The most important of these considerations are:
⚫ The length of time spent byprogrammerin coding a particular sorting
N
E program.
P ⚫ Amount of machine time necessary for running the program.
A
L ⚫ The amount of memory necessary for running the program.
294 11/14/2022 10:59AM
P
R
E Efficiency of Sorting Algorithm
P
A ⚫ Varioussortingmethodsareanalyzedin thecaseslike – best case, averagecase, or worst
R case.
E
D
⚫ Mostof the sort methods we consider have requirements that range from 𝑂(𝑛 log 𝑛) to
B 𝑂 𝑛2 .
Y
⚫ Asort should not be selected only becauseits sorting time is 𝑂(𝑛 log 𝑛); the relation of
S the file size 𝑛 and the other factorsaffecting the actual sorting time must beconsidered.
U
L
A ⚫ Determining the time requirementof sorting technique is to actually run the programand
V measureits efficiency.
N
⚫ Once a particular sorting technique is selected the needis to make the programasefficient
E
P
as possible.
A
L ⚫ Any improvement in sortingtimesignificantlyaffect the overall efficiency and savesa
great deal of computer time.
295 11/14/2022 10:59AM
P
R
E Efficiency of Sorting Algorithm
P
A
R Time Complexity
E Algorithm Data Structure
D Best Average Worst
B
Y Quick Sort Array 𝑂 𝑛 log 𝑛 𝑂 𝑛 log 𝑛 𝑂 𝑛2