APA - Lab.2 - Report
APA - Lab.2 - Report
REPORT
Laboratory work nr.2
at Algorithms and Algorithm Design
Performed by:
st. gr. FAF-161 V.Metei
Verified by:
a.univ. I.Costiuc
Chiinu, 2017
Empiric Analysis and Time Complexity:
4. Merge Sort
Random array
Ascending array
Descending array
()
2. Quick Sort
Random array
Ascending array
Descending array
Worst Case: The worst case occurs when the partition process always picks
greatest or smallest element as pivot. If we consider partition strategy I have
implemented where first element is always picked as pivot, the worst case occur
when the array is already sorted in ascending or descending order. Following is the
recurrence for worst case:
() = ( 1) + ()
(2 )
Best case: The best case occurs when the array is unsorted, that means the
pivot will not be greatest or smallest value in the array. Following is the recurrence
for best case:
() = 2 ( ) + ()
2
The solution of above recurrence is:
()
Average case: The average case for quick sort acts as the best case. The time
complexity will be:
()
3. Heap Sort
Random array
Ascending array
Descending array
()
Thats why overall time complexity of heap sort is () for all three
cases(best, average and worst).
Conclusion:
The algorithms I implemented are: Merge, Quick and Heap Sort. For each of
them I gave a random array, an ascending array and a descending array. I
mentioned this thing because the obtained results depend on the input I gave.
Merge Sort has the same time complexity for all 3 cases. It makes no
difference between best, average and worst case.
Heap Sort acts as Merge Sort with the same time complexity in all 3 cases.
Quick Sort makes a difference between best and worst case. In case the input
will be a random array this algorithm of sorting works better and have a more
favourable time complexity. If the input is an array sorted in ascending or
descending order Quick Sort will have a worse time complexity.
According to this results I want to mention that a certain algorithm may have
the best time complexity for an input, but it may have a worse time complexity for
another type of the input. Thats why we should choose the algorithm in
dependence of the data we have.