Advertisement
Guest User

bubble sort

a guest
Mar 17th, 2012
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "genlib.h"
  3. #include "simpio.h"
  4.  
  5. #define n 10
  6.  
  7. void getArray(int a[]);
  8. void bubbleSort(int a[]);
  9. bool isordered(int a[]);
  10. void printArray(int a[]);
  11.  
  12. main()
  13. {
  14.       int a[n];
  15.       getArray(a);
  16.       bubbleSort(a);
  17.       printArray(a);
  18.      
  19. }
  20.  
  21. void getArray(int a[])
  22. {
  23.      int i;
  24.      for(i=0;i<n;i++)
  25.      {
  26.                      printf("a[%d]=", i);
  27.                      a[i]=GetInteger();
  28.      }
  29. }
  30.  
  31.  
  32. void bubbleSort(int a[])
  33. {
  34.      int i, temp;
  35.      for(i=0;i<n;i++)
  36.      {
  37.                 if(a[i]>a[i+1])
  38.                 {
  39.                                temp=a[i];
  40.                                a[i]=a[i+1];
  41.                                a[i+1]=temp;
  42.                 }
  43.      }
  44.      if(isordered(a)) return;
  45.      else bubbleSort(a);
  46. }
  47.  
  48. bool isordered(int a[])
  49. {
  50.      bool flag, i;
  51.      flag=TRUE;
  52.      for(i=0;i<n;i++)
  53.      {
  54.                      if(a[i]>a[i+1])
  55.                      flag=FALSE;
  56.      }
  57.      if(flag==FALSE) return FALSE;
  58.      if(flag==TRUE) return TRUE;
  59. }
  60.  
  61. void printArray(int a[])
  62. {
  63.      int i;
  64.                for(i=0;i<n;i++)
  65.                {
  66.                                printf("a[%d]=%d", i, a[i]);
  67.                }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement