A Level Data Structures
A Level Data Structures
Data Structure is a way of collecting and organising data in such a way that we can perform
operations on these data in an effective way. Data Structures is about rendering data elements in
terms of some relationship, for better organization and storage. For example, we have some data
which has, player's name "Virat" and age 26. Here "Virat" is of String data type and 26 is of
We can organize this data as a record like Player record, which will have both player's name and
age in it. Now we can collect and store player's records in a file or database as a data structure.
If you are aware of Object Oriented programming concepts, then a class also does the same
thing, it collects different type of data under one single entity. The only difference being, data
In simple language, Data Structures are structures programmed to store ordered data, so that
organized in memory. It should be designed and implemented in such a way that it reduces the
What is an Algorithm ?
predefined task. Algorithm is not the complete code or program, it is just the core logic (solution)
1|P a ge
1. Input- There should be 0 or more inputs supplied externally to the algorithm.
3. Definiteness- Every step of the algorithm should be clear and well defined.
An algorithm is said to be efficient and fast if it takes less time to execute and consumes less
memory space.
1. Time Complexity
2. Space Complexity
Space Complexity
Its 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: Its 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: Its the space required to store all the constants and variables(including
Environment Space: Its the space required to store the environment information needed to
2|P a ge
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
PSEUDOCODE
Pseudocode programs are not executed on computers. Rather, they help you "think out" a
program before writing it in a programming language. You can easily convert a carefully
algorithm. It uses the structural conventions of a normal programming language, but is intended
for human reading rather than machine reading. Pseudocode is an informal way of programming
description that does not require any strict programming language syntax or underlying
Pseudocode summarizes a program‟s flow, but excludes underlying details. System designers
write pseudocode to ensure that programmers understand a software project's requirements and
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
3|P a ge
have been already corrected. Firstly, program description and functionality is gathered and then
pseudocode is used to create statements to achieve the required results for a program. Detailed
pseudocode is inspected and verified by the designer‟s team or programmers to match design
specifications. Catching errors or wrong program flow at the pseudocode stage is beneficial for
development as it is less costly than catching them later. Once the pseudocode is accepted by the
team, it is rewritten using the vocabulary and syntax of a programming language. The purpose of
with sketching out the structure of the program before the actual coding takes place.
Advantages of pseudocode :
• It enables the programmer to concentrate only on the algorithm part of the code development.
Examples Of Pseudocode:
Pseudocode stands for "false code." It is lines of statements that are used as a rough first draft of
real computer code, regardless of the computer code language that will take its place during the
real coding phases. Writing pseudocode in Visual Basic is similar to writing regular pseudocode,
except you insert known variable names and known code snippets along the way. You can create
4|P a ge
How to Write a Pseudocode in Visual Basic
1.List the main functions of the new software and what the end results are to be. For example:
The user is to press the "X" button, and an "X" is supposed to display on the appropriate box.
2.Identify and write down the variable names for the different items needed in the program. For
Button = buttonO.
3.Write the beginning of the pseudocode with "Run Program" and skip a line. Make a left curly
bracket with the "{" key on your keyboard. Drop down one more line and place a right curly
bracket directly underneath it, "}." If you are writing the code by hand, then do not place this
4.Write "Form Load" under the first curly bracket. Skip down one line and indent in five spaces
with another left curly bracket under "Form Load." Write "buttonX = labelBox1.text ("X")"
5.Skip down one line and directly under the first "buttonX" statement write "buttonY=
labelBox2.text("Y")" without the outer quotation marks. Skip down and make the right curly
6.Write "End Routine" directly under all of that on the left so it lines up with "Form Load."
Make the final right curly bracket on the very bottom all the way to the left if you have not done
so already.
Example 1:
5|P a ge
Write the pseudo-code for an If..Then statement that determines the apropriate tax rate for a
$20,000 - $50,000 = 5%
Solution
income tax = 2%
income tax = 5%
[ Remember,psuedocode isn't about writing the program out,it's about giving enough detail
that a programmer can write a program that does what you need the program to do. ]
6|P a ge
- Made it more VB.NET-like
As we have discussed above, anything that can store data can be called as a data structure, hence
Integer, Float, Boolean, Char etc, all are data structures. They are known as Primitive Data
Structures.
Then we also have some complex Data Structures, which are used to store large and connected
Linked List
Tree
Graph
7|P a ge
Stack, Queue etc. (Research and write a brief explanation of these abstract data
structures)
All these data structures allow us to perform different operations on data. We select these data
8|P a ge
Sorting And Searching Algorithms
Not even a single day pass, when we do not have to search for something in our day to day life,
car keys, books, pen, mobile charger and what not. Same is the life of a computer, there is so
much data stored in it, that whenever a user asks for some data, computer has to search its
memory to look for the data and make it available to the user. And the computer has its own
What if you have to write a program to search a given number in an array? How will you do it?
Well, to search an element in a given array, there are two popular algorithms available:
1. Linear Search
2. Binary Search
Linear Search
Linear search is a very basic and simple search algorithm. In Linear search, we search an element
or value in a given array by traversing the array from the starting, till the desired element or
value is found.
It compares the element to be searched with all the elements present in the array and when the
element is matched successfully, it returns the index of the element in the array, else it return -1.
Linear Search is applied on unsorted or unordered lists, when there are fewer elements in a list.
9|P a ge
1. It is used for unsorted and unordered small list of elements.
2. It has a time complexity of O(n), which means the time is linearly dependent on the number
of elements, which is not bad, but not that good too.
3. It has a very simple implementation.
Linear search is a very basic and simple search algorithm. In Linear search, we search an element
or value in a given array by traversing the array from the starting, till the desired element or
value is found.
As we learned in the previous tutorial that the time complexity of Linear search algorithm
is O(n), we will analyse the same and see why it is O(n) after implementing it.
2. In every iteration, compare the target value with the current value of the array.
To search the number 5 in the array given below, linear search will go step by step in a
sequential order starting from the first element in the given array.
10 | P a g e
Write the above linear search in VB.Net
OUTPUT
Input: values[] = {5, 34, 65, 12, 77, 35}
target = 77
Output: 4
Input: values[] = {101, 392, 1, 54, 32, 22, 90, 93}
target = 200
Output: -1 (not found)
11 | P a g e
VB.NET LINEAR SEARCH
Linear search, also known as sequential search is an algorithm for finding a target value within
a list. It sequentially checks each element of the list for the target value until a match is found or
12 | P a g e
13 | P a g e
This is a very straightforward loop comparing every element in the array with the key. As soon
as an equal value is found, it returns. If the loop finishes without finding a match, the search
For small arrays, a linear search is a good solution because it's so straightforward. In an array of
a million elements, a linear search will take, on average, 500,000 comparisons to find the key.
Binary Search
Binary Search is used with sorted array or list. In binary search, we follow the following steps:
14 | P a g e
1. We start by comparing the element to be searched with the element in the middle of the
list/array.
2. If we get a match, we return the index of the middle element.
3. If we do not get a match, we check whether the element to be searched is less or greater than
in value than the middle element.
4. If the element/number to be searched is greater in value than the middle number, then we
pick the elements on the right side of the middle element(as the list/array is sorted, hence on
the right, we will have all the numbers greater than the middle number), and start again from
the step 1.
5. If the element/number to be searched is lesser in value than the middle number, then we pick
the elements on the left side of the middle element, and start again from the step 1.
Binary Search is useful when there are large number of elements in an array and they are sorted.
So a necessary condition for Binary search to work is that the list/array should be sorted.
Binary Search is applied on the sorted array or list of large size. It's time complexity of O(log
n) makes it very fast as compared to other sorting algorithms. The only limitation is that the
array or list of elements must be sorted for the binary search algorithm to work on it.
15 | P a g e
1. Start with the middle element:
o If the target value is equal to the middle element of the array, then return the index of the
middle element.
o If not, then compare the middle element with the target value,
If the target value is greater than the number in the middle index, then pick the
elements to the right of the middle index, and start with Step 1.
If the target value is less than the number in the middle index, then pick the elements
For i = 0 To 10
testArr(i) = i
Next
End Sub
low = 0
16 | P a g e
Dim high As Integer
high = UBound(theArray)
Dim i As Integer
i = (low + high) / 2
arrayFind = True
Exit Do
high = (i - 1)
Else
low = (i + 1)
End If
Loop
arrayFind = False
End If
End Function
Text1.Text = 7
End Sub
17 | P a g e
18 | P a g e
This is VB example code for the binary search.
Module Module1
Sub Main()
Dim arr() As Integer = New Integer() {3, 12, 32, 34, 45, 90}
'Before using binary search, the data set should be sorted already
bubbleSort(arr, arr.Length)
19 | P a g e
Dim tar, pos As Integer
Console.Write("Find what:")
tar = Integer.Parse(Console.ReadLine())
End If
End Sub
While u>=l
mid=(l+u)/2
If target=dataset(mid)
Return mid
Else If target<dataset(mid)
u=mid-1
Else If target>dataset(mid)
l=mid+1
End While
Return -1
20 | P a g e
End Function
21 | P a g e
22 | P a g e
SORTING ALGORITHMS
Sorting Algorithms are methods of reorganizing a large number of items into some specific order
These algorithms take an input list, processes it (i.e, performs some operations on it) and produce
The most common example we experience every day is sorting clothes or other items on an e-
commerce website either by lowest-price to highest, or list by popularity, or some other order.
23 | P a g e
Quick Sort Algorithm
The name "Quick Sort" comes from the fact that, quick sort is capable of sorting a list of data
elements significantly faster (twice or thrice faster) than any of the common sorting algorithms.
It is one of the most efficient sorting algorithms and is based on the splitting of an array
(partition) into smaller ones and swapping (exchange) based on the comparison with 'pivot'
element selected. Due to this, quick sort is also called as "Partition Exchange" sort. Like Merge
sort, Quick sort also falls into the category of divide and conquer approach of problem-solving
methodology.
Application
Before diving into any algorithm, its very much necessary for us to understand what are the real
world applications of it. Quick sort provides a fast and methodical approach to sort any lists of
things. Following are some of the applications where quick sort is used.
Commercial computing: Used in various government and private organizations for the
purpose of sorting various data like sorting of accounts/profiles by name or any given ID,
sorting transactions by time or locations, sorting files by name or date of creation etc.
Numerical computations: Most of the efficiently developed algorithms use priority
queues and inturn sorting to achieve accuracy in all the calculations.
Information search: Sorting algorithms aid in better search of information and what
faster way exists than to achieve sorting using quick sort.
Basically, quick sort is used everywhere for faster results and in the cases where there are space
constraints.
Explanation
Taking the analogical view in perspective, consider a situation where one had to sort the papers
bearing the names of the students, by name from A-Z. One might use the approach as follows:
24 | P a g e
1. Select any splitting value, say L. The splitting value is also known as Pivot.
2. Divide the stack of papers into two. A-L and M-Z. It is not necessary that the piles should
be equal.
3. Repeat the above two steps with the A-L pile, splitting it into its significant two halves.
And M-Z pile, split into its halves. The process is repeated until the piles are small
enough to be sorted easily.
4. Ultimately, the smaller piles can be placed one on top of the other to produce a fully
sorted and ordered set of papers.
5. The approach used here is reduction at each split to get to the single-element array.
6. At every split, the pile was divided and then the same approach was used for the smaller
piles by using the method of recursion.
Technically, quick sort follows the below steps:
Step 1 − Make any element as pivot
Step 2 − Partition the array on the basis of pivot
Step 3 − Apply quick sort on left partition recursively
Step 4 − Apply quick sort on right partition recursively
Problem Statement
Consider the following array: 50, 23, 9, 18, 61, 32. We need to sort this array in the most
efficient manner without using extra place (inplace sorting).
Solution
Step 1:
Make any element as pivot: Decide any value to be the pivot from the list. For
convenience of code, we often select the rightmost index as pivot or select any at random
and swap with rightmost. Suppose for two values “Low” and “High” corresponding to the
first index and last index respectively.
o In our case low is 0 and high is 5.
o Values at low and high are 50 and 32 and value at pivot is 32.
25 | P a g e
Partition the array on the basis of pivot: Call for partitioning which rearranges the
array in such a way that pivot (32) comes to its actual position (of the sorted array). And
to the left of the pivot, the array has all the elements less than it, and to the right greater
than it.
o In the partition function, we start from the first element and compare it with the
pivot. Since 50 is greater than 32, we don‟t make any change and move on to the
next element 23.
o Compare again with the pivot. Since 23 is less than 32, we swap 50 and 23. The
array becomes 23, 50, 9, 18, 61, 32
o We move on to the next element 9 which is again less than pivot (32) thus
swapping it with 50 makes our array as 23, 9, 50, 18, 61, 32.
o Similarly, for next element 18 which is less than 32, the array becomes 23, 9, 18,
50, 61, 32. Now 61 is greater than pivot (32), hence no changes.
o Lastly, we swap our pivot with 50 so that it comes to the correct position.
Thus the pivot (32) comes at its actual position and all elements to its left are lesser, and all
elements to the right are greater than itself.
Step 4: Repeat the steps for the left and right sublists recursively. The final array thus becomes
26 | P a g e
The following diagram depicts the workflow of the Quick Sort algorithm which was described
above.
27 | P a g e
Pseudocode of Quick Sort Algorithm:
28 | P a g e
Implementation of Quick Sort
C++
return index;
}
29 | P a g e
n = rand();
// Randomizing the pivot value in the given subpart of array.
pvt = low + n%(high-low+1);
// Swapping pivot value from high, so pivot value will be taken as a pivot while parti
tioning.
swap(&a[high], &a[pvt]);
int main()
{
int n, i;
cout<<"\nEnter the number of data elements to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
QuickSort(arr, 0, n-1);
return 0;
}
30 | P a g e
Python
# of pivot
def partition(arr,low,high):
31 | P a g e
# pi is partitioning index, arr[p] is now
# at right place
pi = partition(arr,low,high)
# Separately sort elements before
quickSort(arr,0,n-1)
print ("Sorted array is:")
for i in range(n):
print ("%d" %arr[i]),
VB.NET
stack(System.Threading.Interlocked.Increment(top)) = startIndex
stack(System.Threading.Interlocked.Increment(top)) = endIndex
32 | P a g e
endIndex =
stack(System.Math.Max(System.Threading.Interlocked.Decrement(top), top + 1))
startIndex =
stack(System.Math.Max(System.Threading.Interlocked.Decrement(top), top + 1))
Private Shared Function Partition(ByRef data As Integer(), left As Integer, right As Integer) As
Integer
Dim x As Integer = data(right)
33 | P a g e
End If
Next
Return (i + 1)
End Function
a=b
b = temp
End Sub
Complexity Analysis
Best case scenario: The best case scenario occurs when the partitions are as evenly
balanced as possible, i.e their sizes on either side of the pivot element are either are equal
or are have size difference of 1 of each other.
o Case 1: The case when sizes of sublist on either side of pivot becomes equal
occurs when the subarray has an odd number of elements and the pivot is right in
the middle after partitioning. Each partition will have (n-1)/2 elements.
o Case 2: The size difference of 1 between the two sublists on either side of pivot
happens if the subarray has an even number, n, of elements. One partition will
have n/2 elements with the other having (n/2)-1.
34 | P a g e
Questions
35 | P a g e
o The runtime complexity is expected to be O(n log n) as the selected random
pivots are supposed to avoid the worst case behavior.
Why Quick Sort is better than Merge Sort?
o Auxiliary Space : Quick sort is an in-place sorting algorithm whereas Merge sort
uses extra space. In-place sorting means no additional storage space is used to
perform sorting (except recursion stack). Merge sort requires a new temporary
array to merge the sorted arrays thereby making Quick sort the better option.
o Worst Cases : The worst case runtime of quick sort is O(n2) can be avoided by
using randomized quicksort as explained in the previous point. Obtaining average
case behavior by choosing random pivot element improves the performance and
becomes as efficient as merge sort.
o Cache Friendly: Quick Sort is also a cache friendly sorting algorithm as it has
good locality of reference when used for arrays.
Which is faster quick sort or merge sort?
Quick sort is faster than the merge sort. Please refer the above question.
Where is quick sort used?
Quick sort is basically used to sort any list in fast and efficient manner. Since the
algorithm is inplace, quick sort is used when we have restrictions in space availability
too. Please refer to the Application section for further details.
36 | P a g e
Quick Sort Pivot Algorithm
37 | P a g e
38 | P a g e
Bubble Sort Algorithm
Bubble sort, also referred to as comparison sort, is a simple sorting algorithm that repeatedly
goes through the list, compares adjacent elements and swaps them if they are in the wrong
order. This is the most simplest algorithm and inefficient at the same time. Yet, it is very much
Application
Bubble sort is mainly used in educational purposes for helping students understand the
foundations of sorting.
This is used to identify whether the list is already sorted. When the list is already sorted
(which is the best-case scenario), the complexity of bubble sort is only O(n).
In real life, bubble sort can be visualised when people in a queue wanting to be standing
in a height wise sorted manner swap their positions among themselves until everyone is
standing based on increasing order of heights.
39 | P a g e
40 | P a g e
41 | P a g e
42 | P a g e
Implementation Of Bubble Sort
if(!swapped)
break;
}
}
int main()
{
vector<int> arr = {5, 6, 2, 6, 9, 0, -1};
int n = 7;
BubbleSort(arr, n);
return 0;
}
43 | P a g e
Implementation of bubble sort in python
def bubble_sort(arr):
for i in range(len(arr) - 1):
swapped = False
for j in range(0, len(arr) - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swapped = True
if not swapped:
return
44 | P a g e
Pseudocode
We observe in algorithm that Bubble Sort compares each pair of array element unless the whole
array is completely sorted in an ascending order. This may cause a few complexity issues like
what if the array needs no more swapping as all the elements are already ascending.
To ease-out the issue, we use one flag variable swapped which will help us see if any swap has
happened or not. If no swap has occurred, i.e. the array requires no more processing to be
sorted, it will come out of the loop.
Module Module1
45 | P a g e
Sub Main()
n = Convert.ToInt16(Console.ReadLine())
Console.Write(Constants.vbCrLf)
For c = 0 To n - 1
array(c) = Convert.ToInt16(Console.ReadLine())
Next
For c = 0 To n - 2
For d = 0 To n - c - 2
swap = array(d)
46 | P a g e
array(d) = array(d + 1)
array(d + 1) = swap
End If
Next
Next
Console.Write(Constants.vbCrLf)
For c = 0 To n - 1
Console.WriteLine(array(c))
Next
Console.ReadKey()
End Sub
End Module
47 | P a g e
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Go to File, click New Project, and choose Console Application. 2. Name your project
as bubbleSort as your module. Note: This is a console application so we cannot have visual
controls for this tutorial. 3. Add the following code in your module.
1. Module bubbleSort
2. Sub Main()
4. Make a function named sorting. This will automatically sort the inputted elements that we will
create in Sub Main.
48 | P a g e
5. For entering number of elements, put this code below.
1. Console.WriteLine("Bubble Sorting")
2. Console.WriteLine()
3. Dim num, i As Integer
4. Console.Write("Enter Number of Elements: ")
5. num = CInt(Console.ReadLine)
6. Dim arr(num) As Integer
7. Console.WriteLine()
8. For i = 0 To num - 1
9. Console.Write("Enter Element(" & (i + 1) & "): ")
10. arr(i) = CInt(Console.ReadLine)
11. Next
6. For printing the inputted elements above, put this code below.
1. Console.WriteLine()
2. Console.WriteLine("Inputted Elements")
3. Console.WriteLine()
4. For i = 0 To num - 1
5. Console.WriteLine("Element in (" & i & "): " & arr(i))
6. Next
7. Lastly, we will code for the sorting of elements (bubble sort), put this code below.
1. Console.WriteLine()
2. sorting(arr, num)
3. Console.WriteLine("Sorted Elements")
4. Console.WriteLine()
5. For i = 0 To num - 1
6. Console.WriteLine("Element in (" & i & "): " & arr(i))
7. Next
8. Console.ReadLine()
49 | P a g e
50 | P a g e
Questions
51 | P a g e
Static And Dynamic Data Structures
Data structure is a way of storing and organising data efficiently such that the required
operations on them can be performed be efficient with respect to time as well as memory.
Simply, Data Structure are used to reduce complexity (mostly the time complexity) of the code.
A static data structure is fixed in size, but dynamic structures can increase of decrease in size.
52 | P a g e
Example of Static Data Structures: Array
An array is a collection of items stored at contiguous memory locations. The idea is to store
multiple items of the same type together. This makes it easier to calculate the position of each
element by simply adding an offset to a base value, i.e., the memory location of the first element
of the array (generally denoted by the name of the array).
The above image can be looked as a top-level view of a staircase where you are at the base of the
staircase. Each element can be uniquely identified by their index in the array (in a similar way as
you could identify your friends by the step on which they were on in the above example).
In Dynamic data structure the size of the structure is not fixed and can be modified during the
operations performed on it. Dynamic data structures are designed to facilitate change of data
structures in the run time.
53 | P a g e
Example of Dynamic Data Structures: Linked List;Tree
A linked list is a linear data structure, in which the elements are not stored at contiguous memory
locations. The elements in a linked list are linked using pointers as shown in the below image:
In simple words, a linked list consists of nodes where each node contains a data field and a
reference (link) to the next node in the list.
The QUEUE and the STACK are linear lists. This means each data item only points to the one
before it and after it.They have the idea of order built into them, such as 'last' or 'first'. But they
do not imply there is any relationship between the data items themselves.The TREE on the other
hand, is designed to represent the relationship between data items.Just like a family tree, a TREE
data structure is illustrated below.
54 | P a g e
Each data item within a tree is called a 'node'.
The highest data item in the tree is called the 'root' or root node.
Below the root lie a number of other 'nodes'. The root is is the 'parent' of the nodes immediately
linked to it and these are the 'children' of the parent node.
If nodes share a common parent, then they are 'sibling' nodes, just like a family.
The tree structure above is a general tree. But there is a very specific form of tree which is THE
BINARY TREE.
The TREE is a general data structure that describes the relationship between data items or
'nodes'.The parent node of a binary tree has only two child nodes.
55 | P a g e
Static Data Structure vs Dynamic Data Structure
Static Data structure has fixed memory size whereas in Dynamic Data Structure, the size can be
randomly updated during run time which may be considered efficient with respect to memory
complexity of the code. Static Data Structure provides more easier access to elements with
respect to dynamic data structure. Unlike static data structures, dynamic data structures are
flexible.
In competitive programming the constraints on memory limit is not much high and we cannot
exceed the memory limit. Given higher value of the constraints we cannot allocate a static data
In the Operating System, there are two techniques for memory allocation and these are as
follows:
In Contiguous Memory Allocation whenever any user process request for the memory then a
single section of the contiguous memory block is allocated to that process according to the
requirements of the process. Contiguous memory allocation is achieved just by dividing the
memory into the fixed-sized partition.In this, all the available memory space remains together at
one place and freely available memory partitions are not distributed here and there across the
whole memory space.
With the help of Non-contiguous memory allocation, a process is allowed to acquire several
memory blocks at different locations in the memory according to its need. In the non-contiguous
memory allocation, the available free memory space is distributed here and there which means
that all the free memory space is not in one place.In this technique, memory space acquired by a
56 | P a g e
process is not at one place but it is at different locations according to the requirements of the
process.
57 | P a g e
We will learn about the different types of memory management techniques and also the pros and
cons of different memory management techniques.
Memory Management
Memory is central to the operation of a computer system. It consists of a large array of words or
bytes each with its own address. In uniprogramming system, main memory has two parts one for
the operating system and another part is for the program currently being executed. In the
multiprogramming system, the memory part of the user is further divided into accommodate
processes. The task of the subdivision is cannot out by the operating system and is known as
memory management.
1. Uniprogramming:
In the uniprogramming technique, the RAM is divided into two parts one part is for the
resigning the operating system and other portion is for the user process. Here the fence
register is used which contain the last address of the operating system parts. The
operating system will compare the user data addresses with the fence register and if it is
different that means the user is not entering in the OS area. Fence register is also called
boundary register and is used to prevent a user from entering in the operating system
area. Here the CPU utilization is very poor and hence multiprogramming is used.
2. Multiprogramming:
In the multiprogramming, the multiple users can share the memory simultaneously. By
multiprogramming we mean there will be more than one process in the main memory and
if the running process wants to wait for an event like I/O then instead of sitting ideal CPU
will make a context switch and will pick another process.
a. Contiguous memory allocation
b. Non-contiguous memory allocation
58 | P a g e
a) Contiguous memory allocation
In contiguous memory allocation, all the available memory space remain together in one place.
It means freely available memory partitions are not scattered here and there across the whole
memory space.
In the contiguous memory allocation, both the operating system and the user must reside in the
main memory. The main memory is divided into two portions one portion is for the operating
and other is for the user program.
In the contiguous memory allocation when any user process request for the memory a single
section of the contiguous memory block is given to that process according to its need. We can
achieve contiguous memory allocation by dividing memory into the fixed-sized partition.
A single process is allocated in that fixed sized single partition. But this will increase the degree
of multiprogramming means more than one process in the main memory that bounds the number
of fixed partition done in memory. Internal fragmentation increases because of the contiguous
memory allocation.
59 | P a g e
→ Fixed sized partition
In the fixed sized partition the system divides memory into fixed size partition (may or may not
be of the same size) here entire partition is allowed to a process and if there is some wastage
inside the partition is allocated to a process and if there is some wastage inside the partition then
it is called internal fragmentation.
In the variable size partition, the memory is treated as one unit and space allocated to a process is
exactly the same as required and the leftover space can be reused again.
In the non-contiguous memory allocation the available free memory space are scattered here
and there and all the free memory space is not at one place. So this is time-consuming. In
the non-contiguous memory allocation, a process will acquire the memory space but it is not at
one place it is at the different locations according to the process requirement. This technique
of non-contiguous memory allocation reduces the wastage of memory which leads to internal
and external fragmentation. This utilizes all the free memory space which is created by a
different process.
60 | P a g e
Non-contiguous memory allocation is of different types,
1. Paging
2. Segmentation
3. Segmentation with paging
i) Paging
A non-contiguous policy with a fixed size partition is called paging. A computer can address
more memory than the amount of physically installed on the system. This extra memory is
actually called virtual memory. Paging technique is very important in implementing virtual
memory. Secondary memory is divided into equal size partition (fixed) called pages. Every
process will have a separate page table. The entries in the page table are the number of pages a
process. At each entry either we have an invalid pointer which means the page is not in main
memory or we will get the corresponding frame number. When the frame number is combined
with instruction of set D than we will get the corresponding physical address. Size of a page table
61 | P a g e
is generally very large so cannot be accommodated inside the PCB, therefore, PCB contains a
register value PTBR( page table base register) which leads to the page table.
Disadvantages:
1. It makes the translation very slow as main memory access two times.
2. A page table is a burden over the system which occupies considerable space.
ii) Segmentation
Segmentation is a programmer view of the memory where instead of dividing a process into
equal size partition we divided according to program into partition called segments. The
translation is the same as paging but paging segmentation is independent of internal
fragmentation but suffers from external fragmentation. Reason of external fragmentation is
program can be divided into segments but segment must be contiguous in nature.
A variable's data type determines the values that the variable can store and the operations that
can be performed on it. For example, an integer is a whole number, which can be added,
subtracted, multiplied, or divided.
An integer is an example of a primitive data type, as it can contain only one value.
Other examples of primitive data types are real (fractional numbers), Boolean (true or
false values), and character. Although a string is a collection of characters, it is usually
classified as a primitive data type.
62 | P a g e
A composite or compound data type is built by combining primitive data types. An
example of a composite or compound data type is a record or a class.
A data structure is a collection of data that is organised to allow efficient processing.
For example, the contents of an array can be sorted using a wide range of sorting
algorithms. Different data structures lend themselves to different applications. Some data
structures are highly specialised to specific tasks. For example, an abstract syntax tree is a
data structure widely used in compilers to represent the structure of program code.
An abstract data type is a conceptual model that describes how data is organised and
which operations can be carried out on the data (e.g. insert, delete) from the perspective
of an end user who does not need to know how this is implemented. There are often many
ways to implement an abstract data type, depending on the programming language being
used.
63 | P a g e