Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views63 pages

A Level Data Structures

Advanced level data structures
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views63 pages

A Level Data Structures

Advanced level data structures
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

DATA STRUCTURES AND ALGORITHMS

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

integer data type.

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.

For example: "Dhoni" 30, "Gambhir" 31, "Sehwag" 33.

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

structures provides for techniques to access and manipulate data efficiently.

In simple language, Data Structures are structures programmed to store ordered data, so that

various operations can be performed on it easily. It represents the knowledge of data to be

organized in memory. It should be designed and implemented in such a way that it reduces the

complexity and increases the efficiency.

What is an Algorithm ?

An algorithm is a finite set of instructions or logic, written in order, to accomplish a certain

predefined task. Algorithm is not the complete code or program, it is just the core logic (solution)

of a problem, which can be expressed either as an informal high level description

as pseudocode or using a flowchart.

Every Algorithm must satisfy the following properties:

1|P a ge
1. Input- There should be 0 or more inputs supplied externally to the algorithm.

2. Output- There should be at least 1 output obtained.

3. Definiteness- Every step of the algorithm should be clear and well defined.

4. Finiteness- The algorithm should have finite number of steps.

5. Correctness- Every step of the algorithm must generate a correct output.

An algorithm is said to be efficient and fast if it takes less time to execute and consumes less

memory space.

The performance of an algorithm is measured on the basis of following properties

1. Time Complexity

2. Space Complexity

Space Complexity

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.

An algorithm generally requires space for following components :

 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

temporary variables) value.

 Environment Space: Its the space required to store the environment information needed to

resume the suspended function.

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

algorithm completes it's execution in the minimum time possible.

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

prepared pseudocode program to a corresponding Visual Basic program.Pseudocode is an

informal high-level description of the operating principle of a computer program or other

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

technology considerations. It is used for creating an outline or a rough draft of a program.

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

align code accordingly.

Description: Pseudocode is not an actual programming language. So it cannot be compiled into

an executable program. It uses short terms or simple English language syntaxes to write code for

programs before it is actually converted into a specific programming language. This is done to

identify top level flow errors, and understand the programming data flows that the final program

is going to use. This definitely helps save time during actual programming as conceptual errors

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

using pseudocode is an efficient key principle of an algorithm. It is used in planning an algorithm

with sketching out the structure of the program before the actual coding takes place.

Advantages of pseudocode :

• Pseudocode is understood by the programmers of all types.

• It enables the programmer to concentrate only on the algorithm part of the code development.

• It cannot be compiled into an executable program.

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

pseudocode by hand or by typing it in a word processing program.

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.

Same for the O's.

2.Identify and write down the variable names for the different items needed in the program. For

example: Box 1 = labelBox1; Box 2 = labelBox2; Box 3 = labelBox3; X Button = buttonX; O

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

bottom bracket until you finish the first module.

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

without the outside quotation marks.

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

bracket directly below the inner left curly bracket.

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

given annual income level using the guide below:

Under $20,000 = 2% income tax

$20,000 - $50,000 = 5%

$50,001 - $75,000 = 10%

Solution

If Annual Income is < $20,000 Then

income tax = 2%

If Annual Income is $20,000<= $50,000 Then

income tax = 5%

If Annual Income is $50,001 <= $75,000 Then

income tax = 10%

[ 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. ]

making it seem more code-like.

- Removed spaces from identifiers

- "Fixed" the If statement accoding to VB.NET syntax

- Made values "legal"

6|P a ge
- Made it more VB.NET-like

Basic Types Of Data Structures

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

data. Some example of Abstract Data Structure are :

 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

structures based on which type of operation is required.

8|P a ge
Sorting And Searching Algorithms

Introduction to 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

techniques to search through its memory fast.

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.

Features of Linear Search Algorithm

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 Algorithm

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.

Implementing Linear Search

Following are the steps of implementation that we will be following:

1. Traverse the array using a for loop.

2. In every iteration, compare the target value with the current value of the array.

o If the values match, return the current index of the array.

o If the values do not match, move on to the next array element.

3. If no match is found, return -1.

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

Some Examples with Inputs

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

until all the elements have been searched.

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

failed and -1 is returned.

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.

For a much faster search, take a look at binary search.

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.

Features of Binary Search

1. It is great to search through large sorted arrays.


2. It has a time complexity of O(log n) which is a very good time complexity. We will discuss
this in details in the Binary Search tutorial.
3. It has a simple implementation.

