allekco

Bubble sort

Oct 17th, 2019
16
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <stddef.h>
  7.  
  8.  
  9. int main (void) {
  10. int N, i, ii, j;
  11. scanf ("%d", &N);
  12. int A[N];
  13. float random;
  14. srand(time(NULL));
  15.  
  16. for (i=0; i<N; i++){
  17. random = rand();
  18. random = (random / RAND_MAX)*10;
  19. A[i]=random;
  20. // scanf ("%d", &A[i]);
  21.  
  22. }
  23.  
  24. clock_t start = clock ( ) ;
  25.  
  26. for (j=0; j<N; j++){
  27. for (i=1; i<N; i++){
  28. if ( (A[i-1] > A[i]) & (j != i) ){
  29. ii=A[i];
  30. A[i]=A[i-1];
  31. A[i-1]=ii;
  32. }
  33. }
  34. }
  35.  
  36. clock_t end= clock ( ) ;
  37. float seconds = ( float ) ( end - start ) / CLOCKS_PER_SEC;
  38.  
  39. /* for (i=0; i<N; i++){
  40. printf ("%d ", A[i]);
  41.  
  42. }
  43. */
  44. printf (" \n");
  45. printf ("%f ", seconds);
  46.  
  47. return 0;
  48. }
RAW Paste Data