Data Structure and Algorithm
Data Structure and Algorithm
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.
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.
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.
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.
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
{
new GFG().sort(arr);
System.out.print(arr[i]+" ");
int x = array[i];
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
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.