Binary Search Algorithm

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.

Implementing Binary Search Algorithm

Following are the steps of implementation that we will be following:

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

to the left of the middle index, and start with Step 1.

2. When a match is found, return the index of the element matched.

3. If no match is found, then return -1

VB.NET BINARY SEARCH

Private Sub Command_Click()

Dim testArr(10) As Integer, ret As String

For i = 0 To 10

testArr(i) = i

Next

ret = CStr(arrayFind(testArr, Trim(Text1.Text)))

Label1.Caption = "Element found? " & ret

End Sub

Function arrayFind(theArray() As Integer, target As Integer) As Boolean

Dim low As Integer

low = 0

16 | P a g e
Dim high As Integer

high = UBound(theArray)

Dim i As Integer

Dim result As Boolean

Do While low <= high

i = (low + high) / 2

If target = theArray(i) Then

arrayFind = True

Exit Do

ElseIf target < theArray(i) Then

high = (i - 1)

Else

low = (i + 1)

End If

Loop

If Not arrayFind Then

arrayFind = False

End If

End Function

Private Sub Form_Load()

Text1.Text = 7

Label1.Caption = "Binary Search Test"

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}

'sort the array using bubble sort

'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())

pos = binSearch(arr, tar, 0, arr.Length-1)

If (pos <> -1) Then

Console.WriteLine("It is found at {0}.", pos)

Else : Console.WriteLine("Not found!")

End If

Console.ReadLine() 'wait for keypress

End Sub

Function binSearch(ByVal dataset() As Integer, ByVal target As Integer, ByVal l As Integer,


ByVal u As Integer) As Integer

Dim mid As Integer

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

such as highest to lowest, or vice-versa, or even in some alphabetical order.

These algorithms take an input list, processes it (i.e, performs some operations on it) and produce

the sorted list.

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.

Types of Sorting Algorithms:


 Quick Sort.
 Bubble Sort.
 Merge Sort.
 Insertion Sort.
 Selection Sort.
 Heap Sort.
 Radix Sort.

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

Quicksort works in the following way

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

Quick Sort Example:

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 2: The main array after the first step becomes

Step 3: Now the list is divided into two parts:

1. Sublist before pivot element

2. Sublist after pivot element

Step 4: Repeat the steps for the left and right sublists recursively. The final array thus becomes

9, 18, 23, 32, 50, 61.

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

Following are C++, Java and Python implementations of QuickSort.

C++

Quicksort example program in c++:


#include<iostream>
#include<cstdlib>

using namespace std;

// Swapping two values.


void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}

// Partitioning the array on the basis of values at high as pivot value.


int Partition(int a[], int low, int high)
{
int pivot, index, i;
index = low;
pivot = high;

// Getting index of the pivot.


for(i=low; i < high; i++)
{
if(a[i] < a[pivot])
{
swap(&a[i], &a[index]);
index++;
}
}
// Swapping value at high and at the index obtained.
swap(&a[pivot], &a[index]);

return index;
}

// Random selection of pivot.


int RandomPivotPartition(int a[], int low, int high)
{
int pvt, n, temp;

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]);

return Partition(a, low, high);


}

int QuickSort(int a[], int low, int high)


{
int pindex;
if(low < high)
{
// Partitioning array using randomized pivot.
pindex = RandomPivotPartition(a, low, high);
// Recursively implementing QuickSort.
QuickSort(a, low, pindex-1);
QuickSort(a, pindex+1, high);
}
return 0;
}

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

// Printing the sorted data.


cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];

return 0;
}

30 | P a g e
Python

# Python program for Quicksort

# This function takes last element as pivot, places

# the pivot element at its correct position in sorted

# array, and places all smaller (smaller than pivot)

# to left of pivot and all greater elements to right

# of pivot

def partition(arr,low,high):

i = ( low-1 ) # index of smaller element


pivot = arr[high] # pivot
for j in range(low , high):
# If current element is smaller than or
# equal to pivot
if arr[j] <= pivot:

# increment index of smaller element


i = i+1
arr[i],arr[j] = arr[j],arr[i]
arr[i+1],arr[high] = arr[high],arr[i+1]
return ( i+1 )
# The main function that implements QuickSort
# arr[] --> Array to be sorted,

# low --> Starting index,


# high --> Ending index
# Function to do Quick sort
def quickSort(arr,low,high):
if 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

