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

Sorting Question

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
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views

Sorting Question

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
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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 – [email protected]


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 – [email protected]


2

You might also like