Analysis of Algorithms: Bubble Sort, Insertion into Singly Linked List, and Merge Sort, Exercises of Engineering

An analysis of the computation complexity of bubble sort, insertion into a singly linked list, and merge sort algorithms. It includes the best and worst-case scenarios for each algorithm, as well as the design of an ada95 package for reading in integers from a file, performing binary search, and implementing merge sort.

Typology: Exercises

2011/2012

Uploaded on 07/22/2012

senapati_007
senapati_007 🇮🇳

3.8

(4)

109 documents

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Analysis of Algorithms: Bubble Sort, Insertion into Singly Linked List, and Merge Sort and more Exercises Engineering in PDF only on Docsity!

Home Work 9

The problems in this problem set cover lectures C7, C8, C9 and C

  1. What is the Minimum Spanning Tree of the graph shown below using both Prim’s and Kruskal’s algorithm. Show all the steps in the computation of the MST (not just the final MST).

Prim’s Algorithm

Step 1.

MST

Parent

Fringe Set

Step 2

MST

Parent

Fringe Set

Step 3

MST

Parent

Fringe Set

Minimum Spanning Tree

MST

Weight of the MST = 10 + 20 + 25 + 20 + 35 = 110

Kruskals Algorithm

Initialization

1

4

5 3

6

2 10

35

50

45

30

25

55

20

20

1

4

5 3

6

2

Step 1

1

4

5 3

6

2

35

50

45

30

25

55

20

20

1

4

5 3

6

2

35

50

45

30

25

55

20

20

10

1

4

5 3

6

2 10

1

4

5 3

6

2

Step 2

1

4

5 3

6

2

35

50

45

30

25

55

20

1

4

5 3

6

2

35

50

45

30

25

55

20 10

20

1

4

5 3

6

2 10

20

1

4

5 3

6

2

  1. Compute the computation complexity of the bubble sort algorithm. Show all the steps in the computation based on the algorithm.

Algorithm Procedure Bubble_Sort(Input_Output_Array)

for I in 1 .. My_Array_Max loop for J in I+1 .. My_Array_Max loop

N N-

if (Input_Output_Array(I) <= Input_Output_Array(J)) then Temp := Input_Output_Array(I); Input_Output_Array(I) := Input_Output_Array(J); Input_Output__Array(J) := Temp; end if; end loop; end loop;

end Bubble_Sort;

O(N(N-1)) = O(N^2 )

  1. What are the best case and worst case computation complexity of:

a. Inserting a node into an unsorted singly linked list

Inserting into an unsorted singly linked list is carried out using the add_to_front operation. Both the best and worst case execution time is O(1).

b. Inserting a node into a sorted singly linked list

In the case of a sorted linked list, the list has to be traversed to find the right position. The list traversal takes O(n) in the worst case.

Best case execution time is O(1) if the element being inserted is the smallest element in the list (list in ascending order)

Worst case execution time is O(n) if the element being inserted is the largest element in the list (list in ascending order)

a. Design an Ada95 Package to: i. Read in N integers from an input file into an array. (N is user defined <=50) ii. Sort the array in ascending order iii. Perform binary search on the array.

The package is designed as follows: Data Structures:

type my_array is array (1 .. 50) of integer;

Subprograms: -- procedure to create an array with <=50 elements -- assumes that input can be found in input_file.txt -- accepts the array -- returns array with elements read from file and the number of elements read procedure Create ( Num_Array : in out My_Array; Size : out Integer );

procedure Merge ( Input_Array : in out My_Array; Lb_1 : in Integer; Ub_1 : in Integer; Lb_2 : in Integer; Ub_2 : in Integer ); -- procedure to sort the array in ascending order -- accepts the array and the lowerbound(lb) and upperbound(ub) -- returns the sorted array procedure Merge_Sort ( Num_Array : in out My_Array; Lb : in Integer; Ub : in Integer );

--function to perform binary search on the array -- accepts the array, lb, ub and the element being searched for -- returns the index of the element if it is found -- returns -1 if the element is not found function Binary_Search ( Num_Array : My_Array; Lb : Integer; Ub : Integer; Looking_For : Integer ) return Integer;

The merge sort procedure will recursively call itself until it has only single element arrays. These are inherently sorted. It will then merge the successively larger elements until the whole sorted array is produced.

