/* File: genselect.c A generic selection sort function. */ #include #include #include /* for bcopy() */ #include "genselect.h" void SelectionSort(void *A, int n, int size, CMP_FUNC cmp) { int i, j ; char *ptr, *iptr, *jptr, *temp ; temp = malloc(size) ; if (temp == NULL) { fprintf(stderr, "Oops\n") ; exit(1) ; } ptr = (char *) A ; for (i=0 ; i < n ; i++) { iptr = ptr + i*size ; /* Find "largest" element in the remaining array and put in A[i] */ for (j = i+1 ; j < n ; j++) { jptr = ptr + j*size ; if (cmp(iptr, jptr) < 0) { bcopy(iptr, temp, size) ; // copy size bytes from iptr to temp bcopy(jptr, iptr, size) ; // copy size bytes from jptr to iptr bcopy(temp, jptr, size) ; // copy size bytes from temp to jptr } } } }