#include <cstdio>
void qsort(int a[], int l, int r)
{
if(l < r)
{
int i = l, j = r, key = a[l];
while (i < j)
{
while(i < j && a[j] >= key)
j--;
if(i < j)
a[i++] = a[j];
while(i < j && a[i] < key)
i++;
if(i < j)
a[j--] = a[i];
}
a[i] = key;
qsort(a, l, i - 1);
qsort(a, i + 1, r);
}
}
int main()
{
int a[] = {5, 4, 1, 3, 2};
qsort(a, 0, 4);
for(int i= 0 ; i < 5; i++)
printf("%d ", a[i]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmpint(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
int cmpchar(const void *a, const void *b)
{
return *(char *)a - *(char *)b;
}
int cmpdouble(const void *x, const void *y)
{
return *(double*)x > *(double*)y ? 1 : -1;
}
int main()
{
int i;
int a[6] = {5,4,1,3,2,6};
qsort(a, 6, sizeof(a[0]), cmpint);
for(i = 0; i < 6; i++)
printf("%d ",a[i]);
printf("\n\n");
int b[5] = {5,4,1,3,2};
qsort(b+2, 3, sizeof(b[0]), cmpint);
for(i = 0; i < 5; i++)
printf("%d ", b[i]);
printf("\n\n");
char s[] = "acBCAb";
int len = strlen(s);
qsort(s, len, sizeof(s[0]), cmpchar);
for(i = 0; s[i] != '\0'; i++)
printf("%c", s[i]);
printf("\n\n");
double c[5] = {1.1,1.2,1.3,1.4,1.5};
qsort(c, 5, sizeof(c[0]), cmpdouble);
for(i = 0; i < 5; i++)
printf("%lf ", c[i]);
printf("\n\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node{
int age;
char name[10];
};
struct Node{
double data;
int other;
}s[100];
struct str{
int data;
char str[100];
}string[100];
int cmpstruct2(const void *a, const void *b)
{
return (*(struct Node *)a).data > (*(struct Node *)b).data ? 1 : -1;
}
int cmpstruct1(const void *a, const void *b)
{
struct node *pnode1 = (struct node *)a;
struct node *pnode2 = (struct node *)b;
if(pnode1->age != pnode2->age)
return pnode1->age - pnode2->age;
else
return strcmp(pnode1->name, pnode2->name);
}
int cmpstr(const void *a, const void *b)
{
return strcmp((*(struct str *)a)->str, (*(struct str *)b)->str);
}