Skip to content
This repository has been archived by the owner on Dec 13, 2024. It is now read-only.

Commit

Permalink
feat(code): sorting algos
Browse files Browse the repository at this point in the history
  • Loading branch information
storopoli committed Oct 5, 2024
1 parent 3753289 commit d66f00e
Show file tree
Hide file tree
Showing 11 changed files with 793 additions and 2 deletions.
3 changes: 1 addition & 2 deletions code/03-tree-detection.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ int search_cycle(int graph[N][N], int v, int *visited, int parent) {
int all_visited(int *visited) {
int i;
// Find first unvisited vertex
for (i = 0; i < N && visited[i]; ++i)
;
for (i = 0; i < N && visited[i]; ++i);
return i == N; // Return true if no unvisited vertices found, false otherwise
}

Expand Down
77 changes: 77 additions & 0 deletions code/10-bubble_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/**
* Function to perform Bubble Sort on an array.
*
* @param arr The array to sort.
* @param n The size of the array.
*/
void bubble_sort(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] and arr[j + 1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

/**
* Main function to demonstrate Bubble Sort and measure execution time.
*/
int main() {
int N_values[] = {10000, 20000, 30000, 40000};
int num_tests = sizeof(N_values) / sizeof(N_values[0]);

for (int t = 0; t < num_tests; t++) {
int N = N_values[t];
int *arr = (int *)malloc(N * sizeof(int));

// Seed the random number generator
srand(time(NULL));

// Fill the array with random integers
for (int i = 0; i < N; i++) {
arr[i] = rand() % N;
}

// Measure the start time
clock_t start_time = clock();

// Perform Bubble Sort
bubble_sort(arr, N);

// Measure the end time
clock_t end_time = clock();

// Calculate the elapsed time in seconds
double time_spent = (double)(end_time - start_time) / CLOCKS_PER_SEC;

// Verify that the array is sorted
int is_sorted = 1;
for (int i = 1; i < N; i++) {
if (arr[i - 1] > arr[i]) {
is_sorted = 0;
break;
}
}

// Print the result and time
printf("Bubble Sort with N = %d\n", N);
if (is_sorted)
printf("Array is sorted.\n");
else
printf("Array is NOT sorted.\n");
printf("Time taken: %f seconds\n\n", time_spent);

// Free the allocated memory
free(arr);
}

return 0;
}
80 changes: 80 additions & 0 deletions code/11-selection_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/**
* Function to perform Selection Sort on an array.
*
* @param arr The array to sort.
* @param n The size of the array.
*/
void selection_sort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int min_index = i;

// Find the minimum element in the unsorted portion
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min_index]) min_index = j;
}

// Swap the found minimum element with the first unsorted element
int temp = arr[min_index];
arr[min_index] = arr[i];
arr[i] = temp;
}
}

/**
* Main function to demonstrate Selection Sort and measure execution time.
*/
int main() {
int N_values[] = {10000, 20000, 30000, 40000};
int num_tests = sizeof(N_values) / sizeof(N_values[0]);

for (int t = 0; t < num_tests; t++) {
int N = N_values[t];
int *arr = (int *)malloc(N * sizeof(int));

// Seed the random number generator
srand(time(NULL));

// Fill the array with random integers
for (int i = 0; i < N; i++) {
arr[i] = rand() % N;
}

// Measure the start time
clock_t start_time = clock();

// Perform Selection Sort
selection_sort(arr, N);

// Measure the end time
clock_t end_time = clock();

// Calculate the elapsed time in seconds
double time_spent = (double)(end_time - start_time) / CLOCKS_PER_SEC;

// Verify that the array is sorted
int is_sorted = 1;
for (int i = 1; i < N; i++) {
if (arr[i - 1] > arr[i]) {
is_sorted = 0;
break;
}
}

// Print the result and time
printf("Selection Sort with N = %d\n", N);
if (is_sorted)
printf("Array is sorted.\n");
else
printf("Array is NOT sorted.\n");
printf("Time taken: %f seconds\n\n", time_spent);

// Free the allocated memory
free(arr);
}

return 0;
}
79 changes: 79 additions & 0 deletions code/12-insertion_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/**
* Function to perform Insertion Sort on an array.
*
* @param arr The array to sort.
* @param n The size of the array.
*/
void insertion_sort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;

// Move elements of arr[0..i-1], that are greater than key,
// to one position ahead of their current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