# partition and after partition


quickSort(arr, low, pi-1)
quickSort(arr, pi+1, high)
# Driver code to test above
arr = [10, 7, 8, 9, 1, 5]
n = len(arr)

quickSort(arr,0,n-1)
print ("Sorted array is:")
for i in range(n):
print ("%d" %arr[i]),

VB.NET

Public Shared Sub QuickSortIterative(ByRef data As Integer())


Dim startIndex As Integer = 0
Dim endIndex As Integer = data.Length - 1
Dim top As Integer = -1
Dim stack As Integer() = New Integer(data.Length - 1) {}

stack(System.Threading.Interlocked.Increment(top)) = startIndex
stack(System.Threading.Interlocked.Increment(top)) = endIndex

While top >= 0

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

Dim p As Integer = Partition(data, startIndex, endIndex)

If p - 1 > startIndex Then


stack(System.Threading.Interlocked.Increment(top)) = startIndex
stack(System.Threading.Interlocked.Increment(top)) = p - 1
End If

If p + 1 < endIndex Then


stack(System.Threading.Interlocked.Increment(top)) = p + 1
stack(System.Threading.Interlocked.Increment(top)) = endIndex
End If
End While
End Sub

Private Shared Function Partition(ByRef data As Integer(), left As Integer, right As Integer) As
Integer
Dim x As Integer = data(right)

Dim i As Integer = (left - 1)

For j As Integer = left To right - 1


If data(j) <= x Then
i += 1
Swap(data(i), data(j))

33 | P a g e
End If
Next

Swap(data(i + 1), data(right))

Return (i + 1)
End Function

Private Shared Sub Swap(ByRef a As Integer, ByRef b As Integer)


Dim temp As Integer = a

a=b
b = temp
End Sub

Complexity Analysis

Time Complexity of Quick sort

 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

 What is the average case run time complexity of Quick Sort?


The average case run time of quick sort is O(n logn) . This case happens when we dont
exactly get evenly balanced partitions. We might get at worst a 3-to-1 split on either side
of pivot element. The proof of this is quite mathematically rigorous and is out of scope of
the discussion.
 Is Quick Sort a stable algorithm?
Quick sort is not a stable algorithm because the swapping of elements is done according
to pivot‟s position (without considering their original positions). A sorting algorithm is
said to be stable if it maintains the relative order of records in the case of equality of
keys.
 Is Quick Sort an inplace algorithm?
Quick sort is an inplace algorithm which means the numbers are all sorted within the
original array itself.
 What is Randomised Quick Sort? Why is it used?
o Sometimes, it happens that by choosing the rightmost element at all times might
result in the worst case scenario.
o In such cases, choosing a random element as your pivot at each step will reduce
the probability of triggering the worst case behavior. We will be more likely
choosing pivots closer to the center of the array, and when this happens, the
recursion branches more evenly and thus the algorithm terminates a lot faster.

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

necessary to learn about it as it represents the basic foundations of sorting.

Application

Understand the working of Bubble sort

 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

Implementation of Bubble sort in C++


#include <iostream>
#include <vector>

using namespace std;

void BubbleSort (vector<int> &arr, int n)


{
for (int i = 0; i < n - 1; ++i)
{
bool swapped = false;
for (int j = 0; j < n - i - 1; ++j)
{
if (arr[j] > arr[j+1]) //check if adjacent element is
//not in order
{
swap(arr[j], arr[j + 1]);
swapped = true;
}
}
// Value at n-i-1 will be maximum of all the values below this index.

if(!swapped)
break;
}
}

int main()
{
vector<int> arr = {5, 6, 2, 6, 9, 0, -1};
int n = 7;

BubbleSort(arr, n);

// Display the sorted data.


cout << "\nSorted Data: ";
for (i = 0; i < n; i++)
Cout << arr[i] << “ “;

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

arr = [int(x) for x in input(‘Enter numbers: ’).split()]


bubble_sort(arr)
print('Sorted list: ', end='')
print(arr)

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.

Pseudocode of BubbleSort algorithm can be written as follows −

Visual Basic Code for Bubble Sort Algorithm

Module Module1

45 | P a g e
Sub Main()

Dim array(100) As Int16

Dim n, c, d, swap As Int16

Console.WriteLine("Enter number of elements")

n = Convert.ToInt16(Console.ReadLine())

Console.Write(Constants.vbCrLf)

Console.WriteLine("Enter " + n.ToString() + " integers")

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

If (array(d) > array(d + 1)) Then

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)

