Advertisement
gavrilo93

sortiranje na fajl

Oct 25th, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<conio.h>
  3. #define MAX_SIZE 20
  4.  
  5.   void bubble(int a[],int n)
  6.   {
  7.         int i,j,t;
  8.          for(i=n-1;i>=0;i--)
  9.          {
  10.             for(j=0;j<=i;j++)
  11.  
  12.                   {
  13.                     if(a[j]>a[j+1])
  14.                                     {
  15.                                       t=a[j];
  16.                                      a[j]=a[j+1];
  17.                                      a[j+1]=t;
  18.                                     }
  19.                    }
  20.  
  21.  
  22.            }//end for 1.
  23.  
  24.   }//end function.
  25.  
  26. int main ()
  27. {
  28.     int a[MAX_SIZE];
  29.     int i, n;
  30.  
  31.     FILE *pf;
  32.     pf = fopen("ulaz.txt", "r");
  33.  
  34.     printf("unesite broj elemenata niza\n");
  35.     scanf("%d", &n);
  36.  
  37.     for(i=0; i <= n; i++)
  38.     {
  39.         fscanf(pf, "%d", &a[i]);
  40.     }
  41.  
  42.     fclose(pf);
  43.  
  44.     for(i=0; i<n; i++)
  45.     {
  46.         printf("a[%d]=%d\n", i, a[i]);
  47.     }
  48.  
  49.     // poziv funkcije za sortiranje
  50.     bubble(a,n);
  51.  
  52.     pf = fopen("izlaz.txt", "w");
  53.     for (i=0; i<n; i++)
  54.     {
  55.         fprintf(pf, "a[%d] = %d\n", i, a[i]);
  56.  
  57.     }
  58.     fclose(pf);
  59.  
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement