#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// ... (ฟังก์ชั่นการจัดเรียงทั้ง 6 แบบของคุณ) ...

// ฟังก์ชั่นสำหรับสร้างข้อมูลแบบสุ่ม
void generateRandomArray(int arr[], int n) {
    for (int i = 0; i < n; i++) {
        arr[i] = rand() % 100000; // สร้างเลขสุ่มในช่วง 0-99999
    }
}

int main() {
    int sizes[] = {1000, 5000, 10000, 20000, 50000};
    int num_sizes = sizeof(sizes) / sizeof(sizes[0]);

    for (int i = 0; i < num_sizes; i++) {
        int n = sizes[i];
        int *arr = (int *)malloc(n * sizeof(int)); // ใช้ malloc เพื่อจองหน่วยความจำแบบ dynamic
        if (arr == NULL) {
            perror("Memory allocation failed");
            exit(1);
        }

        printf("Testing with n = %d:\n", n);

        for (int j = 0; j < 10; j++) { // รัน 10 รอบ
            generateRandomArray(arr, n);

            // ทดสอบแต่ละอัลกอริทึมและจับเวลา
            clock_t start, end;
            double cpu_time_used;

            // Bubble Sort
            int *arr_copy = (int *)malloc(n * sizeof(int));
            if (arr_copy == NULL) {
                perror("Memory allocation failed");
                exit(1);
            }
            memcpy(arr_copy, arr, n * sizeof(int)); // คัดลอกข้อมูล
            start = clock();
            bubbleSort(arr_copy, n);
            end = clock();
            cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
            printf("Bubble Sort: %.6f seconds\n", cpu_time_used);
            free(arr_copy); // คืนหน่วยความจำ

            // ... (ทำซ้ำกับการจัดเรียงแบบอื่น) ...

        }
        free(arr); // คืนหน่วยความจำ
        printf("\n");
    }

    return 0;
}