-
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.
Merge pull request #37 from rampantvoid/rampantvoid-graph
Fixes #7
- Loading branch information
Showing
8 changed files
with
214 additions
and
10 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.
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,6 @@ | ||
set title 'Sorting Algorithm Performance' | ||
set xlabel 'Array Size' | ||
set ylabel 'Time (seconds)' | ||
set key outside | ||
plot 'sorting_data.txt' using 1:2 with lines title 'Bubble Sort', '' using 1:3 with lines title 'Insertion Sort', '' using 1:4 with lines title 'Selection Sort', '' using 1:5 with lines title 'Quick Sort' | ||
pause -1 |
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,170 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
#include <string.h> | ||
|
||
#define MAX_SIZE 10000 | ||
#define STEP_SIZE 1000 | ||
#define NUM_ALGORITHMS 4 | ||
|
||
// Function prototypes | ||
void bubbleSort(int arr[], int n); | ||
void insertionSort(int arr[], int n); | ||
void selectionSort(int arr[], int n); | ||
void quickSort(int arr[], int n); | ||
void quickSortHelper(int arr[], int low, int high); | ||
int partition(int arr[], int low, int high); | ||
void generateRandomArray(int arr[], int n); | ||
void copyArray(int src[], int dest[], int n); | ||
double measureSortingTime(void (*sortFunction)(int[], int), int arr[], int n); | ||
|
||
int main() { | ||
FILE *dataFile; | ||
dataFile = fopen("sorting_data.txt", "w"); | ||
if (dataFile == NULL) { | ||
printf("Error opening file!\n"); | ||
return 1; | ||
} | ||
|
||
srand(time(NULL)); | ||
|
||
fprintf(dataFile, "Size BubbleSort InsertionSort SelectionSort QuickSort\n"); | ||
|
||
for (int size = STEP_SIZE; size <= MAX_SIZE; size += STEP_SIZE) { | ||
int originalArray[MAX_SIZE]; | ||
int tempArray[MAX_SIZE]; | ||
|
||
generateRandomArray(originalArray, size); | ||
|
||
// Measure Bubble Sort | ||
copyArray(originalArray, tempArray, size); | ||
double bubbleTime = measureSortingTime(bubbleSort, tempArray, size); | ||
|
||
// Measure Insertion Sort | ||
copyArray(originalArray, tempArray, size); | ||
double insertionTime = measureSortingTime(insertionSort, tempArray, size); | ||
|
||
// Measure Selection Sort | ||
copyArray(originalArray, tempArray, size); | ||
double selectionTime = measureSortingTime(selectionSort, tempArray, size); | ||
|
||
// Measure Quick Sort | ||
copyArray(originalArray, tempArray, size); | ||
double quickTime = measureSortingTime(quickSort, tempArray, size); | ||
|
||
fprintf(dataFile, "%d %f %f %f %f\n", size, bubbleTime, insertionTime, selectionTime, quickTime); | ||
} | ||
|
||
fclose(dataFile); | ||
|
||
// Generate gnuplot script | ||
FILE *gnuplotScript = fopen("plot_script.gp", "w"); | ||
if (gnuplotScript == NULL) { | ||
printf("Error opening gnuplot script file!\n"); | ||
return 1; | ||
} | ||
|
||
fprintf(gnuplotScript, "set title 'Sorting Algorithm Performance'\n"); | ||
fprintf(gnuplotScript, "set xlabel 'Array Size'\n"); | ||
fprintf(gnuplotScript, "set ylabel 'Time (seconds)'\n"); | ||
fprintf(gnuplotScript, "set key outside\n"); | ||
fprintf(gnuplotScript, "plot 'sorting_data.txt' using 1:2 with lines title 'Bubble Sort', \ | ||
'' using 1:3 with lines title 'Insertion Sort', \ | ||
'' using 1:4 with lines title 'Selection Sort', \ | ||
'' using 1:5 with lines title 'Quick Sort'\n"); | ||
fprintf(gnuplotScript, "pause -1\n"); | ||
|
||
fclose(gnuplotScript); | ||
|
||
// Execute gnuplot script | ||
system("gnuplot plot_script.gp"); | ||
|
||
return 0; | ||
} | ||
|
||
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]) { | ||
int temp = arr[j]; | ||
arr[j] = arr[j + 1]; | ||
arr[j + 1] = temp; | ||
} | ||
} | ||
} | ||
} | ||
|
||
void insertionSort(int arr[], int n) { | ||
for (int i = 1; i < n; i++) { | ||
int key = arr[i]; | ||
int j = i - 1; | ||
while (j >= 0 && arr[j] > key) { | ||
arr[j + 1] = arr[j]; | ||
j--; | ||
} | ||
arr[j + 1] = key; | ||
} | ||
} | ||
|
||
void selectionSort(int arr[], int n) { | ||
for (int i = 0; i < n - 1; i++) { | ||
int min_idx = i; | ||
for (int j = i + 1; j < n; j++) { | ||
if (arr[j] < arr[min_idx]) { | ||
min_idx = j; | ||
} | ||
} | ||
int temp = arr[min_idx]; | ||
arr[min_idx] = arr[i]; | ||
arr[i] = temp; | ||
} | ||
} | ||
|
||
void quickSort(int arr[], int n) { | ||
quickSortHelper(arr, 0, n - 1); | ||
} | ||
|
||
void quickSortHelper(int arr[], int low, int high) { | ||
if (low < high) { | ||
int pi = partition(arr, low, high); | ||
quickSortHelper(arr, low, pi - 1); | ||
quickSortHelper(arr, pi + 1, high); | ||
} | ||
} | ||
|
||
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++; | ||
int temp = arr[i]; | ||
arr[i] = arr[j]; | ||
arr[j] = temp; | ||
} | ||
} | ||
int temp = arr[i + 1]; | ||
arr[i + 1] = arr[high]; | ||
arr[high] = temp; | ||
return (i + 1); | ||
} | ||
|
||
void generateRandomArray(int arr[], int n) { | ||
for (int i = 0; i < n; i++) { | ||
arr[i] = rand() % 10000; | ||
} | ||
} | ||
|
||
void copyArray(int src[], int dest[], int n) { | ||
memcpy(dest, src, n * sizeof(int)); | ||
} | ||
|
||
double measureSortingTime(void (*sortFunction)(int[], int), int arr[], int n) { | ||
clock_t start, end; | ||
start = clock(); | ||
|
||
sortFunction(arr, n); | ||
|
||
end = clock(); | ||
return ((double)(end - start)) / CLOCKS_PER_SEC; | ||
} |
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,11 @@ | ||
Size BubbleSort InsertionSort SelectionSort QuickSort | ||
1000 0.001700 0.000717 0.001381 0.000126 | ||
2000 0.007950 0.001658 0.003388 0.000198 | ||
3000 0.012689 0.003255 0.006494 0.000240 | ||
4000 0.017138 0.004916 0.009141 0.000251 | ||
5000 0.022912 0.007302 0.013612 0.000309 | ||
6000 0.033369 0.010413 0.019528 0.000380 | ||
7000 0.044990 0.014056 0.026517 0.000456 | ||
8000 0.058775 0.018192 0.034575 0.000518 | ||
9000 0.075243 0.023213 0.043652 0.000587 | ||
10000 0.092617 0.028349 0.053808 0.000686 |
Binary file not shown.
Binary file not shown.
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,10 @@ | ||
192.168.1.100 - - [19/Jul/2024:10:15:32 +0000] "GET /index.html HTTP/1.1" 200 2326 "http://www.example.com" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" | ||
10.0.0.1 - - [19/Jul/2024:10:15:33 +0000] "GET /styles.css HTTP/1.1" 200 1578 "http://www.example.com/index.html" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" | ||
192.168.1.101 - - [19/Jul/2024:10:15:34 +0000] "GET /images/logo.png HTTP/1.1" 200 5432 "http://www.example.com/index.html" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Safari/605.1.15" | ||
10.0.0.2 - - [19/Jul/2024:10:15:35 +0000] "POST /login HTTP/1.1" 302 0 "http://www.example.com/login.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Mobile/15E148 Safari/604.1" | ||
192.168.1.102 - - [19/Jul/2024:10:15:36 +0000] "GET /api/user/profile HTTP/1.1" 200 1024 "http://www.example.com/dashboard" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" | ||
10.0.0.3 - - [19/Jul/2024:10:15:37 +0000] "GET /favicon.ico HTTP/1.1" 404 217 "http://www.example.com" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Firefox/115.0" | ||
192.168.1.103 - - [19/Jul/2024:10:15:38 +0000] "GET /products?category=electronics HTTP/1.1" 200 8792 "http://www.example.com/catalog" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" | ||
10.0.0.4 - - [19/Jul/2024:10:15:39 +0000] "POST /api/cart/add HTTP/1.1" 200 128 "http://www.example.com/products/item1234" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" | ||
192.168.1.104 - - [19/Jul/2024:10:15:40 +0000] "GET /blog/article/top-10-tech-trends HTTP/1.1" 200 15678 "http://www.example.com/blog" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Safari/605.1.15" | ||
10.0.0.5 - - [19/Jul/2024:10:15:41 +0000] "GET /sitemap.xml HTTP/1.1" 200 4096 "-" "Googlebot/2.1 (+http://www.google.com/bot.html)" |
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