Advertisement
Guest User

Sample code for Odd Even Spatial Sorting

a guest
Feb 9th, 2014
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. bool partial_sort()
  2. {
  3.     bool is_sorted = true;
  4.     int index, next;
  5.     PlanarPosition temp;
  6.  
  7.     // compare even columns
  8.     for (int i = 0; i < dim_x - 1; i += 2) {
  9.         for (int j = 0; j < dim_y; j++) {
  10.             index = i + j * dim_x;
  11.             next  = index + 1;
  12.             if ((position_matrix[index].x >  position_matrix[next].x) ||
  13.                 (position_matrix[index].x == position_matrix[next].x  && position_matrix[index].y > position_matrix[next].y)) {
  14.                 temp = position_matrix[index];
  15.                 position_matrix[index] = position_matrix[next];
  16.                 position_matrix[next]  = temp;
  17.                 is_sorted = false;
  18.             }
  19.         }
  20.     }
  21.  
  22.     // compare odd columns
  23.     for (int i = 1; i < dim_x - 1; i += 2) {
  24.         for (int j = 0; j < dim_y; j++) {
  25.             index = i + j * dim_x;
  26.             next  = index + 1;
  27.             if ((position_matrix[index].x >  position_matrix[next].x) ||
  28.                 (position_matrix[index].x == position_matrix[next].x  && position_matrix[index].y > position_matrix[next].y)) {
  29.                 temp = position_matrix[index];
  30.                 position_matrix[index] = position_matrix[next];
  31.                 position_matrix[next]  = temp;
  32.                 is_sorted = false;
  33.             }
  34.         }
  35.     }
  36.  
  37.     // compare even rows
  38.     for (int j = 0; j < dim_y - 1; j += 2) {
  39.         for (int i = 0; i < dim_x; i++) {
  40.             index = i + j * dim_x;
  41.             next  = index + dim_x;
  42.             if ((position_matrix[index].y >  position_matrix[next].y) ||
  43.                 (position_matrix[index].y == position_matrix[next].y  && position_matrix[index].x > position_matrix[next].x)) {
  44.                 temp = position_matrix[index];
  45.                 position_matrix[index] = position_matrix[next];
  46.                 position_matrix[next]  = temp;
  47.                 is_sorted = false;
  48.             }
  49.         }
  50.     }
  51.  
  52.     // compare odd rows
  53.     for (int j = 1; j < dim_y - 1; j += 2) {
  54.         for (int i = 0; i < dim_x; i++) {
  55.             int index = i + j * dim_x;
  56.             next  = index + dim_x;
  57.             if ((position_matrix[index].y >  position_matrix[next].y) ||
  58.                 (position_matrix[index].y == position_matrix[next].y  && position_matrix[index].x > position_matrix[next].x)) {
  59.                 temp = position_matrix[index];
  60.                 position_matrix[index] = position_matrix[next];
  61.                 position_matrix[next]  = temp;
  62.                 is_sorted = false;
  63.             }
  64.         }
  65.     }
  66.     return is_sorted;
  67. }
  68.  
  69. void full_sort()
  70. {
  71.     while (partial_sort() == false)
  72.         ;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement