fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. // ... (ฟังก์ชั่นการจัดเรียงทั้ง 6 แบบของคุณ) ...
  6.  
  7. // ฟังก์ชั่นสำหรับสร้างข้อมูลแบบสุ่ม
  8. void generateRandomArray(int arr[], int n) {
  9. for (int i = 0; i < n; i++) {
  10. arr[i] = rand() % 100000; // สร้างเลขสุ่มในช่วง 0-99999
  11. }
  12. }
  13.  
  14. int main() {
  15. int sizes[] = {1000, 5000, 10000, 20000, 50000};
  16. int num_sizes = sizeof(sizes) / sizeof(sizes[0]);
  17.  
  18. for (int i = 0; i < num_sizes; i++) {
  19. int n = sizes[i];
  20. int *arr = (int *)malloc(n * sizeof(int)); // ใช้ malloc เพื่อจองหน่วยความจำแบบ dynamic
  21. if (arr == NULL) {
  22. perror("Memory allocation failed");
  23. exit(1);
  24. }
  25.  
  26. printf("Testing with n = %d:\n", n);
  27.  
  28. for (int j = 0; j < 10; j++) { // รัน 10 รอบ
  29. generateRandomArray(arr, n);
  30.  
  31. // ทดสอบแต่ละอัลกอริทึมและจับเวลา
  32. clock_t start, end;
  33. double cpu_time_used;
  34.  
  35. // Bubble Sort
  36. int *arr_copy = (int *)malloc(n * sizeof(int));
  37. if (arr_copy == NULL) {
  38. perror("Memory allocation failed");
  39. exit(1);
  40. }
  41. memcpy(arr_copy, arr, n * sizeof(int)); // คัดลอกข้อมูล
  42. start = clock();
  43. bubbleSort(arr_copy, n);
  44. end = clock();
  45. cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
  46. printf("Bubble Sort: %.6f seconds\n", cpu_time_used);
  47. free(arr_copy); // คืนหน่วยความจำ
  48.  
  49. // ... (ทำซ้ำกับการจัดเรียงแบบอื่น) ...
  50.  
  51. }
  52. free(arr); // คืนหน่วยความจำ
  53. printf("\n");
  54. }
  55.  
  56. return 0;
  57. }
Success #stdin #stdout 0.04s 25568KB
stdin
Standard input is empty
stdout
#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;
}