Anasazi  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SortingTools.cpp
1 // @HEADER
2 // *****************************************************************************
3 // Anasazi: Block Eigensolvers Package
4 //
5 // Copyright 2004 NTESS and the Anasazi contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 // This software is a result of the research described in the report
11 //
12 // "A comparison of algorithms for modal analysis in the absence
13 // of a sparse direct method", P. Arbenz, R. Lehoucq, and U. Hetmaniuk,
14 // Sandia National Laboratories, Technical report SAND2003-1028J.
15 //
16 // It is based on the Epetra, AztecOO, and ML packages defined in the Trilinos
17 // framework ( http://trilinos.org/ ).
18 
19 #include "SortingTools.h"
20 
21 
22 int SortingTools::sortScalars(int n, double *y, int *perm) const {
23 
24  // Sort a vector into increasing order of algebraic values
25  //
26  // Input:
27  //
28  // n (integer ) = Size of the array (input)
29  // y (double* ) = Array of length n to be sorted (input/output)
30  // perm (integer*) = Array of length n with the permutation (input/output)
31  // Optional argument
32 
33  int i, j;
34  int igap = n / 2;
35 
36  if (igap == 0) {
37  if ((n > 0) && (perm != 0)) {
38  perm[0] = 0;
39  }
40  return 0;
41  }
42 
43  if (perm) {
44  for (i = 0; i < n; ++i)
45  perm[i] = i;
46  }
47 
48  while (igap > 0) {
49  for (i=igap; i<n; ++i) {
50  for (j=i-igap; j>=0; j-=igap) {
51  if (y[j] > y[j+igap]) {
52  double tmpD = y[j];
53  y[j] = y[j+igap];
54  y[j+igap] = tmpD;
55  if (perm) {
56  int tmpI = perm[j];
57  perm[j] = perm[j+igap];
58  perm[j+igap] = tmpI;
59  }
60  }
61  else {
62  break;
63  }
64  }
65  }
66  igap = igap / 2;
67  }
68 
69  return 0;
70 
71 }
72 
73 
74 int SortingTools::sortScalars_Vectors(int num, double *lambda, double *Q, int ldQ) const {
75 
76  // This routines sorts the scalars (stored in lambda) in ascending order.
77  // The associated vectors (stored in Q) are accordingly ordered.
78  // One vector is of length ldQ.
79  // Q must be of size ldQ * num.
80 
81  int info = 0;
82  int i, j;
83 
84  int igap = num / 2;
85 
86  if ((Q) && (ldQ > 0)) {
87  double *vec = new double[ldQ];
88  double tmp;
89  while (igap > 0) {
90  for (i=igap; i < num; ++i) {
91  for (j=i-igap; j>=0; j-=igap) {
92  if (lambda[j] > lambda[j+igap]) {
93  tmp = lambda[j];
94  lambda[j] = lambda[j+igap];
95  lambda[j+igap] = tmp;
97  memcpy(vec, Q + j*ldQ, ldQ*sizeof(double));
98  memcpy(Q + j*ldQ, Q + (j+igap)*ldQ, ldQ*sizeof(double));
99  memcpy(Q + (j+igap)*ldQ, vec, ldQ*sizeof(double));
100  }
101  else {
102  break;
103  }
104  }
105  }
106  igap = igap / 2;
107  } // while (igap > 0)
108  delete[] vec;
109  } // if ((Q) && (ldQ > 0))
110  else {
111  while (igap > 0) {
112  for (i=igap; i < num; ++i) {
113  for (j=i-igap; j>=0; j-=igap) {
114  if (lambda[j] > lambda[j+igap]) {
115  // Swap two scalars
116  double tmp = lambda[j];
117  lambda[j] = lambda[j+igap];
118  lambda[j+igap] = tmp;
119  }
120  else {
121  break;
122  }
123  }
124  }
125  igap = igap / 2;
126  } // while (igap > 0)
127  } // if ((Q) && (ldQ > 0))
128 
129  return info;
130 
131 }
132 
133