/*
xx__genericko_sortiranje.c
-----
Implementacija genericke rutine za sortiranje polja raznih tipova
podataka.
*/
#include <stdio.h>
#include <string.h>
#define STR_LENGTH 20
void sort(void* p, size_t size, int n,
int(*compare)(const void *, const void *)) {
int i, j;
char buffer[256];
char* a = p;
for(i = 0; i < n-1; i++)
for(j = i+1; j < n; j++)
if(compare((void*)(a+i*size), (void*)(a+j*size)) > 0) {
memcpy(buffer, a+i*size, size);
memcpy(a+size*i, a+j*size, size);
memcpy(a+j*size, buffer, size);
}
}
int cmp_int(const void *m, const void *n) {
return *((int*)m) > *((int*)n);
}
int cmp_string(const void *m, const void *n) {
return strcmp((char*)m, (char*)n);
}
int main() {
int int_arr[]={3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9};
char string_arr[][STR_LENGTH]={"Mrgud",
"Hrga",
"Trapavi",
"Leni",
"Strumfeta",
"Gargamel",
"Majstor",
"Azriel"};
int k;
int int_size=sizeof(int_arr)/sizeof(int);
int string_size=sizeof(string_arr)/STR_LENGTH;
printf("Polje intova prije sortiranja:\n");
for(k = 0; k < int_size; k++)
printf("\nPolje stringova prije sortiranja:\n");
for(k = 0; k < string_size; k++)
sort(int_arr, sizeof(int), int_size, cmp_int);
sort(string_arr, STR_LENGTH, string_size, cmp_string);
printf("\nPolje intova nakon sortiranja:\n");
for(k = 0; k < int_size; k++)
printf("\nPolje stringova nakon sortiranja:\n");
for(k = 0; k < string_size; k++)
return 0;
}