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

Sorting Algorithm Performance Report

Uploaded by

221441
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Sorting Algorithm Performance Report

Uploaded by

221441
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

1

Sorting Algorithm Report

Name:Muhammad Saqlain

Section: BSCS 5B

Roll Number: 221441


2

Analysis of Sorting Algorithm Performance on


Loan Grant Data

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.

2. Sorting Algorithms Overview


2.1 Insertion Sort
Insertion Sort is a simple, intuitive algorithm that builds a sorted array (or
list) one item at a time. The algorithm works by iterating through the input data,
comparing the current element with the previous ones, and inserting it into the correct
position within the sorted section. Though its time complexity is quadratic (O(n²)), it
performs efficiently for small datasets or datasets that are nearly sorted.

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.

2.2 Merge Sort


Merge Sort is a more sophisticated sorting algorithm that uses the divide-and-
conquer paradigm. It divides the input array into two halves, recursively sorts each half,
and then merges the two sorted halves to produce the final sorted array. Merge Sort offers
a significant advantage over Insertion Sort in terms of time complexity, performing at O(n
log n) in all cases.

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.

3. Comparison of Running Times


This section presents a comparison of the running times for Insertion Sort and Merge
Sort based on data extracted from the loan grant dataset. The performance was measured
for both ascending and descending order sorting. The table below summarizes the
running times (in seconds) for both Insertion and Merge Sorts across various attributes:
4

Attribute Insertion Insertion Merge Sort Merge Sort


Name (Ascending) (Descending) (Ascending) (Descending)

Current 53.486 30.869 0.131 0.133


Loan
Amount

Credit 22.298 9.891 0.067 0.062


Score

Annual 22.409 15.513 0.071 0.067


Income

Monthly 22.886 28.618 0.122 0.133


Debt

Year in 28.143 24.968 0.133 0.114


Credit

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.

4.2 Merge Sort: Consistent Efficiency


Merge Sort’s O(n log n) complexity enables it to handle larger datasets more efficiently.
The low execution times across all attributes indicate that the algorithm is well-suited for
sorting tasks involving large amounts of data. One of the key strengths of Merge Sort is its
consistency. Unlike Insertion Sort, the performance remains steady regardless of the
initial ordering of the dataset, making it a preferred option in scenarios where
performance predictability is crucial.
6
7
8
9
10
11

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

// Function to load attribute data from CSV file

Function LoadAttributeData(filename, attributeIndex):

Define attributeData as empty list

Open file (filename)

Skip the first line (header)

// Loop through each line in the file

while not end of file:

Read a line from file

Split the line by commas into values

Extract the value at position 'attributeIndex'

Attempt to convert value to integer

if conversion succeeds:

Add the numeric value to attributeData

else:

Ignore the line (non-numeric value)

Close file

Return attributeData

// Function to display sorted data

Function DisplaySortedData(sortMethod, attributeName, sortedData):


13
Print sortMethod, "for", attributeName, ":"

for each number in sortedData:

Print number

// Insertion Sort (Ascending Order)

Function InsertionSortAscending(arr):

n = size of arr

for i = 1 to n-1:

key = arr[i]

j = i - 1

while j >= 0 and arr[j] > key:

arr[j + 1] = arr[j]

j = j - 1

arr[j + 1] = key

// Insertion Sort (Descending Order)

Function InsertionSortDescending(arr):

n = size of arr

for i = 1 to n-1:

key = arr[i]

j = i - 1

while j >= 0 and arr[j] < key:

arr[j + 1] = arr[j]

j = j - 1

arr[j + 1] = key

// Merge Sort (Ascending Order)

Function MergeSortAscending(arr, left, right):


14
if left < right:

mid = (left + right) / 2

Call MergeSortAscending(arr, left, mid)

Call MergeSortAscending(arr, mid + 1, right)

Call MergeAscending(arr, left, mid, right)

Function MergeAscending(arr, left, mid, right):

n1 = mid - left + 1

n2 = right - mid

Define L = new array of size n1

Define R = new array of size n2

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

while i < n1 and j < n2:

if L[i] <= R[j]:

arr[k] = L[i]

i = i + 1

else:

arr[k] = R[j]

j = j + 1

k = k + 1

while i < n1:

arr[k] = L[i]

i = i + 1
15
k = k + 1

while j < n2:

arr[k] = R[j]

j = j + 1

k = k + 1

// Merge Sort (Descending Order)

Function MergeSortDescending(arr, left, right):

if left < right:

mid = (left + right) / 2

Call MergeSortDescending(arr, left, mid)

Call MergeSortDescending(arr, mid + 1, right)

Call MergeDescending(arr, left, mid, right)

Function MergeDescending(arr, left, mid, right):

n1 = mid - left + 1

n2 = right - mid

Define L = new array of size n1

Define R = new array of size n2

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

while i < n1 and j < n2:

if L[i] >= R[j]:

arr[k] = L[i]
16
i = i + 1

else:

arr[k] = R[j]

j = j + 1

k = k + 1

while i < n1:

arr[k] = L[i]

i = i + 1

k = k + 1

while j < n2:

arr[k] = R[j]

j = j + 1

k = k + 1

// Main Program Logic

Function main:

Define filename = "loangrant.csv"

Define attributeIndex = 18 // Index of the column to sort

Define attributeNames = [list of attribute names]

// Validate attribute index

if attributeIndex is invalid:

Print "Invalid attribute index."

Exit program

// Load attribute data from CSV

Define data = LoadAttributeData(filename, attributeIndex)

// If data is empty, print error and exit

if data is empty:
17
Print "No valid numeric data found for the specified attribute index."

Exit program

// Sorting and timing algorithms

Define start, end, insertionAscTime, insertionDescTime, mergeAscTime, mergeDescTime

// Insertion Sort (Ascending)

Define insertionAsc = Copy of data

start = current time

Call InsertionSortAscending(insertionAsc)

end = current time

insertionAscTime = Calculate elapsed time (end - start)

// Insertion Sort (Descending)

Define insertionDesc = Copy of data

start = current time

Call InsertionSortDescending(insertionDesc)

end = current time

insertionDescTime = Calculate elapsed time (end - start)

// Merge Sort (Ascending)

Define mergeAsc = Copy of data

start = current time

Call MergeSortAscending(mergeAsc, 0, length of mergeAsc - 1)

end = current time

mergeAscTime = Calculate elapsed time (end - start)

// Merge Sort (Descending)

Define mergeDesc = Copy of data

start = current time

Call MergeSortDescending(mergeDesc, 0, length of mergeDesc - 1)

end = current time


18
mergeDescTime = Calculate elapsed time (end - start)

// Display sorted results for each method

Print "Sorted Results:"

Call DisplaySortedData("Insertion Sort (Ascending)", attributeNames[attributeIndex], insertionAsc)

Call DisplaySortedData("Insertion Sort (Descending)", attributeNames[attributeIndex], insertionDesc)

Call DisplaySortedData("Merge Sort (Ascending)", attributeNames[attributeIndex], mergeAsc)

Call DisplaySortedData("Merge Sort (Descending)", attributeNames[attributeIndex], mergeDesc)

// Display execution times for each method

Print "Execution Times:"

Print "Insertion Sort (Ascending):", insertionAscTime, "seconds"

Print "Insertion Sort (Descending):", insertionDescTime, "seconds"

Print "Merge Sort (Ascending):", mergeAscTime, "seconds"

Print "Merge Sort (Descending):", mergeDescTime, "seconds"

End Program

You might also like