Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Programming in C

Assignment 1
(For all the questions, submit the program as *.c file and prepare a separate text file with
pseudocode and explanations)

1. Solve the following problems using non-recursive functions


a. Towers of Hanoi
b. Longest Increasing Subsequence in an array of integers
2. Two algorithms for sorting arrays are given below:

Algorithm A:
The algorithm maintains a partially sorted array. More precisely, at the start of the ith
iteration, the algorithm has the elements a[1] to a[i] in sorted order. In the ith iteration,
a[i+1] is added to this partially sorted array at the correct position so that we now have a
partially sorted array of size i+1. Thus after n-1 iterations, we get a fully sorted array.
Example:
Original Array :

After 1st iteration,

After 2nd iteration,

After 3rd iteration,

and so on.

Algorithm B :
Algorithm B modifies Algorithm A as follows:
For a given array and a fixed value of h, the algorithm uses Algorithm A to sort the
elements that are h indices apart i.e., a[i], a[i+h], a[i+2h], a[i+3h],. are in sorted order for
all values of i. This is repeated for different values of h= { n/2, n/4, n/8, n/16,..}. When
h=1, the array is fully sorted.
Example :
Original Array :

80

60

40

30

10

50

70

20

h=4 :

10

50

40

20

80

60

70

30

All subarrays of elements 4 indices apart ((10, 80), (50,60),) are in sorted order.

h=2:

10

20

40

30

70

50

80

60

All subarrays of elements 2 indices apart ((10, 40, 70, 80), (20, 30, 50, 60)) are in sorted
order.
h=1:

10

20

30

40

50

60

70

80

Implement Algorithm B using C programming language.


3. An interval [a,b) (a<=b), contains all the integers between a and b (including a). For
example, [1,3) contains two integers 1 and 2. Write a program that accepts n intervals as
input and calculate the total number of integers in the union of those n intervals.
For example, the union of intervals [2,5), [1,3) and [7,9) has 6 integers (1,2,3,4,7,8) .

You might also like