Tutorial-1

Question1

Given the problem: “For the given positive integer, justify if it is a prime.”

  1. Formally define the problem

  2. Give some instances and corresponding outputs

3. Construct an algorithm and describe it with/without using pseudo code

  • Input: a positive integer n
  • Output: Yes, if n is a prime; No, Otherwise

Algorithm Quicksort

procedure Quicksort(A,p,rA, p, r)

if p<rp < r then

q=q = Partition(A,p,rA, p, r)

Quicksort(A,p,q1A, p, q - 1)

Quicksort(A,q+1,rA, q + 1, r)

end if

end procedure

procedure Partition(A,p,rA, p, r)

x=A[r]x = A[r]

i=p1i = p - 1

for j=pj = p to r1r - 1 do

if A[j]<xA[j] < x then

i=i+1i = i + 1

exchange A[i]A[i] with A[j]A[j]

end if

exchange A[i]A[i] with A[r]A[r]

end for

end procedure