0% found this document useful (0 votes)
151 views

APA - Lab.2 - Report

This document summarizes the results of a laboratory work analyzing the time complexity of three sorting algorithms: Merge Sort, Quick Sort, and Heap Sort. Testing with random, ascending, and descending arrays of 10,000; 50,000; and 100,000 elements showed that: 1) Merge Sort has consistent O(n log n) time complexity for all input types. 2) Quick Sort's time complexity varies from O(n log n) on average/best case to O(n^2) on worst case, depending on input. 3) Heap Sort also has consistent O(n log n) time complexity for all input types. The conclusion is that the most

Uploaded by

Mihai Lungu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views

APA - Lab.2 - Report

This document summarizes the results of a laboratory work analyzing the time complexity of three sorting algorithms: Merge Sort, Quick Sort, and Heap Sort. Testing with random, ascending, and descending arrays of 10,000; 50,000; and 100,000 elements showed that: 1) Merge Sort has consistent O(n log n) time complexity for all input types. 2) Quick Sort's time complexity varies from O(n log n) on average/best case to O(n^2) on worst case, depending on input. 3) Heap Sort also has consistent O(n log n) time complexity for all input types. The conclusion is that the most

Uploaded by

Mihai Lungu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Ministry of Education of the Republic of Moldova

Technical University of Moldova

Software Engineering and Automatics Department

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

Elements Comparisons Swaps Time of execution


10000 120030 74709 0.234000 s
50000 717358 430611 1.327002 s
100000 1535027 912015 2.823604 s

Ascending array

Elements Comparisons Swaps Time of execution


10000 77619 88624 0.202800 s
50000 450941 556294 1.138802 s
100000 949809 1213783 2.386804 s

Descending array

Elements Comparisons Swaps Time of execution


10000 69008 133616 0.187200 s
50000 401952 784464 1.125202 s
100000 853904 1668928 2.419004 s
Merge Sort is a recursive algorithm and time complexity can be expressed as
following recurrence relation:

() = 2 ( ) + ()
2
The above recurrence can be solved and the obtained result is:

()

Time complexity of Merge Sort is considered to be


() in all 3 cases(worst, average and best) as merge sort always divides the
array in two halves and take linear time to merge two halves.

2. Quick Sort

Random array

Elements Comparisons Swaps Time of execution


10000 322578 24051 0.187200 s
50000 5644709 122899 2.894807 s
100000 21775256 245084 10.963021 s

Ascending array

Elements Comparisons Swaps Time of execution


10000 1250454 9799 0.561600 s
50000 11220617 49799 5.116809 s
100000 34892573 99799 16.017831 s

Descending array

Elements Comparisons Swaps Time of execution


10000 495567 99999 0.603476 s
50000 12475079 49999 5.894532 s
100000 36958324 999999 16.256789 s
The time taken by Quick Sort depends upon the input array and partition
strategy. Following are three cases:

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) + ()

The solution of above recurrence is:

(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

Elements Comparisons Swaps Time of execution


10000 165279 123835 0.343200 s
50000 1000345 735130 2.059203 s
100000 2149022 1569736 4.676409 s

Ascending array

Elements Comparisons Swaps Time of execution


10000 152680 117556 0.312000 s
50000 894013 680509 1.827203 s
100000 1915169 1446362 3.917607 s

Descending array

Elements Comparisons Swaps Time of execution


10000 151181 115574 0.314000 s
50000 924029 691640 1.872003 s
100000 1995210 1484590 4.119407 s
Time complexity of heap sort can be expressed as following recurrence
relation:

() = 2 ( ) + ()
2
The above recurrence can be solved and the obtained result is:

()

Thats why overall time complexity of heap sort is () for all three
cases(best, average and worst).
Conclusion:

During this laboratory work I implemented 3 sorting algorithms which are


using divide et impera method. This technique implies to divide the initial
problem into sub-problems. Solving the sub-problems we will obtain some results
which combined together will represent the result of our initial problem.

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.

You might also like