0% found this document useful (0 votes)
64 views2 pages

Data Structures & Algorithms Problems

This document contains 13 problems related to data structures and algorithms. Problem 1 asks for examples of algorithms that are and are not brute-force approaches. Problem 2 asks if selection sort can be implemented for linked lists with the same efficiency as for arrays. Problem 3 asks to design an algorithm to rearrange alternating disks in minimum moves. Problem 4 asks to rearrange alternating filled and empty glasses in minimum moves for ordered and random initial arrangements. The remaining problems ask about analyzing and implementing various sorting algorithms and data structures including insertion sort, divide-and-conquer, quicksort, heaps, counting sort, and an algorithm using O(n+k) preprocessing time to answer range queries on n integers in 0 to k in O(1)
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)
64 views2 pages

Data Structures & Algorithms Problems

This document contains 13 problems related to data structures and algorithms. Problem 1 asks for examples of algorithms that are and are not brute-force approaches. Problem 2 asks if selection sort can be implemented for linked lists with the same efficiency as for arrays. Problem 3 asks to design an algorithm to rearrange alternating disks in minimum moves. Problem 4 asks to rearrange alternating filled and empty glasses in minimum moves for ordered and random initial arrangements. The remaining problems ask about analyzing and implementing various sorting algorithms and data structures including insertion sort, divide-and-conquer, quicksort, heaps, counting sort, and an algorithm using O(n+k) preprocessing time to answer range queries on n integers in 0 to k in O(1)
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

Course: Data Structures & Algorithms

Class: 22CLC01/22CLC07

CHAPTER 2 – PROBLEMS
Problem 1. a. Give an example of an algorithm that should not be considered an
application of the brute-force approach.
b. Give an example of a problem that cannot be solved by a brute-force
algorithm.

Problem 2. Is it possible to implement selection sort for linked lists with the same 𝑂(𝑛2 )
efficiency as the array version?

Problem 3. Alternating disks You have a row of 2n disks of two colors, n dark and n light.
They alternate: dark, light, dark, light, and so on. You want to get all the dark disks to the
right-hand end, and all the light disks to the left-hand end. The only moves you are
allowed to make are those that interchange the positions of two neighboring disks.

Design an algorithm for solving this puzzle and determine the number of moves it takes.
Does your algorithm follow a brute-force approach?

Problem 4. Alternating glasses.


a. There are 2n glasses standing next to each other in a row, the first n of them filled
with a soda drink and the remaining n glasses empty. Make the glasses alternate
in a filled-empty-filled-empty pattern in the minimum number of glass moves.

b. Solve the same problem if 2n glasses—n with a drink and n empty—are initially
in a random order.

Problem 5. Compare the slide’s implementation of insertion sort with the following
version.
ALGORITHM INSERTION-SORT2(A[0…n-1])
for i  1 to n – 1 do
j  i – 1
while j ≥ 0 and A[j] > A[j+1] do
swap(A[j], A[j+1])
j  j – 1

What is the time efficiency of this algorithm? How is it compared to that of the version
given in the slide?

Problem 6. Write pseudocode for a divide-and-conquer algorithm for finding the


position (index) of the largest element in an array of n numbers. How does this algorithm
compare with the brute-force algorithm for this problem?

Lecturer: Nguyễn Hải Minh – University of Science HCM city – nhminh@[Link]


1
Problem 7. Find the order of growth for solutions of the following recurrences.
a. 𝑇(𝑛) = 4𝑇(𝑛/2) + 𝑛, 𝑇(1) = 1
b. 𝑇(𝑛) = 4𝑇(𝑛/2) + 𝑛2 , 𝑇(1) = 1
c. 𝑇(𝑛) = 4𝑇(𝑛/2) + 𝑛3 , 𝑇(1) = 1

Problem 8. Give an example showing that quicksort is not a stable sorting algorithm.

Problem 9. What is the running time of Quicksort when all elements of array A have the
same value?

Problem 10. Show that, with the array representation for storing an n-element heap, the
leaves are the nodes indexed by ⌊𝑛/2⌋, ⌊𝑛/2⌋ + 1, … , 𝑛 − 1.

Problem 11. Answer the following questions.


a. Construct a heap for the list 1, 8, 6, 5, 3, 7, 4 by the bottom-up algorithm (in the
slide).
b. Construct a heap for the list 1, 8, 6, 5, 3, 7, 4 by successive key insertions (design a
top-down algorithm by yourself).
c. Is it always true that the bottom-up and top-down algorithms yield the same heap
for the same input?

Problem 12. Suppose that we were to rewrite the for loop in line 7 of the Counting-Sort
in the slide as
7. for i  0 to n – 1
Show that the algorithm still works properly, but that is not stable.

Problem 13. Describe an algorithm that, given n integers in the range 0 to k, preprocesses
its input and then answers any query about how many of the n integers fall into a range
[a : b] in O(1) time. Your algorithm should use 𝑂(𝑛 + 𝑘) preprocessing time.

Lecturer: Nguyễn Hải Minh – University of Science HCM city – nhminh@[Link]


2

Common questions

Powered by AI

The reasoning lies in how elements are accessed; arrays allow direct index access, making swaps efficient and straightforward, which keeps complexity at O(n^2). However, linked lists required traversing nodes to access elements, making each swap more complex. Despite this, the overall structure of selection sort inherently maintains O(n^2) due to the nested iterations over elements.

Heap leaves in an array of n elements are indexed starting from ⌊n/2⌋ because binary tree properties ensure all nodes from this midpoint to the end have no children. These indices signify the lowest tier, holding all remaining unparented nodes.

A brute-force approach might be unsuitable for problems requiring complex decision-making or optimization because such algorithms often perform extensive calculations, which can be computationally expensive. These algorithms tend to check all possible solutions and may become impractical when the problem space is large, leading to inefficiencies in time or resources.

Swapping neighboring disks reduces the need for large shifts, efficiently using local exchanges to move disks into place. This method is not brute-force as it does not test all possible combinations indiscriminately but leverages local information to persist towards the goal, often optimizing steps through intelligent swapping patterns.

A preprocessing algorithm, achieving O(n + k) time, counts occurrences of integers in given range, storing cumulative counts in an auxiliary table. Querying uses cumulative differences to answer efficiently in O(1), leveraging precomputed state instead of recalculating for each range.

Construction differences arise from algorithmic paths taken to maintain heap properties. Bottom-up creates heaps by iteratively ensuring all sub-heaps formed by successive insertions are valid, potentially rearranging elements differently than sequential insertions in top-down, which may diverge due to starting conditions.

When Quicksort encounters identical values, its running time degrades to O(n^2) as partitioning does not divide the array efficiently, leading each recursive call to process nearly the whole subarray similarly as opposed to balanced divides.

Quicksort's partitioning procedure can change element order equivalently, unlike stable sorts that retain relative positions. In a case with equal keys, their order in the array might change post-partition because the algorithm does not inherently preserve original sequences.

Divide-and-conquer reduces problem size progressively by dividing the array into smaller subarrays, solving each recursively, then combining results. This reduces redundancy, contrasting with the brute-force approach which checks each element sequentially without leveraging subproblem solutions to minimize operations.

The variant uses additional swaps within a nested loop, slightly differing from typical insertion sort which shifts elements directly. This variation still performs at O(n^2) time complexity for the worst case but may involve more movement operations due to explicit swaps instead of shifting.

You might also like