Difference between revisions of "C: qsort"

From AdonaiMedrado.Pro.Br
Jump to: navigation, search
(New page: <code lang="c"> #include <stdio.h> #define TAMANHO_VETOR 10 int comparar(void *a, void *b) { return *(int *)a-*(int *)b; } int main(void) { int i, v[TAMANHO_VETOR]; for(i=0;i<TAMANHO_...)
 
 
Line 1: Line 1:
 
<code lang="c">
 
<code lang="c">
 
#include <stdio.h>
 
#include <stdio.h>
 +
#include <stdlib.h>
 
#define TAMANHO_VETOR 10
 
#define TAMANHO_VETOR 10
  
int comparar(void *a, void *b)
+
int comparar(const void *a, const void *b)
 
{
 
{
 
return *(int *)a-*(int *)b;
 
return *(int *)a-*(int *)b;
Line 19: Line 20:
 
printf("%d ",*(v+i));
 
printf("%d ",*(v+i));
 
printf("\n");
 
printf("\n");
 +
 +
return 0;
 
}
 
}
 
</code>
 
</code>

Latest revision as of 02:02, 17 April 2009

#include <stdio.h>
#include <stdlib.h>
#define TAMANHO_VETOR 10
 
int comparar(const void *a, const void *b)
{
	return *(int *)a-*(int *)b;
}
 
int main(void)
{
	int i, v[TAMANHO_VETOR];
	for(i=0;i<TAMANHO_VETOR;i++)
		scanf("%d",v+i);
 
	qsort(v,TAMANHO_VETOR,sizeof(int),comparar);
 
	for(i=0;i<TAMANHO_VETOR;i++)
		printf("%d ",*(v+i));
	printf("\n");
 
	return 0;
}