#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
struct musica {
char nome[ 100 ] ;
char estilo[ 100 ] ;
int rank;
} ;
typedef struct musica Musica;
int particao( Musica * musicas[ ] , int baixo, int alto) {
Musica * pivot = musicas[ baixo] , * temp;
int i = baixo - 1 , j = alto + 1 ;
while ( 1 ) {
do { i = i + 1 ; } while ( musicas[ i] -> rank < pivot-> rank) ;
do { j = j - 1 ; } while ( musicas[ j] -> rank > pivot-> rank) ;
if ( i >= j) return j;
temp = musicas[ i] ;
musicas[ i] = musicas[ j] ;
musicas[ j] = temp;
}
}
void quicksort( Musica * musicas[ ] , int baixo, int alto) {
if ( baixo < alto) {
int p = particao( musicas, baixo, alto) ;
quicksort( musicas, baixo, p) ;
quicksort( musicas, p+ 1 , alto) ;
}
}
int main ( void ) {
int i, j;
Musica * a[ 8 ] ;
for ( i= 0 ; i< 4 ; i++ ) {
a
[ i
] = malloc ( sizeof ( Musica
) ) ;
printf ( "Ranking da música: " ) ; scanf ( "%d" ,& ( a
[ i
] -> rank
) ) ;
}
//RANKING DIGITADO DESORDENADO
for ( i= 0 ; i< 4 ; i++ ) {
printf ( "RANK %d\t %s\t %s\t \n " , a
[ i
] -> rank
, a
[ i
] -> nome
, a
[ i
] -> estilo
) ; }
/*
Musica *temp;
for (i=0; i<4; i++) {
for (j=i+1; j<4; j++) {
if (a[i]->rank > a[j]->rank) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
*/
quicksort( a, 0 , 3 ) ;
//RANKING ORDEM
for ( i= 0 ; i< 4 ; i++ ) {
printf ( "RANK %d\t %s\t %s\t \n " , a
[ i
] -> rank
, a
[ i
] -> nome
, a
[ i
] -> estilo
) ; }
return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxsb2NhbGUuaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgoKc3RydWN0IG11c2ljYSB7CgljaGFyIG5vbWVbMTAwXTsKCWNoYXIgZXN0aWxvWzEw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