#include <stdio.h>
#define N 10
void flush(void);
void Riempi(int[],unsigned long);
void Qsort(int[],unsigned long,unsigned long);
void Stampa (int[],unsigned long);
main(){
int v [N];
unsigned short scelta;
unsigned long inf, sup;
do{
printf("\n1-riempi");
printf("\n2-ordina");
printf("\n3stampa");
printf("inserisci la scelta: ");
scanf("%u",&scelta);
flush();
switch(scelta){
case 1: Riempi(v,N);
break;
case 2: Qsort(v,inf,sup);
break;
case 3: Stampa(v,N);
break;
}
}while(scelta<4);
}
void flush (){
while(getchar()!='\n');
}
void Riempi (int v[],unsigned long dim){
unsigned long i;
for(i=0;i<=dim;i++){
printf("elemento in posizione [%d]: ",i);
scanf("%d",&v[i]);
}
}
void scambia (int v[],unsigned long i,unsigned long j){
int temp=v[i];
v[i]=v[j];
v[j]=temp;
}
void Qsort(int v[],unsigned long inf,unsigned long sup){
int pivot=v[(inf+sup)/2];
unsigned long i,j;
i=inf;
j=sup;
do{
while(v[i]<pivot) i++;
while(v[j]>pivot) j--;
if(i<j)
scambia(v,i,j);
if(i<=j) {
i++;
j--;
}
} while(i<=j);
if(inf<j) Qsort(v,inf,j);
if(i<sup) Qsort(v,i,sup);
}
void Stampa (int v[],unsigned long dim){
unsigned long i;
for(i=0;i<dim;i++){
printf("\n%d",v[i]);
}}