/**
* Main function to demonstrate Insertion Sort and measure execution time.
*/
int main() {
int N_values[] = {10000, 20000, 30000, 40000};
int num_tests = sizeof(N_values) / sizeof(N_values[0]);

for (int t = 0; t < num_tests; t++) {
int N = N_values[t];
int *arr = (int *)malloc(N * sizeof(int));

// Seed the random number generator
srand(time(NULL));

// Fill the array with random integers
for (int i = 0; i < N; i++) {
arr[i] = rand() % N;
}

// Measure the start time
clock_t start_time = clock();

// Perform Insertion Sort
insertion_sort(arr, N);

// Measure the end time
clock_t end_time = clock();

// Calculate the elapsed time in seconds
double time_spent = (double)(end_time - start_time) / CLOCKS_PER_SEC;

// Verify that the array is sorted
int is_sorted = 1;
for (int i = 1; i < N; i++) {
if (arr[i - 1] > arr[i]) {
is_sorted = 0;
break;
}
}

// Print the result and time
printf("Insertion Sort with N = %d\n", N);
if (is_sorted)
printf("Array is sorted.\n");
else
printf("Array is NOT sorted.\n");
printf("Time taken: %f seconds\n\n", time_spent);

// Free the allocated memory
free(arr);
}

return 0;
}
124 changes: 124 additions & 0 deletions code/13-merge_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/**
* Function to merge two subarrays of arr[].
* First subarray is arr[left..mid]
* Second subarray is arr[mid+1..right]
*
* @param arr The main array containing both subarrays.
* @param left The starting index of the first subarray.
* @param mid The ending index of the first subarray.
* @param right The ending index of the second subarray.
*/
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;

// Create temp arrays
int *L = (int *)malloc(n1 * sizeof(int));
int *R = (int *)malloc(n2 * sizeof(int));

// Copy data to temp arrays L[] and R[]
for (int i = 0; i < n1; i++) L[i] = arr[left + i];
for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j];

// Merge the temp arrays back into arr[left..right]
int i = 0; // Initial index of first subarray
int j = 0; // Initial index of second subarray
int k = left; // Initial index of merged subarray

while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k++] = L[i++];
} else {
arr[k++] = R[j++];
}
}

// Copy the remaining elements of L[], if any
while (i < n1) arr[k++] = L[i++];

// Copy the remaining elements of R[], if any
while (j < n2) arr[k++] = R[j++];

// Free the temporary arrays
free(L);
free(R);
}

/**
* Function to perform Merge Sort on an array.
*
* @param arr The array to sort.
* @param left The starting index of the array.
* @param right The ending index of the array.
*/
void merge_sort(int arr[], int left, int right) {
if (left < right) {
// Same as (left + right)/2 but avoids overflow
int mid = left + (right - left) / 2;

// Sort first and second halves
merge_sort(arr, left, mid);
merge_sort(arr, mid + 1, right);

merge(arr, left, mid, right);
}
}

/**
* Main function to demonstrate Merge Sort and measure execution time.
*/
int main() {
int N_values[] = {100000, 200000, 400000, 800000};
int num_tests = sizeof(N_values) / sizeof(N_values[0]);

for (int t = 0; t < num_tests; t++) {
int N = N_values[t];
int *arr = (int *)malloc(N * sizeof(int));

// Seed the random number generator
srand(time(NULL));

// Fill the array with random integers
for (int i = 0; i < N; i++) {
arr[i] = rand();
}

// Measure the start time
clock_t start_time = clock();

// Perform Merge Sort
merge_sort(arr, 0, N - 1);

// Measure the end time
clock_t end_time = clock();

// Calculate the elapsed time in seconds
double time_spent = (double)(end_time - start_time) / CLOCKS_PER_SEC;

// Verify that the array is sorted
int is_sorted = 1;
for (int i = 1; i < N; i++) {
if (arr[i - 1] > arr[i]) {
is_sorted = 0;
break;
}
}

// Print the result and time
printf("Merge Sort with N = %d\n", N);
if (is_sorted)
printf("Array is sorted.\n");
else
printf("Array is NOT sorted.\n");
printf("Time taken: %f seconds\n\n", time_spent);

// Free the allocated memory
free(arr);
}

return 0;
}
Loading

0 comments on commit d66f00e

Please sign in to comment.