0% found this document useful (0 votes)
67 views

Data Structure Algorithums Assignment

The document discusses data structures and how they can be used to implement function calls. It describes a stack as a hierarchical data structure that follows LIFO (last-in, first-out) ordering when adding or removing items. A stack is used to implement function calls by allocating memory on the stack frame for each function call. This stack memory is automatically allocated and freed as functions are called and returned.

Uploaded by

whatstubes
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Data Structure Algorithums Assignment

The document discusses data structures and how they can be used to implement function calls. It describes a stack as a hierarchical data structure that follows LIFO (last-in, first-out) ordering when adding or removing items. A stack is used to implement function calls by allocating memory on the stack frame for each function call. This stack memory is automatically allocated and freed as functions are called and returned.

Uploaded by

whatstubes
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

HND in Computing & System

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

Characteristics data structures

Data structures are often characterized by their characteristics. Possible properties:

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.

2. Homogeneous or non-homogeneous: This attribute describes whether all data items


in a given repository are the same or different.

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

Types of data structures

Data structure types are determined by what kinds of functions are required or what types of
methods are used. These categories include:

I. Arrays - An array stores a collection of objects in nearby memory locations. The


status of each element can be easily calculated or retrieved as identical items are
stored together. The rows can be adjusted or the length is flexible.

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

Select the Queue Data Structure

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 −

Enqueue () − add (store) an item to the queue.

Dequeue () − remove (access) an item from the queue.

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.

Is full () − Checks if the queue is full.

Is empty () − Checks if the queue is empty.

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 1 − Check if the queue is full.

Step 2 − If the queue is full, produce overflow error and exit.

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.

Step 5 − return success.

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 1 − Check if the queue is empty.

Step 2 − If the queue is empty, produce underflow error and exit.


HND in Computing & System
Development

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.

Step 5 − Return success.


HND in Computing & System
Development

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.

Stack Memory in Java


Stack Memory in Java is used for static memory allocation and the execution of a thread. It
contains primitive values that are specific to a method and references to objects that are in a
heap, referred from the method.

Access to this memory is in Last-In-First-Out (LIFO) order. Whenever a new method is


called, a new block on top of the stack is created which contains values specific to that
method, like primitive variables and references to objects.
When the method finishes execution, its corresponding stack frame is flushed, the flow goes
back to the calling method and space becomes available for the next method.

Key Features of Stack Memory


Apart from what we have discussed so far, following are some other features of stack
memory:
1. It grows and shrinks as new methods are called and returned respectively
2. Variables inside stack exist only as long as the method that created them is running
3. It’s automatically allocated and deallocated when method finishes execution
4. If this memory is full, Java throws java.lang.StackOverFlowError
5. Access to this memory is fast when compared to heap memory
HND in Computing & System
Development

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

Basic functions Memory Stack


HND in Computing & System
Development

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 -

Push () - push (save) an element in the layer.


Pop () - Removes an element from the layer (access).

When the data is pushed into the layer.

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 -

Step 1 - Checks whether the layer is full.


Step 2 - If the layer is full, create an error and exit.
Step 3 - If the layer is not filled, increment to point to the next empty space.
Step 4 - Add the data element to the stack location at the point indicated above.
Step 5 - Leads to Success.
HND in Computing & System
Development

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.

The following steps in a popup process can be -

Step 1 - Checks if the layer is empty.


Step 2 - If the layer is empty, create an error and exit.
Step 3 - If the layer is not empty, access the data element pointing to the top.
Step 4 - Reduces the upper value to 1.
Step 5 - Leads to
Success.
HND in Computing & System
Development
HND in Computing & System
Development

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.

Type of sorting algorithm

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.

2 1 4 3 5: Here's the resulting array after one iteration.

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:

1. Sort the left half of the array (recursively)


2. Sort the right half of the array (recursively)
HND in Computing & System
Development

3. Merge the solutions

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.

You might also like