Solution 1 Quicksort Solution 2 Any tree which

  • Slides: 22
Download presentation
Solution 1 Quicksort

Solution 1 Quicksort

Solution 2 Any tree which traverses all the vertices of a tree with minimum

Solution 2 Any tree which traverses all the vertices of a tree with minimum cost is MST.

Solution 3 nlogn

Solution 3 nlogn

Solution 4 Real Time Complexity = ∑(Amount of time, a single execution takes *

Solution 4 Real Time Complexity = ∑(Amount of time, a single execution takes * Frequency Count)

Solution 5

Solution 5

ALGORITHM A finite set of instructions which if followed accomplish a particular task. In

ALGORITHM A finite set of instructions which if followed accomplish a particular task. In addition every algorithm must satisfy following criteria:

Input zero or more quantities externally supplied Output At least one quantity is produced

Input zero or more quantities externally supplied Output At least one quantity is produced Definiteness Each instruction must be clear and unambiguous. Finiteness In all cases algorithm must terminate after finite number of steps. Effectiveness Each instruction must be sufficiently basic.

Solution 6

Solution 6

FQ 1. i=1 1 2. Loop (i<=n) n+1 a) j=1 n b) Loop (j<=i)

FQ 1. i=1 1 2. Loop (i<=n) n+1 a) j=1 n b) Loop (j<=i) n((n+1)/2 + 1) i. Application Code ii. j = j + 1 c) i = i + 1 Total time Complexity takes the form= an 2 + bn + c Complexity = QUADRATIC n((n+1)/2) n

Section II

Section II

Solution 1

Solution 1

MERGE SORT The procedure merge sorts the elements in the sub array A[low, high].

MERGE SORT The procedure merge sorts the elements in the sub array A[low, high]. If low high the sub array has at most one element and is therefore already sorted. Otherwise we divide it into A[low, mid] and array[mid+1. . high]

Merge sort(A, low, high) 1 2 3 4 5 if low <high then mid

Merge sort(A, low, high) 1 2 3 4 5 if low <high then mid [(low+high)/2] merge sort(A, low, mid) merge sort(A, mid+1, high) merge (low, mid, high)

Algorithm Merge (low, mid, high) { h: = low; i: =low; j: =mid+1; while((h

Algorithm Merge (low, mid, high) { h: = low; i: =low; j: =mid+1; while((h mid) and (j high)) do { if (a[h] a[j] then { b[I]: = a[h]; h: =h+1; } Else {b[I]: =a[j]; j: =j+1; } I: =I+1; }

If(h> mid) then for k: =j to high do { B[I]: =a[k]; I: =I+1;

If(h> mid) then for k: =j to high do { B[I]: =a[k]; I: =I+1; } Else for k: = h to mid do { B[I] : =a[k]; I: = I+1; } For k: = low to high do a[k]: = b[k]; } Time for merging is proportional to n.

Using the above algo. the solution will be 1, 2, 3, 4, 5, 6,

Using the above algo. the solution will be 1, 2, 3, 4, 5, 6, 7, 8

Solution 2

Solution 2

Counting Sort – No comparisons between elements! – But…depends on assumption about the numbers

Counting Sort – No comparisons between elements! – But…depends on assumption about the numbers being sorted • We assume numbers are in the range 1. . k – The algorithm: • Input: A[1. . n], where A[j] {1, 2, 3, …, k} • Output: B[1. . n], sorted (notice: not sorting in place) • Also: Array C[1. . k] for auxiliary storage

Counting Sort 1 2 3 4 5 6 7 8 9 10 Counting. Sort(A,

Counting Sort 1 2 3 4 5 6 7 8 9 10 Counting. Sort(A, B, k) for i=1 to k C[i]= 0; for j=1 to n C[A[j]] += 1; for i=2 to k C[i] = C[i] + C[i-1]; for j=n downto 1 B[C[A[j]]] = A[j]; C[A[j]] -= 1; Work through example: A={4 1 3 4 3}, k = 4

Solution 3

Solution 3

Heap Sort… • Insert elements one by one. • Delete root element and reheap

Heap Sort… • Insert elements one by one. • Delete root element and reheap (if necessary) The algo is given at page no. 127 of Coremen