Sure, let's implement and compare the performance of the mentioned sorting algorithms. We'll use Python for the implementation and the time module to measure execution time. We'll also analyze the time complexity of each algorithm and discuss the results.
Implementation of Sorting Algorithms
import time
import random
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i+1, n):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
return arr
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i-1
while j >=0 and key < arr[j] :
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
return arr
def partition(arr, low, high):
i = (low-1)
pivot = arr[high]
for j in range(low, high):
if arr[j] <= pivot:
i = i+1
arr[i], arr[j] = arr[j], arr[i]
arr[i+1], arr[high] = arr[high], arr[i+1]
return (i+1)
def quick_sort(arr, low, high):
if low < high:
pi = partition(arr, low, high)
quick_sort(arr, low, pi-1)
quick_sort(arr, pi+1, high)
return arr
def measure_time(sort_func, arr):
start_time = time.time()
sort_func(arr)
end_time = time.time()
return end_time - start_time
# Generate large datasets
data_sizes = [10000, 100000, 1000000] # 10,000 to 1,000,000
results = {}
for size in data_sizes:
data = [random.randint(0, size) for _ in range(size)]
algorithms = {
'Bubble Sort': bubble_sort,
'Selection Sort': selection_sort,
'Insertion Sort': insertion_sort,
'Quick Sort': lambda arr: quick_sort(arr, 0, len(arr)-1)
}
results[size] = {}
for name, algorithm in algorithms.items():
# Copy the data for each algorithm to ensure a fair comparison
copied_data = data.copy()
time_taken = measure_time(algorithm, copied_data)
results[size][name] = time_taken
# Print results
for size, algo_results in results.items():
print(f"Data size: {size}")
for algo, time_taken in algo_results.items():
print(f"{algo}: {time_taken:.6f} seconds")
print()
Time Complexity Analysis
-
Bubble Sort:
- Worst-case and average time complexity: (O(n^2))
- Best-case time complexity: (O(n)) (when the array is already sorted)
-
Selection Sort:
- Worst-case and average time complexity: (O(n^2))
- Best-case time complexity: (O(n^2)) (does not benefit from being partially sorted)
-
Insertion Sort:
- Worst-case and average time complexity: (O(n^2))
- Best-case time complexity: (O(n)) (when the array is already sorted)
-
Quick Sort:
- Worst-case time complexity: (O(n^2)) (rare, occurs when the pivot is the smallest or largest element each time)
- Average time complexity: (O(n \log n))
- Best-case time complexity: (O(n \log n))
Analysis of Results
Running the above code will give you the execution times for each algorithm on datasets of sizes 10,000, 100,000, and 1,000,000. Here’s an example of what the results might look like:
Data size: 10000
Bubble Sort: 5.678912 seconds
Selection Sort: 3.456789 seconds
Insertion Sort: 2.345678 seconds
Quick Sort: 0.123456 seconds
Data size: 100000
Bubble Sort: 567.891234 seconds
Selection Sort: 345.678912 seconds
Insertion Sort: 234.567891 seconds
Quick Sort: 1.234567 seconds
Data size: 1000000
Bubble Sort: 56789.123456 seconds
Selection Sort: 34567.891234 seconds
Insertion Sort: 23456.789123 seconds
Quick Sort: 12.345678 seconds
Conclusions
From the results, it's clear that:
- Bubble Sort and Selection Sort have the highest execution times, especially for larger datasets, due to their (O(n^2)) time complexity.
- Insertion Sort performs slightly better than Bubble Sort and Selection Sort for larger datasets but is still significantly slower than Quick Sort.
- Quick Sort consistently outperforms the other algorithms, especially for larger datasets, due to its average-case time complexity of (O(n \log n)).
In conclusion, for large datasets, algorithms with (O(n \log n)) complexity like Quick Sort are much more efficient than (O(n^2)) algorithms like Bubble Sort, Selection Sort, and Insertion Sort.