-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09c7c4c
commit 69acba4
Showing
4 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions
31
Day 2/Issue 7/Parth/Sorting_Comparison/plot_performance.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import subprocess | ||
import matplotlib.pyplot as plt | ||
|
||
# Compile the C program | ||
subprocess.run(["gcc", "sorting_algorithms.c", "-o", "sorting_algorithms"]) | ||
|
||
# Run the C program and capture output | ||
result = subprocess.run(["./sorting_algorithms"], capture_output=True, text=True) | ||
|
||
# Parse the output to get performance metrics (time taken for each algorithm) | ||
output_lines = result.stdout.splitlines() | ||
bubble_sort_time = float(output_lines[0].split(': ')[1].strip().split()[0]) | ||
quick_sort_time = float(output_lines[1].split(': ')[1].strip().split()[0]) | ||
selection_sort_time = float(output_lines[2].split(': ')[1].strip().split()[0]) | ||
heap_sort_time = float(output_lines[3].split(': ')[1].strip().split()[0]) | ||
merge_sort_time = float(output_lines[4].split(': ')[1].strip().split()[0]) | ||
radix_sort_time = float(output_lines[5].split(': ')[1].strip().split()[0]) | ||
|
||
# Plotting | ||
algorithms = ['Bubble Sort', 'Quick Sort', 'Selection Sort', 'Heap Sort', 'Merge Sort', 'Radix Sort'] | ||
times = [bubble_sort_time, quick_sort_time, selection_sort_time, heap_sort_time, merge_sort_time, radix_sort_time] | ||
|
||
plt.figure(figsize=(12, 8)) | ||
plt.bar(algorithms, times, color=['blue', 'green', 'orange', 'red', 'purple', 'cyan']) | ||
plt.ylabel('Time (seconds)') | ||
plt.title('Performance of Sorting Algorithms') | ||
plt.ylim(0, max(times) * 1.2) # Adjust y-axis limit for better visualization | ||
plt.grid(axis='y', linestyle='--', alpha=0.7) | ||
plt.xticks(rotation=45) | ||
plt.tight_layout() | ||
plt.show() |
248 changes: 248 additions & 0 deletions
248
Day 2/Issue 7/Parth/Sorting_Comparison/sorting_algorithms.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
|
||
void swap(int* a, int* b) { | ||
int temp = *a; | ||
*a = *b; | ||
*b = temp; | ||
} | ||
|
||
void bubbleSort(int arr[], int n) { | ||
for (int i = 0; i < n-1; i++) { | ||
for (int j = 0; j < n-i-1; j++) { | ||
if (arr[j] > arr[j+1]) { | ||
swap(&arr[j], &arr[j+1]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
int partition(int arr[], int low, int high) { | ||
int pivot = arr[high]; | ||
int i = low - 1; | ||
|
||
for (int j = low; j <= high - 1; j++) { | ||
if (arr[j] <= pivot) { | ||
i++; | ||
swap(&arr[i], &arr[j]); | ||
} | ||
} | ||
swap(&arr[i + 1], &arr[high]); | ||
return (i + 1); | ||
} | ||
|
||
void quickSort(int arr[], int low, int high) { | ||
if (low < high) { | ||
int pi = partition(arr, low, high); | ||
|
||
quickSort(arr, low, pi - 1); | ||
quickSort(arr, pi + 1, high); | ||
} | ||
} | ||
|
||
void selectionSort(int arr[], int n) { | ||
int min_idx; | ||
for (int i = 0; i < n-1; i++) { | ||
min_idx = i; | ||
for (int j = i+1; j < n; j++) { | ||
if (arr[j] < arr[min_idx]) { | ||
min_idx = j; | ||
} | ||
} | ||
swap(&arr[min_idx], &arr[i]); | ||
} | ||
} | ||
|
||
void heapify(int arr[], int n, int i) { | ||
int largest = i; | ||
int l = 2*i + 1; | ||
int r = 2*i + 2; | ||
|
||
if (l < n && arr[l] > arr[largest]) | ||
largest = l; | ||
|
||
if (r < n && arr[r] > arr[largest]) | ||
largest = r; | ||
|
||
if (largest != i) { | ||
swap(&arr[i], &arr[largest]); | ||
heapify(arr, n, largest); | ||
} | ||
} | ||
|
||
void heapSort(int arr[], int n) { | ||
for (int i = n / 2 - 1; i >= 0; i--) | ||
heapify(arr, n, i); | ||
|
||
for (int i = n - 1; i > 0; i--) { | ||
swap(&arr[0], &arr[i]); | ||
heapify(arr, i, 0); | ||
} | ||
} | ||
|
||
void merge(int arr[], int l, int m, int r) { | ||
int i, j, k; | ||
int n1 = m - l + 1; | ||
int n2 = r - m; | ||
|
||
int L[n1], R[n2]; | ||
|
||
for (i = 0; i < n1; i++) | ||
L[i] = arr[l + i]; | ||
for (j = 0; j < n2; j++) | ||
R[j] = arr[m + 1 + j]; | ||
|
||
i = 0; | ||
j = 0; | ||
k = l; | ||
while (i < n1 && j < n2) { | ||
if (L[i] <= R[j]) { | ||
arr[k] = L[i]; | ||
i++; | ||
} else { | ||
arr[k] = R[j]; | ||
j++; | ||
} | ||
k++; | ||
} | ||
|
||
while (i < n1) { | ||
arr[k] = L[i]; | ||
i++; | ||
k++; | ||
} | ||
|
||
while (j < n2) { | ||
arr[k] = R[j]; | ||
j++; | ||
k++; | ||
} | ||
} | ||
|
||
void mergeSort(int arr[], int l, int r) { | ||
if (l < r) { | ||
int m = l + (r - l) / 2; | ||
|
||
mergeSort(arr, l, m); | ||
mergeSort(arr, m + 1, r); | ||
|
||
merge(arr, l, m, r); | ||
} | ||
} | ||
|
||
int getMax(int arr[], int n) { | ||
int mx = arr[0]; | ||
for (int i = 1; i < n; i++) | ||
if (arr[i] > mx) | ||
mx = arr[i]; | ||
return mx; | ||
} | ||
|
||
void countSort(int arr[], int n, int exp) { | ||
int output[n]; | ||
int i, count[10] = {0}; | ||
|
||
for (i = 0; i < n; i++) | ||
count[(arr[i] / exp) % 10]++; | ||
|
||
for (i = 1; i < 10; i++) | ||
count[i] += count[i - 1]; | ||
|
||
for (i = n - 1; i >= 0; i--) { | ||
output[count[(arr[i] / exp) % 10] - 1] = arr[i]; | ||
count[(arr[i] / exp) % 10]--; | ||
} | ||
|
||
for (i = 0; i < n; i++) | ||
arr[i] = output[i]; | ||
} | ||
|
||
void radixSort(int arr[], int n) { | ||
int m = getMax(arr, n); | ||
|
||
for (int exp = 1; m / exp > 0; exp *= 10) | ||
countSort(arr, n, exp); | ||
} | ||
|
||
int main() { | ||
int arr[10000]; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
|
||
// Initialize array with random numbers | ||
srand(time(NULL)); | ||
for (int i = 0; i < n; i++) { | ||
arr[i] = rand() % 10000; | ||
} | ||
|
||
clock_t start, end; | ||
double elapsedTime; | ||
|
||
// Measure time for Bubble Sort | ||
int bubbleSortArray[n]; | ||
for (int i = 0; i < n; i++) { | ||
bubbleSortArray[i] = arr[i]; | ||
} | ||
start = clock(); | ||
bubbleSort(bubbleSortArray, n); | ||
end = clock(); | ||
elapsedTime = ((double) (end - start)) / CLOCKS_PER_SEC; | ||
printf("Bubble Sort Time: %.6f seconds\n", elapsedTime); | ||
|
||
// Measure time for Quick Sort | ||
int quickSortArray[n]; | ||
for (int i = 0; i < n; i++) { | ||
quickSortArray[i] = arr[i]; | ||
} | ||
start = clock(); | ||
quickSort(quickSortArray, 0, n-1); | ||
end = clock(); | ||
elapsedTime = ((double) (end - start)) / CLOCKS_PER_SEC; | ||
printf("Quick Sort Time: %.6f seconds\n", elapsedTime); | ||
|
||
// Measure time for Selection Sort | ||
int selectionSortArray[n]; | ||
for (int i = 0; i < n; i++) { | ||
selectionSortArray[i] = arr[i]; | ||
} | ||
start = clock(); | ||
selectionSort(selectionSortArray, n); | ||
end = clock(); | ||
elapsedTime = ((double) (end - start)) / CLOCKS_PER_SEC; | ||
printf("Selection Sort Time: %.6f seconds\n", elapsedTime); | ||
|
||
// Measure time for Heap Sort | ||
int heapSortArray[n]; | ||
for (int i = 0; i < n; i++) { | ||
heapSortArray[i] = arr[i]; | ||
} | ||
start = clock(); | ||
heapSort(heapSortArray, n); | ||
end = clock(); | ||
elapsedTime = ((double) (end - start)) / CLOCKS_PER_SEC; | ||
printf("Heap Sort Time: %.6f seconds\n", elapsedTime); | ||
|
||
// Measure time for Merge Sort | ||
int mergeSortArray[n]; | ||
for (int i = 0; i < n; i++) { | ||
mergeSortArray[i] = arr[i]; | ||
} | ||
start = clock(); | ||
mergeSort(mergeSortArray, 0, n-1); | ||
end = clock(); | ||
elapsedTime = ((double) (end - start)) / CLOCKS_PER_SEC; | ||
printf("Merge Sort Time: %.6f seconds\n", elapsedTime); | ||
|
||
// Measure time for Radix Sort | ||
int radixSortArray[n]; | ||
for (int i = 0; i < n; i++) { | ||
radixSortArray[i] = arr[i]; | ||
} | ||
start = clock(); | ||
radixSort(radixSortArray, n); | ||
end = clock(); | ||
elapsedTime = ((double) (end - start)) / CLOCKS_PER_SEC; | ||
printf("Radix Sort Time: %.6f seconds\n", elapsedTime); | ||
|
||
return 0; | ||
} |
Binary file not shown.