Professional Documents
Culture Documents
FINAL (اسئلة متوقعة تيجي في الامتحان)
FINAL (اسئلة متوقعة تيجي في الامتحان)
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
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?
Pseudocode : 1 3 4 2
MO3AZ