Data Structure Algorithums Assignment
Data Structure Algorithums Assignment
Development
Task 1: Which data structure can be used, when simulating the above scenario? What
are the valid operations that can be carried out on this data structure?
Data Structure
Data structure is a specialized format for organizing, processing, retrieving and storing data.
While there are several basic and advanced structure types, any data structure is designed to
arrange data to suit a specific purpose so that it can be accessed and worked with in
appropriate ways.
In computer programming, a data structure may be selected or designed to store data for the
purpose of working on it with various algorithms. Each data structure contains information
about the data values, relationships between the data and functions that can be applied to the
data.
HND in Computing & System
Development
1. Linear or non-linear: This attribute describes whether the data items are arranged
chronologically in a row, or in a sequence that is not aligned with a graph.
3. Static or dynamic: This attribute describes how data structures are compiled. Static
data structures consist of fixed sizes, structures, and memory locations at the time of
compilation. Dynamic data structures have sizes, structures, and memory locations
that can be shrunk or expanded depending on the application.
Characteristic Description
Linear In Linear data structures, the data items are arranged in a linear
sequence. Example: Array
Non-Linear In Non-Linear data structures, the data items are not in sequence.
Example: Tree, Graph
Homogeneous In homogeneous data structures, all the elements are of same type.
Example: Array
Non- In Non-Homogeneous data structure, the elements may or may not
Homogeneous be of the same type. Example: Structures
Static Static data structures are those whose sizes and structures associated
memory locations are fixed, at compile time. Example: Array
Dynamic Dynamic structures are those which expands or shrinks depending
upon the program need and its execution. Also, their associated
memory locations changes. Example: Linked List created using
pointers
The data structures can also be classified on the basis of the following characteristics:
HND in Computing & System
Development
Data structure types are determined by what kinds of functions are required or what types of
methods are used. These categories include:
II. Stack - A layer stores the set of objects in the linear array used by the functions. This
order may be last in first out (LIFO) or first out (FIFO).
III. Queues - An array stores a set of objects like a stack; however, the order of operation
can be first.
IV. Linked Lists - A linked list saves a collection of objects in a linear order. Each
element or node in the linked list contains a data item and a reference or link to the
next item in the list.
V. Trees - Tree saves a collection of objects in a concise, hierarchical way. Each node is
connected to other nodes and can have multiple sub-values, also called children.
VI. Graphs - A map stores a set of objects in a non-linear fashion. Graphs are made up of
a set of finite nodes, also called vertices, and the lines connecting them are called
edges. These are useful for representing real-life systems, such as computer networks.
VII. Tries - A tri, or main tree, is a data map that stores strings as data items that can be
organized into a visual map.
VIII. Hash Tables - A hash table, or a hash map, saves a set of objects in a sub-array that
sets the value keys. A hash table uses a hash function to convert an index into a row of
buckets containing the desired data item.
These are considered complex data structures because they can store large interconnected
data. Examples of primitive, or basic, data structures are integers, floats, Booleans, and
characters.
HND in Computing & System
Development
Queue
Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is
open at both its ends. One end is always used to insert data (enqueue) and the other is used to
remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item
stored first will be accessed first.
Basic Operations
Queue operations may involve initializing or defining the queue, utilizing it, and then
completely erasing it from the memory. Here we shall try to understand the basic operations
associated with queues −
Few more functions are required to make the above-mentioned queue operation efficient.
These are −
Peek () − Gets the element at the front of the queue without removing it.
In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing
(or storing) data in the queue we take help of rear pointer.
HND in Computing & System
Development
Enqueue Operation
Queues maintain two data pointers, front and rear. Therefore, its operations are comparatively
difficult to implement than that of stacks.
The following steps should be taken to enqueue (insert) data into a queue −
Step 3 − If the queue is not full, increment rear pointer to point the next empty space.
Step 4 − Add data element to the queue location, where the rear is pointing.
Dequeue Operation
Accessing data from the queue is a process of two tasks − access the data where front is
pointing and remove the data after access. The following steps are taken to perform dequeue
operation
Step 3 − If the queue is not empty, access the data where front is pointing.
Step 4 − Increment front pointer to point to the next available data element.
Task 3: Determine the operations of a memory stack and how it is used to implement
function calls related to the above scenario.
Stack
A stack (sometimes called a "push-down stack") is a hierarchical set of objects that always
take place at the same end, adding new items and removing existing ones. This result is
commonly referred to as "over." The opposite edge above is called the "base".
The base of the cascade is significant because it indicates that the items stored in the crust
close to the base were long in the layer. The most recently added item has to be removed first.
This sorting principle is sometimes called LIFO, the last first-out. It returns a queue based on
the time in the collection. New items are at the top, old items are near the base.
Since only a limited number of functions are allowed, a single layer is considered a restricted
data structure. In addition to push and pop functions, some functions may allow advanced
functionality:
1. Overview - See the highest item in the stack.
2. Copy - Copy the value of the top item to a variable and push it back into the stack.
3. Swap - Replace the two best items in the stack.
4. Rotate - Move the highest elements in the stack as specified by a number or move
them in a rotating fashion.
Stack concept software implementations are performed using arrays and linked lists, where
the top level is monitored using a variable or header pointer, respectively. Many
programming languages offer built-in features to support stack implementation. Hardware
stacks are implemented for the purpose of memory allocation and access using a standard
look and size. Stack registers are used to store the stack pointer value.
Stack Representation
Stack can be executed by array, structure, pointer, and linked list. The layer is either a fixed
size or it can have a dynamic resizing feel. Here, we are going to implement the stack using
arrays, which becomes a standard size layer implementation.
The following diagram depicts a layer and its functions:-
Stack functions include initializing the stack, using it, and initializing it. In addition to these
basic things, a layer is used for the following two primary functions -
To use a layer efficiently, you need to check the state of the layer. For the same purpose, the
following function is added to the layers: -
Peek () - Get the best data element of the layer without removing it.
Is Full () - Check if the layer is full.
Is Empty () - Check whether the layer is empty.
At all times, we maintain a pointer to the last bushed data of the layer. Since this pointer
always refers to the top of the stack, it is named above. The top pointer returns the top value
of the layer without actually removing it.
Push Operation
The process of storing a new data element is called a push operation. There are a series of
steps in the Bush process -
If used to implement a linked list layer, in Step 3, the space is dynamically allocated.
Pop Operation
Accessing content when it is removed from the layer is called a pop operation. In the queue
implementation of the pop () function, the data element is not actually removed, and instead
is reduced to the lower level in the top layer to indicate the next value. But in a linked-list
implementation, pop () actually removes the data element and allocates memory space.
Task 4: Sort the banquet hall list and display from the smallest to largest based on the
maximum number of seating arrangements that can be done.
Sort the banquets halls with two different sorting algorithms and compare the
performances of those two algorithms.
Sorting algorithm
Sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used
orders are lexicographical order and numerical order. Efficient sorting is important part for
optimizing the use of other algorithms such as search and merge algorithms, which require
input data to be in sorted lists; this is also often useful for canonicalizing data and for
producing human-readable output.
1. Bubble Sort
2. Insertion Sort
3. Selection Sort
4. Merge Sort
5. Heapsort
6. Quicksort
Bubble Sort
Bubble sort works by swapping adjacent elements if they're not in the desired order. This
process repeats from the beginning of the array until all elements are in order.
We know that all elements are in order when we manage to do the whole iteration without
swapping at all - then all elements we compared were in the desired order with their adjacent
elements, and by extension, the whole array.
Here are the steps for sorting an array of numbers from least to greatest:
HND in Computing & System
Development
4 2 1 5 3: The first two elements are in the wrong order, so we swap them.
2 4 1 5 3: The second two elements are in the wrong order too, so we swap.
2 1 4 5 3: These two are in the right order, 4 < 5, so we leave them alone.
2 1 4 5 3: Another swap.
Because at least one swap occurred during the first pass (there were actually three), we need
to go through the whole array again and repeat the same process.
By repeating this process, until no more swaps are made, we'll have a sorted array.
The reason this algorithm is called Bubble Sort is because the numbers kind of "bubble up" to
the "surface." If you go through our example again, following a particular number (4 is a
great example), you'll see it slowly moving to the right during the process.
All numbers move to their respective places bit by bit, left to right, like bubbles slowly rising
from a body of water.
Merge Sort
Merge Sort uses recursion to solve the problem of sorting more efficiently than algorithms
previously presented, and in particular it uses a divide and conquer approach.
Using both of these concepts, we'll break the whole array down into two subarrays and then:
This tree is meant to represent how the recursive calls work. The arrays marked with the
down arrow are the ones we call the function for, while we're merging the up arrow one’s
going back up. So you follow the down arrow to the bottom of the tree, and then go back up
and merge.
In our example, we have the array 3 5 3 2 1, so we divide it into 3 5 4 and 2 1. To sort them,
we further divide them into their components. Once we've reached the bottom, we start
merging up and sorting them as we go.