Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions Data Structure Algorithms/1/ACM_DSA_01.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include <stdio.h>

/* Implement a binary search algorithm to find the index of a given element in a sorted array. */

int binsrch(int arr_len, int arr[]) {

int l_index = 0, r_index = arr_len - 1, t, m_index;

printf("\nPlease Enter Value To Be Searched For Using The Binary Search Algorithm:\n\n=>\tTARGET\t:\t");
scanf("%d", &t);

while (l_index <= r_index) {

m_index = (l_index + r_index)/2;

if (arr[m_index] == t) { return m_index; }

if (arr[m_index] < t) { l_index = m_index + 1; continue; }
else if (arr[m_index] > t) { r_index = m_index - 1; continue; }

}

return -1;

}

void main() {

int arr_ch, t_index;

printf("\nHello!\n\nPlease Select An Option:\n\n\t1. Enter Elements To Populate Custom Array, Then Use Binary Search.\n\t2. Test Binary Search Using Pre-Defined Array.\n\n");

printf("=>\t"); scanf("%d", &arr_ch);

while (1) {

if (arr_ch == 1) {

printf("\n\nPlease Enter The Number Of Elements You Wish To Add To The Array:\n\n");

int n, i, j;

printf("=>\tn\t:\t"); scanf("%d", &n);

int num_arr[n];

printf("\nPlease Enter Elements:\n\n");

for (i = 0; i < n; i++) { printf("=>\tELEMENT-%d\t:\t", i + 1); scanf("%d", &num_arr[i]); }

// sorting
for (i = 0; i < n; i++) {

for (j = i + 1; j < n; ++j) {

if (num_arr[i] > num_arr[j]) {

num_arr[i] ^= num_arr[j];
num_arr[j] = num_arr[i] ^ num_arr[j];
num_arr[i] ^= num_arr[j];

}

}

}

printf("\nSorted Array:\n\n{ %d", num_arr[0]);

for (i = 1; i < n; i++) { printf(", %d", num_arr[i]); }

printf(" }\n\n");

t_index = binsrch(n, num_arr);

break;

}
else if (arr_ch == 2) {

int n = 100, num_arr[100] = {

1, 2, 4, 5, 6, 7, 10, 11, 14, 16,
19, 20, 21, 24, 25, 26, 29, 31, 32, 33,
34, 35, 37, 43, 45, 46, 47, 51, 52, 53,
56, 58, 59, 62, 63, 66, 67, 71, 72, 74,
76, 77, 79, 83, 84, 86, 88, 89, 91, 93,
95, 96, 97, 101, 123, 143, 150, 158, 172, 184,
221, 224, 232, 235, 245, 279, 299, 304, 306, 315,
318, 330, 363, 366, 402, 408, 516, 535, 551, 553,
558, 562, 567, 583, 586, 627, 640, 675, 693, 719,
728, 756, 769, 784, 820, 838, 855, 867, 871, 897

};

printf("\n\nHere Is An Array Of 100 Elements (Read Left-to-Right, Top-to-Bottom, Sequentially.):\n\n");

int i, j;

printf("{");

for (j = 0; j < n / 10; j++) {

printf("\t%d", num_arr[j]);

for (i = j + (n / 10); i < n; i += n / 10) { printf(",\t%d", num_arr[i]); }

if (j == (n / 10) - 1) { printf("\t}\n"); continue; }

printf("\n");

}

t_index = binsrch(n, num_arr);

break;

}
else { t_index = -2; break; }

}

if (t_index >= 0) { printf("\n\nThe element is present at the index %d.\n\n<Stopping Execution>\n", t_index); }
else if (t_index == -1) { printf("\n\nThe element was not found in the array.\n\n<Stopping Execution>\n"); }
else { printf("\n\nInvalid Argument For Choice, Exiting...\n\n<Stopping Execution>\n"); }

}
132 changes: 132 additions & 0 deletions Data Structure Algorithms/2/ACM_DSA_02.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#include <stdio.h>

/* Implement the bubble sort algorithm to sort an array of integers. */

void switch_vars(int* a, int* b) { *a ^= *b; *b = *a ^ *b; *a ^= *b; }

void bubble_sort(int length_arr, int num_arr[]) {

int i = 0, j, k;

while (i < length_arr) {

for (j = 0; j < length_arr - (i + 1); j++) {

if (num_arr[j] > num_arr[j + 1]) { switch_vars(&num_arr[j], &num_arr[j + 1]); }

}

i++;

}

if (length_arr != 100) {

printf("\nSorted Array:\n\n{ %d", num_arr[0]);

for (i = 1; i < length_arr; i++) { printf(", %d", num_arr[i]); }

printf(" }\n\n");

}
else {

printf("\nSorted Array:\n\n");

printf("{");

for (j = 0; j < length_arr / 10; j++) {

printf("\t%d", num_arr[j]);

for (i = j + (length_arr / 10); i < length_arr; i += length_arr / 10) { printf(",\t%d", num_arr[i]); }

if (j == (length_arr / 10) - 1) { printf("\t}\n"); continue; }

printf("\n");

}

}
}

