#include <stdio.h>

#include "../excercise04/make_random_data/make_random_data.hpp"
#include "../excercise04/sort/quicksort.hpp"

int quickselection(const int k, int *data, const int start, const int end)
{
    if(start >= end) return data[start];
    int pivot = partition(data, start, end);

    int p = pivot - start;
    if(p > k - 1) return quickselection(k, data, start, pivot - 1);
    if(p < k - 1) return quickselection(k - (p + 1), data, pivot + 1, end);
    return data[pivot];
}

int main(void)
{
    const int size = 100000;
    int *data = make_random_data(size);

    int k;
    printf("Enter k= ");
    scanf("%d", &k);

    if(k <= 0 || k > size)
    {
        printf("Error: Out of range.\n");
        return 1;
    }

    printf("%d\n", quickselection(k, data, 0, size - 1));

    free_random_data(data);
    return 0;
}

#include <stdio.h>

#include "../excercise04/make_random_data/make_random_data.hpp"
#include "../excercise04/sort/quicksort.hpp"

int get_index_smallest(const int k, int *data, const int start, const int end)
{
    if(k < 0 || k > end) return -1; /* Base case */
    int p = partition(data, start, end);

    if(k == 0) return data[start];
    else if(p - start == k) return data[p];
    else if(p - start > k) return get_index_smallest(k, data, start, p - 1);
    else return get_index_smallest(k - p - 1, data, p + 1, end);
}

int main(void)
{
    int k;
    printf("Enter k= ");
    scanf("%d", &k);

    const int size = 100000;
    int *data = make_random_data(size);
    printf("%d\n", get_index_smallest(k, data, 0, size - 1));

    free_random_data(data);
    return 0;
}

--- 
+++ 
@@ -3,26 +3,33 @@
 #include "../excercise04/make_random_data/make_random_data.hpp"
 #include "../excercise04/sort/quicksort.hpp"
 
-int get_index_smallest(const int k, int *data, const int start, const int end)
+int quickselection(const int k, int *data, const int start, const int end)
 {
-    if(k < 0 || k > end) return -1; /* Base case */
-    int p = partition(data, start, end);
+    if(start >= end) return data[start];
+    int pivot = partition(data, start, end);
 
-    if(k == 0) return data[start];
-    else if(p - start == k) return data[p];
-    else if(p - start > k) return get_index_smallest(k, data, start, p - 1);
-    else return get_index_smallest(k - p - 1, data, p + 1, end);
+    int p = pivot - start;
+    if(p > k - 1) return quickselection(k, data, start, pivot - 1);
+    if(p < k - 1) return quickselection(k - (p + 1), data, pivot + 1, end);
+    return data[pivot];
 }
 
 int main(void)
 {
+    const int size = 100000;
+    int *data = make_random_data(size);
+
     int k;
     printf("Enter k= ");
     scanf("%d", &k);
 
-    const int size = 100000;
-    int *data = make_random_data(size);
-    printf("%d\n", get_index_smallest(k, data, 0, size - 1));
+    if(k <= 0 || k > size)
+    {
+        printf("Error: Out of range.\n");
+        return 1;
+    }
+
+    printf("%d\n", quickselection(k, data, 0, size - 1));
 
     free_random_data(data);
     return 0;
Change the entire logic of Quickselect
