This commit is contained in:
2026-05-12 10:21:01 -06:00
parent a159f1a1b4
commit 8b0471639a

View File

@@ -1,6 +1,166 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "arraylist.h"
#define RAND_INT_SIZE 100
#define ARR_SIZE 16
typedef struct {
bool is_valid;
size_t val;
} IndexResult;
IndexResult iterative_binary_search(ArrayList *arr, int x, int low, int high);
IndexResult recursive_binary_search(ArrayList *arr, int x, int low, int high);
void quick_sort(ArrayList *arr, int low, int high);
int partition(ArrayList *arr, int low, int high);
void swap(ArrayList *arr, int index1, int index2);
int main(void) {
srand(time(NULL));
// no need to destroy if this is all the programm,
// in any other case destroy IS needed for memory
// leaks :)
ArrayList *arr = arraylist_init(ARR_SIZE, sizeof(int));
if (arr == NULL) {
puts("El arreglo no pudo ser inicializado.");
return EXIT_FAILURE;
}
for (size_t i = 0; i < ARR_SIZE; i++) {
int x = rand() % (RAND_INT_SIZE + 1);
arraylist_push_back(arr, &x);
}
// Improvised quicksort, should implement generic one
// like the one from the standart library but for my
// arraylist implementation
quick_sort(arr, 0, arraylist_size(arr) - 1);
puts("El arreglo es:");
for (size_t i = 0; i < ARR_SIZE; i++) {
int x;
arraylist_get(arr, i, &x);
printf("[%zu] = %d\n", i, x);
}
puts("Ingrese el metodo con el que quiere encontrar un elemento:");
puts("1) Busqueda binaria iterativa.");
puts("2) Busqueda binaria recursiva.");
int choice;
if (scanf("%d", &choice) != 1) {
puts("Input invalido, reinicie el programa.");
return EXIT_FAILURE;
} else if (choice == 0 || choice > 2) {
puts("Opcion invalid, reinicie el programa.");
return EXIT_FAILURE;
}
int x;
puts("Ingrese el valor que quiere buscar en el arreglo:");
if (scanf("%d", &x) != 1) {
puts("Input invalido, reinicie el programa.");
return EXIT_FAILURE;
}
IndexResult result;
if (choice == 1) {
result = iterative_binary_search(arr, x, 0, arraylist_size(arr) - 1);
} else {
result = iterative_binary_search(arr, x, 0, arraylist_size(arr) - 1);
}
if (!result.is_valid) {
puts("No se encontro el valor en el arreglo");
return EXIT_FAILURE;
}
printf("Se encontro el valor %d en la posicion %zu.\n", x, result.val);
return EXIT_SUCCESS;
}
int partition(ArrayList *arr, int low, int high) {
int pivot;
arraylist_get(arr, high, &pivot);
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
int jval;
arraylist_get(arr, j, &jval);
if (jval < pivot) {
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return i + 1;
}
void quick_sort(ArrayList *arr, int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quick_sort(arr, low, pi - 1);
quick_sort(arr, pi + 1, high);
}
}
void swap(ArrayList *arr, int index1, int index2) {
int x;
arraylist_get(arr, index1, &x);
int y;
arraylist_get(arr, index2, &y);
arraylist_set(arr, index1, &y);
arraylist_set(arr, index2, &x);
}
IndexResult iterative_binary_search(ArrayList *arr, int x, int low, int high) {
while (low <= high) {
int mid = (low + high) / 2;
int mid_val;
arraylist_get(arr, mid, &mid_val);
if (x == mid_val) {
return (IndexResult) {.is_valid = true, .val = mid};
} else if (x > mid_val){
low = mid + 1;
} else {
high = mid - 1;
}
}
return (IndexResult) {.is_valid = false, .val = 0};
}
IndexResult recursive_binary_search(ArrayList *arr, int x, int low, int high) {
if (low > high) {
return (IndexResult) {.is_valid = false, .val = 0};
} else {
int mid = (low + high) / 2;
int mid_val;
arraylist_get(arr, mid, &mid_val);
if (x == mid_val) {
return (IndexResult) {.is_valid = true, .val = mid};
} else if (x > mid_val) {
return recursive_binary_search(arr, x, mid + 1, high);
} else {
return recursive_binary_search(arr, x, low, mid - 1);
}
}
}