You are on page 1of 7

‫‪Algorithms‬‬

‫اسئلة متوقعة تيجي في االمتحان‬


What is an algorithm ?

A step-by-step process will be designed to solve the problem.

What is the relationship between data and algorithm ?


The algorithm processes data to make it meaningful, the algorithm provides the logic, and the data provides
the values. this relationship is expressed as:
Program = algorithm + data structures

What is the characteristics of an algorithm ?

Each step must be exact : Precisely described.


Algorithm Must be :
 Terminate : Contain a finite number of steps.
 Effective : Provide the correct answer to the problem.
 General : Solve every instance of the problem.
MO3AZ
SAIF
Explain and write the pseudo-code for bubble sort algorithm? Explain in detail the running time
analysis and time complexity?

The simplest sorting algorithm that works by repeatedly swapping Example :


the adjacent elements if they are in the wrong order.
3 1 4 2
Time Complexity in any case : O( )
3 1 4 2 3 > 1 - Swap

Pseudocode : 1 3 4 2 3 > 4 - No Swap

1 3 4 2 4 > 2 - Swap
function bubbleSort(arr , n ):
1 3 2 4 1 > 3 - No Swap
for i from 0 to n-1:
for j from 0 to n-i-1: 1 3 2 4 3 > 2 - Swap

if arr[j] > arr[j+1]: 1 2 3 4 1 > 2 - No Swap

swap(arr[j], arr[j+1])
1 2 3 4

MO3AZ
SAIF
Explain and write the pseudo-code for Selection sort algorithm? Explain in detail the running
time analysis and time complexity?

Example :
A simple sorting algorithm that works by repeatedly selecting the
3 1 4 2
smallest element from the unsorted portion of an array and places
Min = 3 3>1 => Min
it at the beginning. =1
Min = 1 1>4 => Min
Time Complexity in any case : O( ) =1
Min = 1 1>2 => Min
Pseudocode : = 11 3 4 2
Pass=#1
Min 3 : Min3=>14 - Swap(
=> 3Min
,1)
function selectionSort( arr , n ) =3
Min = 3 3>2 => Min
for i from 0 to n - 1 =2
1 #2 : Min
Pass 2 =2 - 4Swap( 3 ,32 )
minIndex = i
Min = 4 4>3 => Min
for j from i+1 to n =3
Pass #3 : Min = 3 - Swap( 4 , 3 )
if arr[j] < arr[minIndex] 1 2 3 4
minIndex = j
1 2 3 4
swap(arr[i], arr[minIndex])
MO3AZ
SAIF
Explain and write the pseudo-code for Insertion sort algorithm? Explain in detail the running
time analysis and time complexity?

A sorting algorithm that works by considering one element at a Example :


3 1 4 2
time and inserting it into its correct position.
Time Complexity :
3 1 4 2
Best Case : O( n ) , [ array is already order ] Shift element ( 3 ) and insert key (1)
Worst Case : O( ) , [ array in reversed order ] into its correct position

Pseudocode : 1 3 4 2

function insertionSort(arr , n): No elements shifted and the key (4)


is in the correct position
for i from 1 to n:
key = arr[i]
1 3 4 2
j=i-1
while j >= 0 and arr[j] > key: Shift elements ( 4 , 3 ) and insert key
arr[j + 1] = arr[j] (2) into its correct position
j=j-1 1 2 3 4
arr[j + 1] = key MO3AZ
SAIF
Explain and write the pseudo-code for Merge sort algorithm? Explain in detail the running time
analysis and time complexity? function MergeSort(arr , l , r ):
if l < r
m = ( l + r )/2
A sorting algorithm that works by dividing an array into smaller MergeSort(arr , l , m )
MergeSort(arr , m+1 , r )
subarrays, sorting each subarray, and then merging the sorted Merge(arr , l , m , r )
subarrays back together to form the final sorted array. function Merge(arr , l , m , r ):
n1 = m - l + 1 , n2 = r – m , left[n1] , right[n2]
Time Complexity in any case : O( )
for i from 0 to n1:
left[i] = arr[l+i]
Example : for j from 0 to n2:
3 1 4 2 right[j] = arr[m+j+1]
i=0,j=0,k=l
while i < n1 and j < n2
3 1 4 2 if left[i] <= right[j]
arr[k] = left[i] , i++
else
3 1 4 2
arr[k] = right[j] , j++
k++
1 3 2 4 while i < n1:
arr[k] = left[i] , i++ , k++
while j < n2:
1 2 3 4 arr[k] = right[j] , j++ , k++ MO3AZ
SAIF
MISSION COMPLETED !

MO3AZ

You might also like