Console.WriteLine("Sorted list in ascending order: ")

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.

1. Sub sorting(ByVal x() As Integer, ByVal y As Integer)


2. Dim i, a, t As Integer
3. For i = 0 To y - 1
4. For a = i + 1 To y - 1
5. If x(i) > x(a) Then
6. t = x(i)
7. x(i) = x(a)
8. x(a) = t
9. End If
10. Next
11. Next
12. End Sub

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

 What is the best case time complexity of bubble sort?


The time complexity in the best case scenario is O(n) because it has to traverse through
all the elements once to recognize that the array is already sorted.
 What is the advantage of bubble sort over other sorting techniques?
o The built-in ability to detect whether the list is sorted efficiently is the only
advantage of bubble sort over other sorting techniques.
o When the list is already sorted (which is the best-case scenario), the complexity of
bubble sort is only O(n).
o It is faster than other in case of sorted array and consumes less time to describe
whether the input array is sorted or not.
 Why bubble sort is called “bubble” sort?
The “bubble” sort is called so because the list elements with greater value than their
surrounding elements “bubble” towards the end of the list. For example, after first pass,
the largest element is bubbled towards the right most position. After second pass, the
second largest element is bubbled towards the second last position in the list and so on.
 Is bubble sort a stable algorithm?
o Bubble sort is a stable algorithm.
o A sorting algorithm is said to be stable if two objects with equal keys appear in
the same order in sorted output as they appear in the input array to be sorted.
 Is bubble sort an inplace algorithm?
o Bubble sort is an inplace algorithm.
o An algorithm is said to be inplace if it does not need an extra space and produces
an output in the same memory that contains the data by transforming the input „in-
place‟. However, a small constant extra space used for variables is allowed.
 Is Bubble sort slow?
o Bubble sort is slower than the other O(n2) sorting algorithms.
o It is about four times as slow as insertion sort and twice as slow as selection sort.
o It has a good best-case behavior, but is impractically slow on almost all real world
data sets and is not considered for implementation in real applications.
 Can bubble sort be implemented recursively?
o Yes.
o Recursive Bubble Sort has no additional performance/implementation advantages,
but can be used to understand recursion and sorting concepts better.
o Base Case: If array size is 1, return.
o Do One Pass of normal Bubble Sort. This pass bubbles largest element of current
subarray to correct position.
o Recur for all elements except last of current subarray.

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.

Data structures can be two types :

1. Static Data Structure

2. Dynamic Data Structure

A static data structure is fixed in size, but dynamic structures can increase of decrease in size.

What is a Static Data structure?


In Static data structure the size of the structure is fixed. The content of the data structure can be
modified but without changing the memory space allocated to it.

52 | P a g e
Example of Static Data Structures: Array

Array Data Structure

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

What is Dynamic Data Structure?

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

Linked List Data Structure

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.

Tree Data Structure

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 link joining one node to another is called the 'branch'.

The tree structure above is a general tree. But there is a very specific form of tree which is THE
BINARY TREE.

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.

Use of Dynamic Data Structure in Competitive Programming

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

structure of that size so Dynamic Data Structures can be useful.

Difference between Contiguous and Non-Contiguous Memory Allocation

In the Operating System, there are two techniques for memory allocation and these are as
follows:

 Contiguous Memory Allocation

 Non-Contiguous Memory Allocation

Contiguous Memory Allocation

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.

Non-Contiguous Memory Allocation

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.

Differences Between Contiguous And Non-Contiguous Memory Allocation.

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.

Memory Management Techniques

The memory management techniques is divided into two parts...

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.

Advantage: Management or book keeping is easy.

Disadvantage: Internal fragmentation

→ Variable size partition

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.

Advantage: There is no internal fragmentation.

Disadvantage: Management is very difficult as memory is becoming purely fragmented after


some time.

b) Non-contiguous memory allocation

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.

Advantages: It is independent of external fragmentation.

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.

iii) Segmentation with paging

In segmentation with paging, we take advantages of both segmentation as well as paging. It is a


kind of multilevel paging but in multilevel paging, we divide a page table into equal size
partition but here in segmentation with paging, we divide it according to segments. All the
properties are the same as that of paging because segments are divided into pages.

Data Types And Data Structures

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

You might also like