Sorting Algorithm Performance Report
Sorting Algorithm Performance Report
Name:Muhammad Saqlain
Section: BSCS 5B
1. Introduction
Sorting algorithms are a fundamental aspect of computer science, crucial for
organizing and retrieving data efficiently. Sorting is often one of the first problems
introduced in computer science curricula due to its versatility and importance in various
applications. From financial markets, where timely access to sorted data can be the
difference between profit and loss, to the organization of massive databases for social
media platforms, the performance of sorting algorithms has far-reaching implications.
This report analyzes the performance of two sorting algorithms, Insertion Sort and Merge
Sort, when applied to a real-world dataset of loan grants.
The focus of this analysis is to compare the running times of Insertion Sort and
Merge Sort on multiple attributes of loan grant data, such as 'Current Loan Amount' and
'Credit Score'. The performance is evaluated for both ascending and descending orders.
The aim is to assess the practical efficiency of these algorithms for sorting real-world data
and determine which algorithm performs better depending on the dataset size and
structure.
Algorithmic Steps:
1. Start from the second element in the array, treating the first element as the sorted
sub-array.
2. Compare the current element with the previous elements and shift the larger
elements one position to the right.
3
3. Insert the current element into its correct position.
4. Repeat the process until the entire array is sorted.
Time Complexity:
• Best case: O(n) – when the array is already sorted.
• Worst case: O(n²) – when the array is sorted in reverse order.
• Average case: O(n²).
Although the time complexity is not favorable for large datasets, Insertion Sort can
be more efficient for smaller datasets or when working with nearly sorted data.
Algorithmic Steps:
1. Divide the array into two halves.
2. Recursively apply merge sort to both halves.
3. Merge the two sorted halves to produce the final sorted array.
Time Complexity:
• Best case: O(n log n).
• Worst case: O(n log n).
• Average case: O(n log n).
Merge Sort is particularly efficient for large datasets due to its consistent
performance regardless of the initial order of the input array. It is widely considered
a preferred option for sorting large datasets in real-world applications.
The table clearly shows that Merge Sort is significantly faster than Insertion Sort,
especially when dealing with larger datasets like 'Current Loan Amount'. The execution
times for Insertion Sort increase dramatically for larger datasets, whereas Merge Sort
remains efficient and consistent.
4. Performance Observations
4.1 Insertion Sort: Practical Efficiency
Insertion Sort, despite its O(n²) time complexity, shows decent
performance on smaller datasets or data that is nearly sorted. For example, attributes
such as 'Credit Score' and 'Annual Income' have smaller data sizes, and are likely to be
more naturally ordered, resulting in lower execution times. However, for larger or more
5
disordered attributes like 'Current Loan Amount', the quadratic complexity of Insertion
Sort becomes evident, with significantly higher running times.
5. Conclusion
In conclusion, the performance analysis of the sorting algorithms Insertion
Sort and Merge Sort reveals several key insights. While Insertion Sort is simple to
implement and effective for small or nearly sorted datasets, it becomes inefficient when
applied to large, unordered datasets. In contrast, Merge Sort, with its divide-and-conquer
approach, consistently outperforms Insertion Sort on larger datasets. The minimal
variation in running times between ascending and descending orders further
demonstrates Merge Sort’s robustness.
12
In practical applications, Merge Sort should be the preferred choice for sorting large
datasets or data that is expected to arrive in random order. On the other hand, for small
datasets or when near-order is already established, Insertion Sort can be sufficient and
faster due to its lower constant factors. Understanding the strengths and limitations of
each algorithm in different contexts allows for optimized performance in sorting tasks.
Start Program
if conversion succeeds:
else:
Close file
Return attributeData
Print number
Function InsertionSortAscending(arr):
n = size of arr
for i = 1 to n-1:
key = arr[i]
j = i - 1
arr[j + 1] = arr[j]
j = j - 1
arr[j + 1] = key
Function InsertionSortDescending(arr):
n = size of arr
for i = 1 to n-1:
key = arr[i]
j = i - 1
arr[j + 1] = arr[j]
j = j - 1
arr[j + 1] = key
n1 = mid - left + 1
n2 = right - mid
for i = 0 to n1-1:
L[i] = arr[left + i]
for j = 0 to n2-1:
R[j] = arr[mid + 1 + j]
i = 0, j = 0, k = left
arr[k] = L[i]
i = i + 1
else:
arr[k] = R[j]
j = j + 1
k = k + 1
arr[k] = L[i]
i = i + 1
15
k = k + 1
arr[k] = R[j]
j = j + 1
k = k + 1
n1 = mid - left + 1
n2 = right - mid
for i = 0 to n1-1:
L[i] = arr[left + i]
for j = 0 to n2-1:
R[j] = arr[mid + 1 + j]
i = 0, j = 0, k = left
arr[k] = L[i]
16
i = i + 1
else:
arr[k] = R[j]
j = j + 1
k = k + 1
arr[k] = L[i]
i = i + 1
k = k + 1
arr[k] = R[j]
j = j + 1
k = k + 1
Function main:
if attributeIndex is invalid:
Exit program
if data is empty:
17
Print "No valid numeric data found for the specified attribute index."
Exit program
Call InsertionSortAscending(insertionAsc)
Call InsertionSortDescending(insertionDesc)
End Program