Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define N 5
  4.  
  5. void bubbleSort(float arr[], int n);
  6. void swap(float *xp, float *yp);
  7. void printArray(float arr[], int size);
  8.  
  9.  
  10. int main(){
  11.  
  12.     int i;
  13.     float nums[N];
  14.  
  15.     for(i=0; i<N; ++i)
  16.         scanf("%f", &nums[i]);
  17.  
  18.     bubbleSort(nums, N);
  19.  
  20.     return 0;
  21.  
  22. }
  23.  
  24. void bubbleSort(float arr[], int n)   //bubble sort function
  25. {
  26.     int i, j, counter=0;
  27.     for (i=1; i < n; i++)
  28.         for (j=0; j < n-i; j++)
  29.             if (arr[j] > arr[j+1])
  30.             {
  31.                 swap(&arr[j], &arr[j+1]);
  32.                 ++counter;
  33.                 printf("STEP %d: ", counter);
  34.                 printArray(arr, N);
  35.             }
  36. }
  37.  
  38.  
  39.  
  40. void swap(float *xp, float *yp) //function which swaps two integers
  41. {
  42.     float temp = *xp;
  43.     *xp = *yp;
  44.     *yp = temp;
  45. }
  46.  
  47. void printArray(float arr[], int size)
  48. {
  49.     int i;
  50.     for(i=0; i<size; ++i)
  51.         printf("%.2f ", arr[i]);
  52.     printf("\n");
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement