Skip to content

Commit

Permalink
Merge pull request #37 from rampantvoid/rampantvoid-graph
Browse files Browse the repository at this point in the history
Fixes #7
  • Loading branch information
anshikasrivastava17 authored Jul 22, 2024
2 parents 51e6273 + b23fafe commit 49c3e0c
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 10 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions Day 2/Issue 7/Priyanshuu/plot_script.gp
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
170 changes: 170 additions & 0 deletions Day 2/Issue 7/Priyanshuu/sorting.c
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;
}
11 changes: 11 additions & 0 deletions Day 2/Issue 7/Priyanshuu/sorting_data.txt
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 added Day 2/Issue 7/Priyanshuu/sorting_performance
Binary file not shown.
Binary file not shown.
10 changes: 10 additions & 0 deletions Day 2/Issue 9/Priyanshu/log/sample_access.log
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)"
27 changes: 17 additions & 10 deletions Day 2/Issue 9/Priyanshu/log_archiver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,31 @@
# Configuration
LOG_SOURCE_DIR="./log"
ARCHIVE_DIR="./archive/logs"
DAYS_OLD=2
DAYS_OLD=1
SCRIPT_PATH=$(readlink -f "$0")
LOCK_FILE="/tmp/log_archiver.lock"

# Function to perform log archiving
perform_archiving() {

echo "Starting archiving process..."
echo "LOG_SOURCE_DIR: $LOG_SOURCE_DIR"
echo "ARCHIVE_DIR: $ARCHIVE_DIR"
echo "DAYS_OLD: $DAYS_OLD"

mkdir -p "$ARCHIVE_DIR"

CURRENT_DATE=$(date +"%Y-%m-%d")


ARCHIVE_FILE="logs_${CURRENT_DATE}.tar.gz"

find "$LOG_SOURCE_DIR" -type f -mtime +$DAYS_OLD -print0 | tar czvf "$ARCHIVE_DIR/$ARCHIVE_FILE" --null -T -

find "$LOG_SOURCE_DIR" -type f -mtime +$DAYS_OLD -delete


echo "Files to be archived:"
find "$LOG_SOURCE_DIR" -type f -not -newermt "-${DAYS_OLD} days" -print

echo "Creating archive..."
find "$LOG_SOURCE_DIR" -type f -not -newermt "-${DAYS_OLD} days" -print0 |
tar czvf "$ARCHIVE_DIR/$ARCHIVE_FILE" --null -T - --verbose

echo "Deleting archived files..."
find "$LOG_SOURCE_DIR" -type f -not -newermt "-${DAYS_OLD} days" -delete

echo "Log archiving completed. Archive saved as $ARCHIVE_DIR/$ARCHIVE_FILE"
}

Expand Down

0 comments on commit 49c3e0c

Please sign in to comment.