Merge

Pre-Conditions: An array of 1 or more elements, Legal values of Lower_Bound_1, Upper_Bound_1, Lower_Bound_2 and Upper_Bound_2,

Post-Condition: A merged array that is sorted

Algorithm

  1. Check if a. Lower_Bound_1 < Upper_Bound_ b. Lower_Bound_2 < Upper_Bound_ c. Lower_Bound_1 < Lower_Bound_ d. Upper_Bound_1 < Upper_Bound_
  2. If any of the above conditions are violated, a. Display Error b. Stop Executing the program
  3. Set a. Index_1 := Lower_Bound_1; b. Index_2 := Lower_Bound_2; c. Index := Lower_Bound_1; d. Temp_Array := Input_Array
  4. While (Index_1 <= Upper_Bound_1) and (Index_2 < =Upper_Bound_2) a. If (Input_Array(Index_1) < Input_Array (Index_2) i. Temp_Array(Index) := Input_Array(Index_1) ii. Index_1 := Index_1 + 1; iii. Index := Index + 1; b. Else i. Temp_Array(Index) := Input_Array(Index_2) ii. Index_2 := Index_2 + 1; iii. Index := Index + 1;
  5. While (Index_1 <= Upper_Bound_1) a. Temp_Array(Index) := Input_Array(Index_1) b. Index_1 := Index_1 + 1; c. Index := Index + 1;
  6. While (Index_2 <= Upper_Bound_2) a. Temp_Array(Index) := Input_Array(Index_2) b. Index_2 := Index_2 + 1;


c. Index := Index + 1;

  1. Input_Array := Temp_Array

Binary Search

Pre-Conditions: Array to be searched Item that you are searching for

Post-condition: Index location of the item you are searching for Return –1 if the number is not found. Algorithm

  1. Set Return_Index to –1;
  2. Set Current_Index to (Upper_Bound - Lower_Bound + 1) /2.
  3. Loop i. if the lower_bound > upper_bound Exit; ii. if ( Input_Array(Current_Index) = Number_to_Search) then Return_Index = Current_Index) Exit;

iii. if ( Input_Array(Current_Index) > Number_to_Search) then Lower_Bound = Current_Index + else Upper_Bound = Current_Index – 1

  1. Return Return_Index

b. Write a program to test your package that will

  • Prompt the user for a number to search for.
  • If the number is found using the binary search algorithm
    • Display the location (index)
    • Display the number
  • If the number is not found using the binary search algorithm
    • Display “Number not in array to the user”

-- program to test Home_Work_9 Package -- Programmer: Jayakanth Srinivasan -- Date Last Modified: April 06,

with Ada.Text_Io; with Ada.Integer_Text_IO; with Home_Work_9; use Home_Work_9;

procedure Test_Hw_9 is

Unified Engineering II Spring 2004

Problem S8 Solution

  1. The convolution is given by ∞ y(t) = g(t) ∗ u(t) = g(t − τ )u(τ ) dτ (1) −∞

Note that u(τ ) is nonzero only for − 3 ≤ τ ≤ 0, and g(t − τ ) is nonzero only for 0 ≤ t − τ ≤ 3, that is, for −3 + t ≤ τ ≤ t. So there are four distinct regimes:

(a) t < − 3 (b) − 3 ≤ t ≤ 0 (c) 0 ≤ t ≤ 3 (d) t > 3

For cases (a) and (d), there is no overlap between g(t − τ ) and u(τ ), so y(t) = 0. For case (b), the overlap is for − 3 ≤ τ ≤ t. So ∞ y(t) = g(t − τ )u(τ ) dτ −∞ t = sin(− 2 π(t − τ )) sin(2πτ ) dτ − 3

At this point, we have to do a little trig:

sin(− 2 π(t − τ )) sin(2πτ ) = sin(2π(τ − t)) sin(2πτ ) = [sin(2πτ ) cos(2πt) − cos(2πτ ) sin(2πt)] sin(2πτ ) = cos(2πt) sin^2 (2πτ ) − sin(2πt) cos(2πτ ) sin(2πτ ) sin(4πτ ) = cos(2πt)

