Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.71 KB | None | 0 0
  1. /*D'Ottavi Fabio esercizio 5 esercitazione 2, ordinare un vettore*/
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define N 5
  5.  
  6. int main (int argc, char * argv[])
  7. {
  8.     int vet[N], i, j, min, pos, tmp;
  9.     for (i=0;i<N;i++)
  10.     {
  11.         printf("inserire valore nella posizione %d \n", i);
  12.         scanf("%d", &vet[i]);
  13.     }
  14.     for (j=0;j<N;j++)
  15.     {
  16.         min=vet[j];
  17.         for (i=j;i<N;i++)          
  18.         {                                       /*il for parte da j perchè risparmio tempo non*/
  19.             if(min>vet[i])                      /*facendo calcolare la parte già ordinata*/
  20.             {
  21.                 min=vet[i];
  22.                 pos=i;
  23.             }
  24.         }  
  25.         tmp=vet[j];
  26.         vet[j]=vet[pos];
  27.         vet[pos]=tmp;
  28.     }
  29.     printf("il vettore ordinato risulta:  ");
  30.     for (i=0;i<N;i++)
  31.     {
  32.         printf("%d  ", vet[i]);
  33.     }
  34.    
  35.     exit (EXIT_SUCCESS);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement