fork download
  1. #include <stdio.h>
  2. #include <sys/time.h>
  3. int i, j, temp, flag;
  4.  
  5. void simple(int arr[], int n) {
  6. for (i = 0; i < n - 1; i++) {
  7. for (j = 0; j < n - 1; j++) {
  8. if (arr[j] > arr[j + 1]) {
  9. temp = arr[j];
  10. arr[j] = arr[j + 1];
  11. arr[j + 1] = temp;
  12. }
  13. }
  14. }
  15. }
  16.  
  17. void semi(int arr[], int n) {
  18. for (i = 0; i < n - 1; i++) {
  19. for (j = 0; j < n - i - 1; j++) {
  20. if (arr[j] > arr[j + 1]) {
  21. temp = arr[j];
  22. arr[j] = arr[j + 1];
  23. arr[j + 1] = temp;
  24. }
  25. }
  26. }
  27. }
  28.  
  29. void optimized(int arr[], int n) {
  30. int flag = 0;
  31. for (i = 0; i < n - 1; i++) {
  32. flag = 0;
  33. for (j = 0; j < n - i - 1; j++) {
  34. if (arr[j] > arr[j + 1]) {
  35. temp = arr[j];
  36. arr[j] = arr[j + 1];
  37. arr[j + 1] = temp;
  38. flag = 1;
  39. }
  40. }
  41. if (flag == 0) {
  42. break;
  43. }
  44. }
  45. }
  46.  
  47. int main() {
  48. int n, choice, t1, t2, tc;
  49. struct timeval tt;
  50. struct timezone tz;
  51. printf("Enter the size:- \n");
  52. scanf("%d", &n);
  53. int arr[n];
  54. printf("Enter %d elements:- \n", n);
  55. for (i = 0; i < n; i++) {
  56. scanf("%d", &arr[i]);
  57. }
  58. printf("\nSelect type of Bubble Sort\n1:- Simple\n2:- Semi Optimized\n3:- Optimized\n ");
  59. scanf("%d", &choice);
  60.  
  61. switch(choice){
  62. case 1:
  63. gettimeofday(&tt,&tz);
  64. t1=tt.tv_usec;
  65. simple(arr,n);
  66. gettimeofday(&tt,&tz);
  67. t2=tt.tv_usec;
  68. break;
  69. case 2:
  70. gettimeofday(&tt,&tz);
  71. t1=tt.tv_usec;
  72. semi(arr,n);
  73. gettimeofday(&tt,&tz);
  74. t2=tt.tv_usec;
  75. break;
  76. case 3:
  77. gettimeofday(&tt,&tz);
  78. t1=tt.tv_usec;
  79. optimized(arr,n);
  80. gettimeofday(&tt,&tz);
  81. t2=tt.tv_usec;
  82. break;
  83. default:
  84. printf("\n Invalid Choice\n");
  85. }
  86. printf("\nSorted Array\n");
  87. for (i = 0; i < n; i++) {
  88. if (i == n - 1) {
  89. printf("%d", arr[i]);
  90. } else {
  91. printf("%d, ", arr[i]);
  92. }
  93. }
  94. tc = t2 - t1;
  95. printf("\nTime Complexity = %d\n", tc);
  96. return 0; }
Success #stdin #stdout 0.02s 25648KB
stdin
Standard input is empty
stdout
#include <stdio.h>
#include <sys/time.h>
int i, j, temp, flag;
 
void simple(int arr[], int n) {
   for (i = 0; i < n - 1; i++) {
       for (j = 0; j < n - 1; j++) {
           if (arr[j] > arr[j + 1]) {
               temp = arr[j];
               arr[j] = arr[j + 1];
               arr[j + 1] = temp;
           }
       }
   }
}
 
void semi(int arr[], int n) {
   for (i = 0; i < n - 1; i++) {
       for (j = 0; j < n - i - 1; j++) {
           if (arr[j] > arr[j + 1]) {
               temp = arr[j];
               arr[j] = arr[j + 1];
               arr[j + 1] = temp;
           }
       }
   }
}
 
void optimized(int arr[], int n) {
   int flag = 0;
   for (i = 0; i < n - 1; i++) {
       flag = 0;
       for (j = 0; j < n - i - 1; j++) {
           if (arr[j] > arr[j + 1]) {
               temp = arr[j];
               arr[j] = arr[j + 1];
               arr[j + 1] = temp;
               flag = 1;
           }
       }
       if (flag == 0) {
           break;
       }
   }
}
 
int main() {
   int n, choice, t1, t2, tc;
   struct timeval tt;
   struct timezone tz;
   printf("Enter the size:- \n");
   scanf("%d", &n);
   int arr[n];
   printf("Enter %d elements:- \n", n);
   for (i = 0; i < n; i++) {
       scanf("%d", &arr[i]);
   }
   printf("\nSelect type of Bubble Sort\n1:- Simple\n2:- Semi Optimized\n3:- Optimized\n ");
   scanf("%d", &choice);
 
   switch(choice){
       case 1:
       gettimeofday(&tt,&tz);
       t1=tt.tv_usec;
       simple(arr,n);
       gettimeofday(&tt,&tz);
       t2=tt.tv_usec;
       break;
       case 2:
       gettimeofday(&tt,&tz);
       t1=tt.tv_usec;
       semi(arr,n);
       gettimeofday(&tt,&tz);
       t2=tt.tv_usec;
       break;
       case 3:
       gettimeofday(&tt,&tz);
       t1=tt.tv_usec;
       optimized(arr,n);
       gettimeofday(&tt,&tz);
       t2=tt.tv_usec;
       break;
       default:
       printf("\n Invalid Choice\n");
   }
   printf("\nSorted Array\n");
   for (i = 0; i < n; i++) {
       if (i == n - 1) {
           printf("%d", arr[i]);
       } else {
           printf("%d, ", arr[i]);
       }
   }
   tc = t2 - t1;
   printf("\nTime Complexity = %d\n", tc);
   return 0; }