fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define SIZE 10
  6.  
  7. // Function to perform Bubble Sort
  8. void bubbleSort(int arr[], int n) {
  9. int i, j, temp;
  10. for (i = 0; i < n - 1; i++) {
  11. for (j = 0; j < n - i - 1; j++) {
  12. // Swap if the element found is greater than the next element
  13. if (arr[j] > arr[j + 1]) {
  14. temp = arr[j];
  15. arr[j] = arr[j + 1];
  16. arr[j + 1] = temp;
  17. }
  18. }
  19. }
  20. }
  21.  
  22. // Function to print the array
  23. void printArray(int arr[], int n) {
  24. for (int i = 0; i < n; i++) {
  25. printf("%d ", arr[i]);
  26. }
  27. printf("\n");
  28. }
  29.  
  30. int main() {
  31. int arr[SIZE];
  32.  
  33. // Seed the random number generator
  34. srand(time(0));
  35.  
  36. // Generate 10 random numbers and store them in the array
  37. for (int i = 0; i < SIZE; i++) {
  38. arr[i] = rand() % 100; // Random number between 0 and 99
  39. }
  40.  
  41. printf("Original Array: \n");
  42. printArray(arr, SIZE);
  43.  
  44. // Sort the array using bubble sort
  45. bubbleSort(arr, SIZE);
  46.  
  47. printf("\nSorted Array: \n");
  48. printArray(arr, SIZE);
  49.  
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0.02s 25720KB
stdin
Standard input is empty
stdout
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 10

// Function to perform Bubble Sort
void bubbleSort(int arr[], int n) {
    int i, j, temp;
    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - i - 1; j++) {
            // Swap if the element found is greater than the next element
            if (arr[j] > arr[j + 1]) {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

// Function to print the array
void printArray(int arr[], int n) {
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int arr[SIZE];
    
    // Seed the random number generator
    srand(time(0));
    
    // Generate 10 random numbers and store them in the array
    for (int i = 0; i < SIZE; i++) {
        arr[i] = rand() % 100;  // Random number between 0 and 99
    }
    
    printf("Original Array: \n");
    printArray(arr, SIZE);
    
    // Sort the array using bubble sort
    bubbleSort(arr, SIZE);
    
    printf("\nSorted Array: \n");
    printArray(arr, SIZE);
    
    return 0;
}