Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
109 views

Data Structure and Algorithm

This document contains student worksheets for an ITS 209 course on data structures and algorithms. The worksheets cover topics including definitions of key terms like data structures, algorithms, linear and non-linear data structures, and abstract data types. They also include questions asking students to define and explain different data structure types like arrays, linked lists, stacks, queues, trees, and graphs. The worksheets are part of chapters covering introduction to data structures, different data structure types, classes and objects, linear data structures, and non-linear data structures.

Uploaded by

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

Data Structure and Algorithm

This document contains student worksheets for an ITS 209 course on data structures and algorithms. The worksheets cover topics including definitions of key terms like data structures, algorithms, linear and non-linear data structures, and abstract data types. They also include questions asking students to define and explain different data structure types like arrays, linked lists, stacks, queues, trees, and graphs. The worksheets are part of chapters covering introduction to data structures, different data structure types, classes and objects, linear data structures, and non-linear data structures.

Uploaded by

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

COMPUTER SCIENCE & INFORMATION TECHNOLOGY DEPARTMENT

WORKSHEETS
IN
APPLICATION SOFTWARE

Fuertes, Crislyn C.
(Student’s Name)

LEAH ACABAL-PILEO
Instructor, ITS 209
ITS 209 | Data Structures and Algorithm
CHAPTER I
Introduction to Data Structure
Direction: Write your answer inside the area provided on each number. If your answer will not fit, use the back page of the paper and indicate the number.

Define the following terms with your own understanding:


1. Data Structures
Is a way of storing data and organising data in order to use effieciently.
2. Algorithm
Algorithm is a step-by-step procedure, which defines a set of instructions or logic, to accomplish a certain predefined
task.
3. Linear Data Structures
Data items are arranged in linear sequence.
4. Non-linear Data Structure
Data items are not in sequence.
5. Abstract Data Types
It stores data and allows various operations on the data to access or change it.
Answer the following questions:
1. What is the difference between Data Structures and Algorithm?
Data Structures is about rendering data elements in terms of some relationship, for better organization and storage
while algorithm is not the complete code or program, it is just the core logic of a problem, which can be expressed
either as an informal high level description as pseudocode or using a flowchart.
2. What is the importance of Data Structure?
it allows a variety of critical operations to be performed, using as few resources, both execution time and memory
space, as possible. It allow the programmers to do data management in an efficient way.
3. What is the importance of Algorithms?
An algorithm is important in optimizing a computer program according to the available resources. Algorithm makes
sure computer will do the given task at best possible manner.
4. How do memory works
Memory manager has the job of finding a contiguous block of locations of at least the requested size from somewhere
within the memory pool. The memory manager will typically return some piece of information that the requestor can
hold on to so that later it can recover the data that were just stored by the memory manager. This piece of information
is called a handle.

5. What are Core Operations of ADT?


 Add an item
 Remove an item
 Find, retrieve, or access an item

ITS 209 | Data Structures and Algorithm


CHAPTER II
Data Structures
Direction: Write your answer inside the area provided on each number. If your answer will not fit, use the back page of the paper and indicate the number.

Define the following terms with your own understanding:


1. Homogenous Data Structure
All the elements are of same type.
2. Non-Homogenous Data Structure
The elements may or may not be of the same type.
3. Stack
It ordered collection of data items. "Last in first out". Only the element at the top maybe accessed at any point in time.
4. Queues
Queue is best described as "first in, first out", inserts element at the rear and ejects an element from the front.
5. Lists
It ordered collection of data items. Order is important.
6. Tree
Tree is a collection of nodes starting at the root nodes, where each nodes contains a data value and list of references to
the other nodes with the constraints that no reference is duplicated and none points to the root.
Answer the following questions:
1. What is the difference between linear Data Structure and Non-linear Data Structure?
The linear data structure is a structure wherein data elements are adjacent to each other. On the other hand, a non-
linear data structure is a structure wherein each data element can connect to more than two adjacent data elements.
2. What are the characteristics of a linear data type?
Linear data type is easy to implement because computer memory is arranged in a linear way. The data items are
arranged in an orderly manner where the elements are attached adjacently.
3. What are the characteristics of a non-linear data types?
Non-linear data type is not easy to implement in comparison to linear data structure. It arranges the data in a sorted
order and there exists a relationship between the data elements.
4. What are the characteristics of an abstract data types?
Allows you to think what you can do to a collection of data independently of how you do it. And allows to develop each
data structures in relative isolation from the rest of the solution.

ITS 209 | Data Structures and Algorithm


CHAPTER III
Classes and Objects
Direction: Write your answer inside the area provided on each number. If your answer will not fit, use the back page of the paper and indicate the number.

Define the following terms with your own understanding:


1. Classes
Is a definition of an object
2. Objects
Is an instance of a class.
3. Instance Variables
It declared in a class, but outside a method.
4. Method
Is a collection of statements that perform some specific task and return the result to the caller.
5. Identifier
Is a name given to a package, class, interface, method, or variable.

Answer the following questions:


1. What are difference between objects and classes?
An object is that classes serve only one purpose, they are the template from which objects inherit properties and
functionality. A class is like a blueprint from which a house is built.
2. Explain each type of linear data structure, in your own words.
There are four types of linear data structures which are arrays that data is stored and retrieved using an index that
refers to the element number in the data sequence and it can be accessed in any order. A stack is a data structure in
which only the top element can be accessed. A queue is a data structure that can simulate a list or stream of data. And
a linked list is a sequence of nodes in which each node is connected to the node following it.
3. Explain each type of non-linear data structure, in your own words.
There are two type of nonlinear data structures which are graph is one type of data structure that contains a set of
ordered pairs and trees that contains a root node and others as child nodes.

ITS 209 | Data Structures and Algorithm


CHAPTER IV
Linear Data Structures
Direction: Write your answer inside the area provided on each number. If your answer will not fit, use the back page of the paper and indicate the number.

Define the following terms with your own understanding:


1. Array
Arrays means that data can be accessed in any order. In programming, an array is declared as a variable having a
number of indexed elements
2. Linked-Lists
A linked list is a linear data structure (like arrays) where each element is a separate object.
3. Stack
A stack is a linear data structure which the order follows LIFO(Last In First Out) approach for accessing elements.
4. Queue
A queue is a form of linear structure that follows the FIFO (First In First Out) approach for accessing elements.
5. Elements
The elements are adjacently attached to each other and in a specified order.

6. Index
Indexing is defined as the process of associating a key with data location.
7. Doubly Linked-List
Doubly linked lists are a special type of linked list wherein traversal across the data elements can be done in both
directions.

Answer the following questions:


1. How Arrays Are Stored in the Memory?
An array store primitive data types or reference (to derived data) types just like objects the variable of the array holds
the reference to the array. An array stores its elements in contiguous memory locations.
2. What are multi-dimensional Array?
Multidimensional arrays make use of multiple indexes to store data. It is useful when storing data that cannot be
represented using single dimensional indexing, such as data representation in a board game, tables with data stored in
more than one column.
3. Explain Each types of Linked-List.
 Singly Linked List – each node stores the address or reference of the next node in the linked list, leave for the
last node that stores NULL.
 Doubly Linked List – each node keeps two references. One point to the next node and the other points to the
previous node.
 Circular Linked List – in this type of linked list, all nodes are connected to form a circle. Hence, there is no NULL
at the end.

ITS 209 | Data Structures and Algorithm


CHAPTER V
Non-Linear Data Structures
Direction: Write your answer inside the area provided on each number. If your answer will not fit, use the back page of the paper and indicate the number.

Define the following terms with your own understanding:


1. Trees
Tree represents the nodes connected by edges.
2. Graphs
A graph is a pictorial representation of a set of objects where some pairs of objects are connected by links.

3. Hash Tables
Data is stored in an array format, where each data value has its own unique index value.
4. Non- Linear Data Structure
A data structure is said to be non-linear if traversal of nodes is nonlinear in nature.
Answer the following questions:
1. How do Non-Linear Data Structure Differ from Linear data structure?
The linear data structure arranges the data into a sequence and follow some sort of order. Whereas, the non-linear
data structure does not organize the data in a sequential manner.

2. What are the characteristics of a Tree?


 There is a sequence of nodes.
 There starting node also known as a root node.
 Every node other that the root has a parent node.
 Nodes may have any number of children.
3. What are the characteristics of a Graph?
 Consists of a set of nodes and a set of edges that relate the nodes to one another.
 Made up of set of nodes called vertices and a set of line called edges that connect the nodes.
 The edges without direction called undirected graph.
 The graph whose edges directed from one vertrex to another called directed graph.
4. What are the characteristics of Hash Table?
 The hash function is easy to understand and simple to compute.
 The hash function should produce the keys which will get distributed, uniformly over an array.
 The hash function is a perfect hash function when it uses all the input data.
 The hash function should generate different hash values for the similar string.
 A number of collisions should be less while placing the data in the hash table.
ITS 209 | Data Structures and Algorithm
CHAPTER VI
Algorithm and Analysis

Define the following terms with your own understanding:


1. Variable
Is a data name that be used to store data value.
2. Worst-Case
It is the longest time that an algorithm will use over all instances of size n for a given problem to
produce a desire result.
3. Best-Case
It is the shortest time that the algorithm will use over all instances of size n for a given problem to
produce a desire result.
4. Constant Function
In computer programming, one easy way to compare different algorithms is to use the constant
function indicated by f(n) = c or O(1).
5. Logarithm Function
The logarithm function has the form f (n) = log n to the base b for some constant b. The value of b has to
be greater than 1. This function is defined as x = log n to base b, if and only if bx = n.
6. Linear Function
An algorithm is a linear function or has linear time complexity represented by O(n) when the time taken
is directly proportional to the size n. It is also represented by the linear equation f(n) = cn.
7. Loops
A loop is a sequence of instructions that is continually repeated until a certain condition is reached.
Answer the following questions:
5. Explain what algorithm analysis is in your own words.

6. Why is experimental Studies important?


Experimental study is important because allows us to write a program that can implement an algorithm
and run the program with varying size and data sets but it has limitations like it is necessary to
implement and test the algorithm in order to determine its running time. Experiments can be done only
on a limited set of inputs, and may not be indicative of the running time on other inputs not included in
the experiment. In order to compare two algorithms, the same hardware and software environments
should be used.

ITS 209 | Data Structures and Algorithm


CHAPTER VI
Sorting

Define the following terms with your own understanding:


1. Merge-Sort
Merge sort is a sorting technique based on divide and conquer technique, first divides the array into
equal halves and then combines them in a sorted manner.
2. Divide
The problem into sub-problem that are similar to the original but smaller in size.
3. Conquer
The sub-problems by solving them recursively. If they small enough, just solve them in a straightforward
manner.
4. Combine
The solutions to create a solution to the original problem.
5. Bucket Sort
It assumes that the input is generated by a random process and drawn from uninform distribution.
6. Quick Sort
It is called as partition exchange sort. The main principle of this method is divide and rule.

Answer the following questions:


7. The divide-and-conquer pattern consists of the following three steps: Discuss
 Divide - break the problem into several sub problems of small size.
 Conquer - solve the problem recursively.
 Combine - combine these solutions to create a solution to the original problem.

8. What is the best algorithm for sorting each of the following: general comparable objects, long
character strings, 32-bit integers, double-precision floating-point numbers, and bytes? Justify your
answer.

Answer the following


Activity 1.
Assume that each swap(x, y) means three assignments (namely tmp = x; x = y; y = tmp). Improve the
insert sort algorithm pseudocode shown in the handout to reduce the number of assignments performed in
the inner loop.
Answer:
Pseudocode is an informal notation that is pretty similar to real source code but which omits any
irrelevant details. For example we write swap(x,y) instead of the sequence of three assignments that
would normally be required in many languages. The exact syntax is not terribly important: what matters
more is clarity, brevity and conveying the essential ideas and features of the algorithm. It should be
trivial to convert a piece of well-written pseudocode into the programming language of your choice.

Activity 2.
Provide a useful invariant for the inner loop of insertion sort, in the form of an assertion to be inserted
between the “while” line and the “swap” line.
Answer:
for (i = 1 to n-1)
{
key = arr[i];
j = i-1;
while (j >= 0 and arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
swap arr[i] and arr[j]
swap arr[i + 1] and arr[high])
return (i + 1)
}
Activity 3.
What is the asymptotic complexity of the variant of insertsort that does fewer swaps?
Answer:
If we indicate as n the size of the input array to be sorted, and as f(n) the very precise (but very difficult
to accurately represent in closed form) function giving the time taken by our algorithm to compute an
answer on the worst possible input of size n, on a specific computer, then our task is not to find an
expression for f(n) but merely to identify a much simpler function g(n) that works as an upper bound,
i.e. a g(n) such that f(n) = O(g(n)). Of course a loose upper bound is not as useful as a tight one: if f(n) =
O(n 2 ), then f(n) is also O(n 5 ), but the latter doesn’t tell us as much.

Activity 4.
The proof of Assertion 1 (lower bound on exchanges) convinces us that Θ(n) exchanges are always
sufficient. But why isn’t that argument good enough to prove that they are also necessary?
Answer:
Assertion 1 (lower bound on exchanges). If there are n items in an array, then Θ(n) exchanges always
suffice to put the items in order. In the worst case, Θ(n) exchanges are actually needed.
Proof. Identify the smallest item present: if it is not already in the right place, one exchange moves it to
the start of the array. A second exchange moves the next smallest item to place, and so on. After at
worst n − 1 exchanges, the items are all in order. The bound is n − 1 rather than n because at the very
last stage the biggest item has to be in its right place without need for a swap—but that level of detail is
unimportant to Θ notation.

Activity 5.
When looking for the minimum of m items, every time one of the m−1 comparisons fails the best-so-far
minimum must be updated. Give a permutation of the numbers from 1 to 7 that, if fed to the Selection
sort algorithm, maximizes the number of times that the above-mentioned comparison fails.
Answer:
M(M − 1)/2.
7(7-1)2
=7(6)2
=7(12)
=84

Activity 6.
Code up the details of the binary partitioning portion of the binary insertion sort algorithm.
Answer:
import java.util.Arrays;

