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

1- Write an algorithm to find the square root of a given number.

Also write the


pseudo code and draw flow chart.

Step 1: Read the input number and store it in the variable 'num'.
Step 2: Initialize variables:
Step 2.1: 'sqrt' as num / 2
Step 2.2: 'temp' as 0
Step 3. Repeat the following steps until 'sqrt' is not equal to 'temp':
Step 3.1: Set 'temp' as the current value of 'sqrt'.

Step 3.2: Update 'sqrt' by calculating the average of num / temp and temp: (num /
temp + temp) / 2.

Step 4: Display the square root of 'num' as 'sqrt'.

Pseudo Code

1- Input num
2- Set sqrt=num/2.0
3- Set temp=0
4- While sqrt != temp is true
temp = sqrt;
sqrt = (num / temp + temp) / 2.0;
5- endWhile
6- print sqrt
2- Given two positive non-zero integers n and m, write an algorithm for finding their
greatest common devisor (gcd). Also write the Pseudo code and draw the flow chart

Step 1 : Read the input numbers and store it in the variable n,m
Step 2: Initialize variable
Step 2.1: R= n mod m
Step 3: Let n = m and m = R
Step 4: Repeat Steps 2 and 3 until n mod m is greater than 0
Step 5: GCD = m
Step 6: stop

Pseudo code
1- Input n,m
2- While n mod m>0 is true
R=n mod m
n=m
m=R
End While
3- Print m
3- Algorithm to Compute All Prime Factors of a Given Integer:

Step 1: Start.

Step 2: read the input number and store it in num.

Step 4: Initialize a factor (let's call it factor) to 2.

Step 5: Repeat until num becomes 1:

Step 5.1: While num is divisible by factor:

Step 5.1.1: Display factor

Step 5.1.2: Update num by dividing it by factor.

Step 5.2 Increment factor by 1.

Step 6: End

Pesudo Code

1- Input num
2- Declare primeFactor as array
3- factor=2
4- While num>1 is true
While num mod factor =0 is true
Print factor
num=num/factor
End While
factor=factor+1
End While
4- an algorithm to generate a uniform set of pseudo-random numbers using linear
congruential method. Successive members of the linear congruential sequence (x) are
generated using the expression: xn+1 = (a*n + b) mod m for n >= 0, where the
parameters a,b,m,x0 must be chosen in advance according to certain criteria. The
parameters a, b and m are referred to as multiplier, increment and modulus respectively
and their values should be greater than or equal to zero and m should be greater than
x0, a and b

Step 1: Initialize the parameters a, b, m, and x0 according to the specified criteria:


Step 1.1: Ensure that a, b, and m are greater than or equal to zero.
Step 1.2: Ensure that m is greater than x0, a, and b.
Step 2: Initialize a seed value x with x0.
Step 3: Repeat for each random number needed:
Step 3.1: Calculate the next pseudo-random number using the linear
congruential formula: xn+1 = (a * xn + b) mod m
Step 3.2: Set x to the newly generated pseudo-random number (xn+1).
Step 3.3: Output or use the value of x as a pseudo-random number.
The values of x at each step represent the generated pseudo-random numbers.
5- Given some integer x, write an algorithm to compute the value of xn where n is positive
number considerably greater than 1. Also write the pseudo code and draw flow chart.

Algorithm to Calculate Power (x^n) Iteratively:


Step 1: Start
Step 2: Declare integer variables x, n, and result.
Step 3: Read the value of x from the user.
Step 4: Read the value of n from the user.
Step 5: If n is less than or equal to 1, display an error message and exit.
Step 6: Initialize result to 1.
Step 7: Repeat the following n times:
Step 7.1: Multiply result by x.
Step 8: Display the value of result as the result of x^n.
Step 9: End.

Pseudo Code
1- Input x
2- Input n
3- If n <=1 then
Print(“power number is not correct”
End if
4- result=1
5- for i=0;i<n;i++
result=result*x
End loop
6- print result
6- Write an algorithm to implement the algorithm to search an element in an array of N
elements using linear search and binary search and determine the time to search the
element in each case

Step 1: Start
Step 2: Initialize a variable element_to_search to the element you want to search for.
Step 3: Initialize an array arr with N elements.
Step 4: Initialize variables found_index_linear and found_index_binary to -1.
Step 5: Initialize a variable start_time to the current time.
Step 6: Perform a linear search:
Step 6.1: For each element in the array arr from index 0 to N-1:
Step 6.1.1 If the current element is equal to element_to_search, set
found_index_linear to the current index and break.
Step 6.2: If found_index_linear is not -1, the element is found.
Step 6.3: If found_index_linear is -1, the element is not found.
Step 7: Calculate the time taken for linear search by subtracting start_time from the
current time (call it time_linear_search).
Step 8: Sort the array arr if it's not already sorted (binary search requires a sorted array).
Perform a binary search:
Step 8.1: Initialize variables low to 0 and high to N-1.
Step 8.2: While low is less than or equal to high:
Step 8.2.1: Calculate the middle index as (low + high) / 2.
Step 8.2.2: If the middle element is equal to element_to_search, set
found_index_binary to the middle index and break.
Step 8.2.3: If the middle element is less than element_to_search, update low
to middle + 1.
Step 8.2.4: If the middle element is greater than element_to_search, update
high to middle - 1.
Step 8.3: If found_index_binary is not -1, the element is found.
Step 8.4: If found_index_binary is -1, the element is not found.
Step 9: Calculate the time taken for binary search by subtracting start_time from the
Step 10: current time (call it time_binary_search).
Step 11: Display whether the element was found or not in both searches and the time
taken for each search.
Step 12: End
7- Algorithm to Remove Duplicate Values from an Ordered Array:

Step 1: Start

Step 2: Initialize an integer variable N to the number of elements in the array.

Step 3: If N is less than or equal to 1, there are no duplicates, so no action is needed. End the
algorithm.

Step 4: Initialize two pointers, current and next, both initially set to 0.

Step 5: Repeat while next is less than N - 1:

Step 5.1: If array[current] is equal to array[next], increment next to skip duplicates.

Step 5.2: If array[current] is not equal to array[next], copy the value at array[next] to
the position array[current + 1].

Step 5.2: Increment both current and next.

Step 6: The array from index 0 to current now contains unique elements. Truncate the array
to size current + 1.

Step 7: End.
8- Write algorithm for Quick sort on an unsorted array of N elements and determine
the time to sort the elements. Also discuss the best, worst and average cases.

Step 1: Start

Step 2: If the array has fewer than two elements, it is already sorted, so no further action is
needed. End.

Step 3: Choose a pivot element from the array. Various strategies exist for selecting the pivot,
such as selecting the first, last, or middle element.

Step 4: Partition the array into two sub-arrays:

Step 4.1: Elements less than the pivot (left sub-array).

Step 4.2: Elements greater than or equal to the pivot (right sub-array).

Step 5: Recursively apply Quick Sort to the left and right sub-arrays.

Step 6: Combine the sorted sub-arrays and the pivot to obtain the sorted array.

Step 7: End.

 Best Case: The best case occurs when the pivot chosen consistently divides the array into
nearly equal halves. In this case, the time complexity is O(n log n), where n is the number
of elements in the array.

 Worst Case: The worst case occurs when the pivot is consistently the smallest or largest
element, resulting in unbalanced partitions. In this case, the time complexity is O(n^2).

 Average Case: The average case time complexity for Quick Sort is O(n log n). In practice,
Quick Sort often performs well because the choice of a good pivot and randomization can
reduce the likelihood of worst-case.

You might also like