void main() {

int arr_choice;

printf("\nHello!\n\nPlease Select An Option:\n\n\t1. Enter Elements To Populate Custom Array, Then Use Bubble Sort.\n\t2. Test Bubble Sort Using Pre-Defined Unsorted Array.\n\n");

printf("=>\t"); scanf("%d", &arr_choice);

switch (arr_choice) {

case 1: {

printf("\n\nPlease Enter The Number Of Elements You Wish To Add To The Array:\n\n");

int n, i, j;

printf("=>\tn\t:\t"); scanf("%d", &n);

int num_arr[n];

printf("\nPlease Enter Elements:\n\n");

for (i = 0; i < n; i++) { printf("=>\tELEMENT-%d\t:\t", i + 1); scanf("%d", &num_arr[i]); }

printf("\nUnsorted Array:\n\n{ %d", num_arr[0]);

for (i = 1; i < n; i++) { printf(", %d", num_arr[i]); }

printf(" }\n\n");

bubble_sort(n, num_arr);

break;

}

case 2: {

int n = 100, num_arr[100] = {

0, 260, 267, 268, 13, 14, 16, 277, 278, 24,
26, 27, 282, 29, 284, 31, 34, 291, 36, 294,
39, 298, 44, 45, 46, 306, 51, 310, 55, 56,
57, 59, 60, 318, 321, 324, 69, 71, 77, 337,
338, 86, 87, 342, 89, 90, 346, 348, 98, 355,
100, 356, 102, 357, 359, 105, 361, 363, 109, 365,
366, 112, 368, 370, 371, 372, 117, 118, 120, 122,
124, 130, 133, 137, 138, 141, 149, 153, 154, 157,
159, 160, 168, 171, 178, 181, 183, 184, 194, 197,
202, 205, 213, 214, 223, 224, 225, 231, 232, 236

};

printf("\n\nHere Is An Array Of 100 Elements (Read Left-to-Right, Top-to-Bottom, Sequentially):\n\n");

int i, j;

printf("{");

for (j = 0; j < n / 10; j++) {

printf("\t%d", num_arr[j]);

for (i = j + (n / 10); i < n; i += n / 10) { printf(",\t%d", num_arr[i]); }

if ( j == (n / 10) - 1) { printf("\t}\n"); continue; }

printf("\n");

}

bubble_sort(n, num_arr);

break;

}

}

}
105 changes: 105 additions & 0 deletions Data Structure Algorithms/3/ACM_DSA_03.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include <stdio.h>

/* Implement a linear search algorithm to find the index of a given element in an array. */

int linsrch(int arr_len, int arr[]) {

int target, i;

printf("\nPlease Enter Value To Be Searched For Using The Linear Search Algorithm:\n\n=>\tTARGET\t:\t");
scanf("%d", &target);

for (i = 0; i < arr_len; i++) {

if (target == arr[i]) { return i; }

}

return -1;

}

void main() {

int arr_ch, t_index;

printf("\nHello!\n\nPlease Select An Option:\n\n\t1. Enter Elements To Populate Custom Array, Then Use Linear Search.\n\t2. Test Linear Search Using Pre-Defined Array.\n\n");

printf("=>\t"); scanf("%d", &arr_ch);

while (1) {

if (arr_ch == 1) {

printf("\n\nPlease Enter The Number Of Elements You Wish To Add To The Array:\n\n");

int n, i, j;

printf("=>\tn\t:\t"); scanf("%d", &n);

int num_arr[n];

printf("\nPlease Enter Elements:\n\n");

for (i = 0; i < n; i++) { printf("=>\tELEMENT-%d\t:\t", i + 1); scanf("%d", &num_arr[i]); }

printf("\nArray:\n\n{ %d", num_arr[0]);

for (i = 1; i < n; i++) { printf(", %d", num_arr[i]); }

printf(" }\n\n");

t_index = linsrch(n, num_arr);

break;

}
else if (arr_ch == 2) {

int n = 100, num_arr[100] = {

0, 260, 267, 268, 13, 14, 16, 277, 278, 24,
26, 27, 282, 29, 284, 31, 34, 291, 36, 294,
39, 298, 44, 45, 46, 306, 51, 310, 55, 56,
57, 59, 60, 318, 321, 324, 69, 71, 77, 337,
338, 86, 87, 342, 89, 90, 346, 348, 98, 355,
100, 356, 102, 357, 359, 105, 361, 363, 109, 365,
366, 112, 368, 370, 371, 372, 117, 118, 120, 122,
124, 130, 133, 137, 138, 141, 149, 153, 154, 157,
159, 160, 168, 171, 178, 181, 183, 184, 194, 197,
202, 205, 213, 214, 223, 224, 225, 231, 232, 236

};

printf("\n\nHere Is An Array Of 100 Elements (Read Left-to-Right, Top-to-Bottom, Sequentially.):\n\n");

int i, j;

printf("{");

for (j = 0; j < n / 10; j++) {

printf("\t%d", num_arr[j]);

for (i = j + (n / 10); i < n; i += n / 10) { printf(",\t%d", num_arr[i]); }

if (j == (n / 10) - 1) { printf("\t}\n"); continue; }

printf("\n");

}

t_index = linsrch(n, num_arr);

break;

}
else { t_index = -2; break; }

}

if (t_index >= 0) { printf("\n\nThe element is present at the index %d.\n\n<Stopping Execution>\n", t_index); }
else if (t_index == -1) { printf("\n\nThe element was not found in the array.\n\n<Stopping Execution>\n"); }
else { printf("\n\nInvalid Argument For Choice, Exiting...\n\n<Stopping Execution>\n"); }

}
Loading