Cod sursa(job #2432570)

Utilizator thinkphpAdrian Statescu thinkphp Data 24 iunie 2019 13:13:40
Problema Sortare prin comparare Scor 40
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.36 kb
#include <stdio.h>
#include <malloc.h>
#define FIN "algsort.in"
#define FOUT "algsort.out"

void __bubble(int *p, int n) {

     int finished = 0, 
 
         changed, 
 
         size = n, 

         it;

     while( !finished ) {

            changed = 0;

            for(it = 0; it < size - 1; ++it) {

                if(p[it] > p[it+1]) {

                   int temp = p[it];

                       p[it] = p[it+1]; 

                       p[it+1] = temp;

                       changed = 1;   
                }
            } 

            if(changed) size--;

               else

                        finished = 1;
     }

}

int main() {

    int n, *p, i;

    //the following example show the usage of freopen
    //freopen(char *filename, char *mode, FILE *stream)
    //so whatever we read at STDIN that goes inside scarface.in
    freopen(FIN, "r", stdin);
    freopen(FOUT, "w", stdout);

    //read number of elements from FILE
    scanf("%d", &n); 

    //the malloc Stands for Memory allocation
    //reserves a block of memory of the specified number of bytes
    //it returns o pointer of type void which can be casted into pointer any form
    p = (int*)malloc(sizeof(int)*n);

    for(i = 0; i < n; ++i) scanf("%d", p + i); 

    __bubble(p, n);

    for(i = 0; i < n; ++i) printf("%d ", *(p + i));


    //free the space allocated.
    free(p);
 
return(0);
}