Ordinamento

Ordinare una sequenza di interi oppure di un certo TIPO

#include 

#define NMAX 100

int cmp(const void* a, const void *b)
{
    int i1=*((int*)a);
    int i2=*((int*)b);
	
    if(i1 <  i2)
        return -1;
    else if(i1 == i2)
        return  0;    
    else
        return +1;    
}

int sequenza[NMAX];
...
qsort(sequenza, N, sizeof(int), cmp);
...
#include 
using namespace std;

#define NMAX 100

int sequenza[NMAX];
...
sort(sequenza, sequenza+N);
...
#include 
#define NMAX 100

struct TIPO{ int x,y; };
TIPO SEQUENZA[NMAX];

int cmp(const void* a, const void* b)
{
    TIPO t1=*((TIPO*)a);
    TIPO t2=*((TIPO*)b);
	
    if(t1.x <  t2.x)
        return -1;
    else if(t1.x == t2.x)
        return  0;    
    else
        return +1;    
}
...
int N=10;
...
qsort(SEQUENZA, N, sizeof(TIPO), cmp);
...
#include 
#include 
#define NMAX 100
using namespace std;

struct TIPO{ int x,y; };
TIPO SEQUENZA[NMAX];

bool cmp(TIPO a, TIPO b)
{
    if(a.x < b.x)
        return true;
    else
        return false; 
}
...
int N=10;
... 
vector vettore(SEQUENZA,SEQUENZA+N);
sort(vettore.begin(), vettore.end(), cmp);