class GFG
{

public static void main(String[] args)


{

final int[] arr = {37, 23, 0, 17, 12, 72, 31,

46, 100, 88, 54 };

new GFG().sort(arr);

for(int i=0; i<arr.length; i++)

System.out.print(arr[i]+" ");

public void sort(int array[])

for (int i = 1; i < array.length; i++)

int x = array[i];

// Find location to insert using binary search

int j = Math.abs(Arrays.binarySearch(array, 0, i, x) + 1);

//Shifting array to one location right

System.arraycopy(array, j, array, j+1, i-j);

//Placing element at its correct location

array[j] = x;
}

}
}

Activity 7.
Prove that Bubble sort will never have to perform more than n passes of the outer loop.
Answer:
It consists of repeated passes through the array during which adjacent elements are compared and, if
out of order, swapped. The algorithm terminates as soon as a full pass requires no swaps. Bubble sort is
so called because, during successive passes, “light” (i.e. low-valued) elements bubble up towards the
“top” (i.e. the cell with the lowest index, or the left end) of the array. Like Insertion sort, this algorithm
has quadratic costs in the worst case but it terminates in linear time on input that was already sorted.

Activity 8.
Can you spot any problems with the suggestion of replacing the somewhat mysterious line a3[i3] =
smallest(a1, i1, a2, i2) with the more explicit andobviousa3[i3] = min(a1[i1], a2[i2])? What would be
your preferred way of solving such problems? If you prefer to leave that line as it is, how would you
implement the procedure smallest it calls? What are the tradeoffs between your chosen method and
any alternatives?
Answer:
BEHAVIOUR: Run the merge sort algorithm on the integer array a,
returning a sorted version of the array as the result. (Note that
the array is NOT sorted in place.)
PRECONDITION: array a contains len(a) integer values.
POSTCONDITION: a new array is returned that contains the same
integer values originally in a, but sorted in ascending order."""

if len(a) < 2:
# ASSERT: a is already sorted, so return it as is
return a

# Split array a into two smaller arrays a1 and a2


# and sort these recursively
h = int(len(a) / 2)
a1 = mergeSort(a[0:h])
a2 = mergeSort(a[h:END])

# Form a new array a3 by merging a1 and a2


a3 = new empty array of size len(a)
i1 = 0 # index into a1
i2 = 0 # index into a2
i3 = 0 # index into a3
while i1 < len(a1) or i2 < len(a2):
# ASSERT: i3 < len(a3)
a3[i3] = smallest(a1, i1, a2, i2) # updates i1 or i2 too
i3 = i3 + 1
# ASSERT: i3 == len(a3)
return a3

Activity 9.
In one line we return the same array we received from the caller, while in another we return a new
array created within the merge sort subroutine. This asymmetry is suspicious. Discuss potential
problems.
Answer:
The major portion of the algorithm is given two sorted arrays, we have to merge them into a single
sorted array. There is something known as the Two Finger Algorithm that helps us merge two sorted
arrays together. Using this subroutine and calling the merge sort function on the array halves
recursively will give us the final sorted array we are looking for.
Since this is a recursion based algorithm, we have a recurrence relation for it. A recurrence relation is
simply a way of representing a problem in terms of its subproblems.

Activity 10.
Justify that the merging procedure just described will not overwrite any of the elements in the second
half.
Answer:
An alternative is to run the algorithm bottom-up, doing away with the recursion. Group elements two
by two and sort (by merging) each pair. Then group the sorted pairs two by two, forming (by merging)
sorted quadruples. Then group those two by two, merging them into sorted groups of 8, and so on until
the last pass in which you merge two large sorted groups. Unfortunately, even though it eliminates the
recursion, this variant still requires O(n) additional temporary storage, because to merge two groups of
k elements each into a 2k sorted group you still need an auxiliary area of k cells (move the first half into
the auxiliary area, then repeatedly take the smallest element from either the second half or the
auxiliary area and put it in place).

Activity 11.
Can picking the pivot at random really make any difference to the expected performance? How will it
affect the average case? The worst case? Discuss.
Answer:
Consider first the ideal case, where each selection manages to split the array into two equal parts.
Then the total cost of Quicksort satisfies f(n) = 2f(n/2) + kn, and hence grows as O(n lg n) as we proved
in section 2.9. But, in the worst case, the array might be split very unevenly—perhaps at each step only
a couple of items, or even none, would end up less than the selected pivot. In that case the recursion
(now f(n) = f(n − 1) + kn) will go around n deep, and therefore the total worst-case costs will grow to be
proportional to n2 .One way of estimating the average cost of Quicksort is to suppose that the pivot
could equally probably have been any one of the items in the data. It is even reasonable to use a
random number generator to select an arbitrary item for use as a pivot to ensure this.

You might also like