1 − cos(4πτ ) − sin(2πt) 2 2 So the integral is given by t (^) cos(2πt) t (^) cos(2πt) t sin(2πt) y(t) = dτ − cos(4πτ ) dτ − sin(4πτ ) dτ − 3 2 − 3 2 − 3 2 cos(2πt) cos(2πt) t^ sin(2πt) t = (t + 3) − sin(4πτ ) + cos(4πτ ) 2 8 π (^) τ =− 3 8 π τ =− 3 cos(2πt) cos(2πt) sin(2πt) = (t + 3) − sin(4πt) + [cos(4πt) − 1] 2 8 π 8 π

(As often happens with problems involving trig functions, there are other equiv alent expressions.)

For case (c), the region of integration is −3 + t ≤ τ ≤ 0. So � 0 � 0 � (^0) cos(2πt) cos(2πt) sin(2πt) y(t) = dτ − cos(4πτ ) dτ − sin(4πτ ) dτ −3+t 2 −3+t 2 −3+t^2 cos(2πt) cos(2πt) 0 sin(2πt)^0 = (3 − t) − sin(4πτ ) + cos(4πτ ) 2 8 π (^) τ =−3+t 8 π τ =−3+t cos(2πt) cos(2πt) sin(2πt) = (3 − t) + sin(4πt) − [cos(4πt) − 1] 2 8 π 8 π

  1. y(t) is plotted below.

-2-4 -3 -2 -1 0 1 2 3 4

-1.

-0.

0

1

2

Time, t (seconds)

y(t)

  1. The maximum value of y(t − T ) occurs at time T. So I would use this center peak to identify the delay time T.
  2. The adjacent peaks are nearly as tall as the center peak, so if noise were added to the signal, the tallest peak might not be the center peak, so we might use the wrong peak to determine the delay time.
  3. The chirp signal of Problem S6 produces an ambiguity function with only one prominent peak. Therefore, the addition of noise should not make it difficult to accurately determine the delay time.

Normal differential equation methods are difficult to apply, because we cannot apply the normal initial condition that e 1 (0) = 0. This is because the chain of capacitors running from the voltage source to ground causes there to be an impulse of current at time t = 0, and the voltages across the capacitors change instantaneously at t = 0. It is possible to use differential equation methods, we just have to be more careful about the initial conditions. However, Laplace methods are easier.

(b)

u(t)^ y(t)

E 1 E 2









Again, use impedance methods, using the node labelling above. Then the node equations are (C 1 s + G 1 )E 1 − C 1 sE 2 = G 1 U (2) −C 1 sE 1 + [(C 1 + C 2 )s + G 2 ]E 2 = 0 where G = 1/R. We can use Cramer’s rule to solve for E 2 :

C 1 s + G 1 G 1 U (s) −C 1 s 0 E 2 (s) = C 1 s + G 1 −C 1 s (C 1 + C 2 )s + G 2

−C 1 s G 1 C 1 s = U (s) C 1 C 2 s^2 + (G 1 C 1 + G 1 C 2 + G 2 C 1 )s + G 1 G 2 Since we are finding the step response, 1 U (s) = , Re[s] > 0 s Plugging in numbers, we have

  1. 1 s 1 5 / 3 Y (s) = E 2 (s) =
  2. 06 s^2 + 0. 35 s + 0. 25 s

s^2 + 5. 833 ¯^6

3 s + 4. 166 ¯ In order to find y(t), we must expand Y (s) in a partial fraction expansion. To do so, we must factor the denominator, using either numerical techniques or the quadratic formula. The result is

s^2 + 5. 833 ¯ 3 s + 4. 1666 ¯ = (s + 5)(s + 0. 833 3)¯ (5)

We can use the coverup method to factor Y (s), so that 5 / 3 0. 4 Y (s) = =

(s + 5)(s + 0. 833 ¯3)^ s + 5 s + 0. 8333 ¯^

The region of convergence must be Re[s] > − 0. 833 ¯3, since the step response is causal, and the r.o.c. is to the right of the rightmost pole. Therefore, the step response is given by the inverse transform of Y (s), so that

gs(t) = − 0. 4 e−^5 t^ + 0. 4 e−^0.^833 ¯^3 t^ σ(t) (7)

The step response is plotted below: