Algorithms-section6.2
Algorithms-section6.2
Section Content:
Sequence search, Binary search & Insertion sort Algorithm
EX:
Algorithm:
Sequence search (A,k)
{
for (i=0; i<n; i++){
if(k==A[i])
return i;
end if
return -1;
}
}
Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed
Binary search (Half interval search / Logarithmic search):
A search algorithm that finds the position of a target value within a sorted array.
Binary search compares the target value to the middle element of the array. If
they are not equal, the half in which the target cannot lie is eliminated and the
search continues on the remaining half, again taking the middle element to
compare to the target value, and repeating this until the target value is found. If
the search ends with the remaining half being empty, the target is not in the array
EX:
Binary search is efficiency for sorted arrays that are sort contiguously “close
together” in memory making O (log n)
𝑳+𝑹
1st step: L= first index, R= last index, m=
𝟐
𝑳+𝑹
2nd step: L= m+1, R= last index, m=
𝟐
𝑳+𝑹
3rd step: L= m+1, R= last index, m=
𝟐
Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed
Algorithm:
1. Binary search (A,f,L,k)
2. if (L<f)
3. return -1
4. else
L+f
5. m=
2
6. if (k==A[m])
7. index=m
8. else if (k<A[m])
9. index= Binary search (A,f,m-1,k)
10.else
11. index= Binary search (A,m+1,L,k)
12.return index
Best case: when k is found in the middle element of array, then it’s found after
only one iteration O(1)
Worst case: when k isn’t found in the middle element of array, in all recursive
calls until reaching the level in which there is only one element O(log n)
Average case: when k isn’t found in the middle element of array, we have to
call the method to check the other subarray’s middle elements O(log n)
Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed
Insertion sort:
Give an unsorted or sorted in inverse order so we want to sort it using Insertion
sort, if we have an array so we will compare the 2nd element with the 1st, if it is
smaller we’ll swap else we’ll see 3rd element & compare it with 2nd if it is smaller
it will compare with 1st if it is smaller then swap else it’ll be swapped with the 2nd
element & so on.
EX:
Algorithm:
1. insertion sort (A)
2. for i=1 to length(A)
3. x=A[i]
4. j= i-1
5. While j>=0 & A[j]>x
6. A[j+1]= A[j]
7. j=j-1
8. End while
9. A[j+1]=x
10.End for
Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed
Trace:
1 0 1 2 3 4 2 i=2 to 4
5 7 0 1 2 x=A[i]= 0
j=i-1=1
i=1 to 4 j>=0 & A[1]>0 YES
x=A[i]= 7 A[1+1] = A[1] A[2]=7
j=i-1=0 j=1-1=0
j>=0 & A[0]>7 5 7 7 1 2
NO j>=0 & A[0]>0 YES
A[0+1] = A[0] A[1]=5
5 5 7 1 2
j=0-1=-1
A[-1+1]=x A[0]=0
0 5 7 1 2
3 i=3 to 4 4 i=4 to 4
x=A[i]= 1 x=A[i]= 2
j=i-1=2 j=i-1=3
j>=0 & A[2]>1 YES j>=0 & A[3]>2 YES
A[2+1] = A[2] A[3]=7 A[3+1] = A[3] A[4]=7
j=2-1=1 j=3-1=2
0 5 7 7 2 0 1 5 7 7
j>=0 & A[1]>1 YES j>=0 & A[2]>2 YES
A[1+1] = A[1] A[2]=5 A[2+1] = A[2] A[3]=5
0 5 5 7 2 0 1 5 5 7
j=1-1=0 j=2-1=1
j>=0 & A[0]>1 NO j>=0 & A[1]>2 NO
A[0+1]=x A[1]=1 A[1+1]=x A[2]=2
0 1 5 7 2 0 1 2 5 7
Best case: happen when the array is already sorted, then it will move one time
on the array O(n)
Worst case: happen when the array is sorted in reverse order O(n2)
Average case: happen when the array isn’t sorted so will move over all the
array & sort it O(n2)
Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed