10/16/2026

Sorting Algorithms used in Computer Science

  • Various sorting algorithms are used in many programs built and analyzed by developers and data scientists to search through data sets and large databases to retrieve and sort items in different contexts.
  • These sorting algorithms generally have varying speeds, performance, and retrieval times and have better and worse uses depending on what they are needed for.

Main Question/Idea

  • What sorting algorithms work best for insertions, deletions, and ordering?

  • We can check correlation between speeds and times of different algorithms, averages of algorithm speeds, and make comparisons between algorithm testing data.

  • An example of this could be using R to check the difference in average speeds between two algorithms used to sort data. We could take the results of using Merge Sort and Selection Sort on the same data, find their average speeds, and compare them to find which might work better in the context of the current system.

  • For the following slides I will be using data from the Sorting_Algorithms.csv file found from the “Sorting Algorithms Performance Evaluation” post on kaggle.com uploaded by user Md. Ismiel Hossen Abir. This data includes test runs of different common sorting algorithms, their completion times during tests, and the memory usage by the system when running each algorithm.

Average Difference in Speed between Algorithms

  • We can find the difference in average speed between algorithms by taking the difference of the average completion times for the first algorithm over the average completion time of the second algorithm.

\[ Difference In Speed = AvgTimeofAlgorithm1 - AvgTimeofAlgorithm 2 \]

  • We could also take a proportion of the two speeds to find a percentage value of how fast one algorithm is compared to another.

\[ PercentageOfSpeedOfAlgorithm1Against2 = \frac{AvgTimeOfAlgorithm1}{AvgTimeOfAlgorithm2} \]

Comparing Memory Usage and Speed

  • Another way we can analyze the algorithms using data is checking the average speed or completion time of the algorithm against the provided memory usage in kilobytes for the data to find a correlation between memory usage and algorithm speed to find how using different algorithms may need to use memory on the system differently.

\[ Effect Of Memory Usage On Speed = \frac{RuntimeOfAlgorithm}{MemUsageOfAlgorithm} \]

Plot Of Average Algorithm Speeds

This slide scrolls:

  • We can plot data such as average speeds of the algorithms to find that same algorithms are able to complete much faster than other algorithms, with Selection Sort, Insertion Sort, Odd-Even Sort, and Bubble Sort having a larger time complexity than the other algorithms. This is an important consideration in handling database searches that must be performed frequently and quickly.

R Code used for Previous Graph

# Import libraries and enter data into dataframe
library(ggplot2)
library(dplyr)
algodata <- read.csv("Sorting_Algorithm.csv")

# Determine average times of algorithms by individual algorithm
averageTimes <- algodata %>%
  group_by(algorithm) %>%
  summarise(avg_time_ms = mean(execution_time_ms), .groups = "drop")

# Create a plot of algorithms on the x-axis and their average times on the y-axis
ggplot(averageTimes, aes(x = reorder(algorithm, avg_time_ms), y = avg_time_ms)) +
  geom_bar(stat = "identity",
           fill = "maroon") +
  labs(x = "Algorithm",
       y = "Average Execution Time in Milliseconds",
       title = "Average Sorting Algorithm Runtime") +
  theme_minimal()

Plot of Average Memory Usage

This slide scrolls:

  • Memory usage is also important to take into account when determining the best uses for each sorting algorithm, faster search algorithms can lead to greater memory usage that may not work well in certain cases. Selection Sort and Insertion Sort had higher completion times but lower memory usage, while Quick Sort and Bucket Sort have faster times but higher memory usage.

Code Used for Previous Plot

# Uses libraries and import data
library(ggplot2)
library(dplyr)
algodata <- read.csv("Sorting_Algorithm.csv")

# Find average memory usage in kb for each algorithm
averageMem <- algodata %>%
  group_by(algorithm) %>%
  summarise(avgMemUsage = mean(memory_usage_kb), .groups = "drop")

# Plot algorithms by name against average memory usage
ggplot(averageMem, aes(x = reorder(algorithm, avgMemUsage), y = avgMemUsage)) +
  geom_bar(stat = "identity",
           fill = "green") +
  labs(x = "Algorithm",
       y = "Average Memory Usage in Kilobytes",
       title = "Average Memory Usage by Algorithm") +
  theme_minimal()

Interactive Plot of Completion Time Data

This slide scrolls:

  • Tools such as plotly can be used with R to create interactive plots of data. Here average completion times are displayed where the user can scroll over items to get precise numbers for the data.

Plotly Code for Interactive Plot

# load libraries / packages
library(ggplot2)
library(plotly)
library(tidyverse)

# retrieve avgtimes again
averageTimes <- algodata %>%
  group_by(algorithm) %>%
  summarise(avg_time_ms = mean(execution_time_ms), .groups = "drop")

# use plotly library to create interactive visualization of data
fig <- ggplot(averageTimes, aes(x = reorder(algorithm, avg_time_ms), y = avg_time_ms, fill = algorithm)) +
  geom_bar(stat = "identity") + 
  labs(x = "Algorithm",
       y = "Average Time in Milliseconds",
       title = "Sorting Algorithm Average Completion Times") +
  theme_minimal() +
  theme(axis.text.x = element_text(size = 6))

fig <- ggplotly(fig)
# fig      # (add fig here to show plotly graph)

Conclusion

  • Different Sorting Algorithms have different use cases and levels of efficiency based on the type of data being handled and the types of operations needed.
  • Sorting Algorithms with a greater speed and lower completion times may have increased memory usage on systems which needs to be taken into account.
  • Different types of algorithms are better for different types of applications, using R plots and visual data can help to see differences in algorithm performance based on test data.