Advertisement
cd62131

2 Step Sort

Dec 7th, 2013
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. static int compare(const void *p1, const void *p2) {
  4.   for (int i = 0; i < 3; i++) {
  5.     if (((double *)p1)[i] > ((double *)p2)[i]) return 1;
  6.     else if (((double *)p1)[i] < ((double *)p2)[i]) return -1;
  7.   }
  8.   return 0;
  9. }
  10. int main(int ac, char **av) {
  11.   double x[][3] = {
  12.     { 1.0, 1.0, 1.0 },
  13.     { 2.0, 5.0, 0.0 },
  14.     { 2.0, 4.0, 1.0 },
  15.     { 2.0, 3.0, 2.0 },
  16.     { 2.0, 2.0, 3.0 },
  17.     { 2.0, 1.0, 4.0 },
  18.     { 1.0, 5.0, 0.0 },
  19.     { 1.0, 4.0, 1.0 },
  20.     { 1.0, 3.0, 2.0 },
  21.     { 1.0, 2.0, 3.0 },
  22.     { 1.0, 1.0, 4.0 },
  23.     { 2.0, 2.0, 2.0 }
  24.   };
  25.   printf("Before:\n");
  26.   for (int i = 0; i < sizeof(x) / sizeof(double *) / 3; i++) {
  27.     printf("%2.1f, %2.1f, %2.1f\n", x[i][0], x[i][1], x[i][2]);
  28.   }
  29.   qsort(x, sizeof(x) / sizeof(double *) / 3, sizeof(x[0]), compare);
  30.   printf("After:\n");
  31.   for (int i = 0; i < sizeof(x) / sizeof(double *) / 3; i++) {
  32.     printf("%2.1f, %2.1f, %2.1f\n", x[i][0], x[i][1], x[i][2]);
  33.   }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement