TheBoussinesqModel  3.2.1
 All Data Structures Files Functions Variables Typedefs Macros Pages
sorting.c
Go to the documentation of this file.
1 
22 #include "turtle.h"
23 #include "sorting.h"
24 
52 int bubble_sort(LONGVECTOR *v,short print) {
61  long i,mk;
62 
63  int swapped=1;
64 
65  long n=v->nh;
66  do {
67  swapped=0;
68  n=n-1;
69  for(i=v->nl;i<=n;i++){
70  if (v->element[i]>v->element[i+1]){
72  mk=v->element[i];
73  v->element[i]=v->element[i+1];
74  v->element[i+1]=mk;
75  swapped=1;
76  }
77  }
78  if (n<v->nl && swapped==1) {
79  printf("Error in bubble_sort longvector was not correctly sorted!! ");
81  swapped=1;
82  }
83  } while (swapped==1);
84 
85  return 0;
86 }
87 
103  long i,mk,lk;
104 
105  int swapped=1;
106 
107  long n=v->nh;
108 
109  LONGVECTOR *la;
110 
111  la=new_longvector(v->nh);
112 
113  for (i=la->nl;i<=la->nh;i++) {
114  la->element[i]=i;
115  }
116 
117 
118  do {
119  swapped=0;
120  n=n-1;
121  for(i=v->nl;i<=n;i++){
122  if (v->element[i]>v->element[i+1]){
124  mk=v->element[i];
125  v->element[i]=v->element[i+1];
126  v->element[i+1]=mk;
128  lk=la->element[i];
129  la->element[i]=la->element[i+1];
130  la->element[i+1]=lk;
131  swapped=1;
132  }
133  }
134  if (n<v->nl && swapped==1) {
135  printf("Error in bubble_sort longvector was not correctly sorted!! ");
136  print_longvector_elements(v,print);
137  swapped=1;
138  }
139  } while (swapped==1);
140 
141  return la;
142 }
143 
144 
145 int bubble_sort_matrix(LONGVECTOR *v, LONGMATRIX *m, short print) {
158  LONGVECTOR *iv;
159  long r,c,mk,lk;
160 
161  if (v->nh!=m->nrh) printf("Error in bubble_sort_matrix the vector to b sorted and the matrix has different size: %ld and %ld (rows)/n",v->nh,m->nrh);
162 
163 
164  iv=addresses_bubble_sort(v,print);
165  for (r=m->nrl;r<=m->nrh;r++) {
166  lk=iv->element[r];
167  if (lk!=r) {
168  for (c=m->ncl;c<=m->nch;c++){
169  mk=m->element[r][c];
170  m->element[r][c]=m->element[lk][c];
171  m->element[lk][c]=mk;
172  }
173  iv->element[r]=r;
174  iv->element[lk]=lk;
175  }
176  }
177 
178 
179 
180 
181  if (print==1) printf("Function bubble_sort_matrix was successfully executed! \n");
182 
183  return 0;
184 
185 }
186 
187 
188 //int sort_by_address(LONGVECTOR *all){
197 // LONGVECTOR *addresses_bubble_sort(LONGVECTOR *v,short print)
198 //}//