/* primer35.c - sortiranje niza i slozenost * - struktura programa */ #include #define MAX 5 void unos( int t[], int n ); void ispis( int t[], int n ); void bubble1( int t[], int n ); void bubble2( int t[], int n ); void bubble3( int t[], int d, int g); void trampa( int *, int * ); // primer11.c main(){ int niz[MAX]; unos( niz, MAX ); ispis( niz, MAX ); bubble3(niz, 0, MAX-1 ); //alternativno bubble1, 2 ispis( niz, MAX ); } void unos( int t[], int n ){ int i; for( i = 0; i < n; i++ ) scanf("%d", &t[i] ); } void ispis( int t[], int n ){ int i; for( i = 0; i < n; i++ ) printf("%d ", t[i] ); printf("\n"); } void bubble1( int t[], int n ) { int i, j, mark; /* mark = 1 ako se vrsi razmena mesta; inace = 1 */ do { i = 0; while( t[i] <= t[i+1] && i < n ) i = i + 1; if( i < n-1 ) { trampa( &t[i], &t[i+1] ); mark = 1; } else mark = 0; for( j = 0; j < n; j++ ) printf("%d ", t[j] ); printf("\n"); } while (mark == 1 ); } void bubble2( int t[], int n ) { int i, j, k; for( j = n-1; j>=0; j--) { for( i = 0; i < j; i++ ) { if( t[i] > t[i+1] ) trampa( &t[i], &t[i+1] ); for( k = 0; k < n; k++ ) printf("%d ", t[k] ); printf("\n"); } } } void trampa( int *a, int *b ) { int temp; temp = *a; *a = *b; *b = temp; } void bubble3( int t[], int donja, int gornja ){ int i, j, k; while (gornja > donja) { j = donja; for ( i = donja; i < gornja; i++ ) if ( t[i] > t[i+1] ) { trampa( &t[i], &t[i+1] ); { for( k = 0; k < MAX; k++ ) printf("%d ", t[k] ); printf("\n"); } j = i;} gornja = j; for ( i = gornja; i > donja; i-- ) if ( t[i] < t[i-1] ) { trampa( &t[i], &t[i-1] ); { for( k = 0; k < MAX; k++ ) printf("%d ", t[k] ); printf("\n"); } j = i;} donja = j; } }