/angel-ps/core/src/main/java/com/tencent/angel/utils/Sort.java

https://github.com/Tencent/angel · Java · 532 lines · 424 code · 89 blank · 19 comment · 104 complexity · 8d4c32c2193c4479a890f3a0343f7c7b MD5 · raw file

  1. /*
  2. * Tencent is pleased to support the open source community by making Angel available.
  3. *
  4. * Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
  7. * compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/Apache-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed under the License
  12. * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  13. * or implied. See the License for the specific language governing permissions and limitations under
  14. * the License.
  15. *
  16. */
  17. package com.tencent.angel.utils;
  18. import it.unimi.dsi.fastutil.doubles.DoubleComparator;
  19. import it.unimi.dsi.fastutil.ints.IntComparator;
  20. import java.util.Random;
  21. /**
  22. * Quick sort utils
  23. */
  24. public class Sort {
  25. public static void quickSort(int[] array, double[] values, int low, int high) {
  26. if (low < high) {
  27. int tmp = array[low];
  28. double tmpValue = values[low];
  29. int ii = low, jj = high;
  30. while (ii < jj) {
  31. while (ii < jj && array[jj] >= tmp) {
  32. jj--;
  33. }
  34. array[ii] = array[jj];
  35. values[ii] = values[jj];
  36. while (ii < jj && array[ii] <= tmp) {
  37. ii++;
  38. }
  39. array[jj] = array[ii];
  40. values[jj] = values[ii];
  41. }
  42. array[ii] = tmp;
  43. values[ii] = tmpValue;
  44. quickSort(array, values, low, ii - 1);
  45. quickSort(array, values, ii + 1, high);
  46. }
  47. }
  48. public static void quickSort(long[] array, double[] values, int low, int high) {
  49. if (low < high) {
  50. long tmp = array[low];
  51. double tmpValue = values[low];
  52. int ii = low, jj = high;
  53. while (ii < jj) {
  54. while (ii < jj && array[jj] >= tmp) {
  55. jj--;
  56. }
  57. array[ii] = array[jj];
  58. values[ii] = values[jj];
  59. while (ii < jj && array[ii] <= tmp) {
  60. ii++;
  61. }
  62. array[jj] = array[ii];
  63. values[jj] = values[ii];
  64. }
  65. array[ii] = tmp;
  66. values[ii] = tmpValue;
  67. quickSort(array, values, low, ii - 1);
  68. quickSort(array, values, ii + 1, high);
  69. }
  70. }
  71. public static void quickSort(long[] array, int low, int high) {
  72. if (low < high) {
  73. long tmp = array[low];
  74. int ii = low, jj = high;
  75. while (ii < jj) {
  76. while (ii < jj && array[jj] >= tmp) {
  77. jj--;
  78. }
  79. array[ii] = array[jj];
  80. while (ii < jj && array[ii] <= tmp) {
  81. ii++;
  82. }
  83. array[jj] = array[ii];
  84. }
  85. array[ii] = tmp;
  86. quickSort(array, low, ii - 1);
  87. quickSort(array, ii + 1, high);
  88. }
  89. }
  90. public static void quickSort(int[] array, int low, int high) {
  91. if (low < high) {
  92. int tmp = array[low];
  93. int ii = low, jj = high;
  94. while (ii < jj) {
  95. while (ii < jj && array[jj] >= tmp) {
  96. jj--;
  97. }
  98. array[ii] = array[jj];
  99. while (ii < jj && array[ii] <= tmp) {
  100. ii++;
  101. }
  102. array[jj] = array[ii];
  103. }
  104. array[ii] = tmp;
  105. quickSort(array, low, ii - 1);
  106. quickSort(array, ii + 1, high);
  107. }
  108. }
  109. public static void quickSort(int[] array, int[] values, int low, int high) {
  110. if (low < high) {
  111. int tmp = array[low];
  112. int tmpValue = values[low];
  113. int ii = low, jj = high;
  114. while (ii < jj) {
  115. while (ii < jj && array[jj] >= tmp) {
  116. jj--;
  117. }
  118. array[ii] = array[jj];
  119. values[ii] = values[jj];
  120. while (ii < jj && array[ii] <= tmp) {
  121. ii++;
  122. }
  123. array[jj] = array[ii];
  124. values[jj] = values[ii];
  125. }
  126. array[ii] = tmp;
  127. values[ii] = tmpValue;
  128. quickSort(array, values, low, ii - 1);
  129. quickSort(array, values, ii + 1, high);
  130. }
  131. }
  132. public static void quickSort(int[] array, long[] values, int low, int high) {
  133. if (low < high) {
  134. int tmp = array[low];
  135. long tmpValue = values[low];
  136. int ii = low, jj = high;
  137. while (ii < jj) {
  138. while (ii < jj && array[jj] >= tmp) {
  139. jj--;
  140. }
  141. array[ii] = array[jj];
  142. values[ii] = values[jj];
  143. while (ii < jj && array[ii] <= tmp) {
  144. ii++;
  145. }
  146. array[jj] = array[ii];
  147. values[jj] = values[ii];
  148. }
  149. array[ii] = tmp;
  150. values[ii] = tmpValue;
  151. quickSort(array, values, low, ii - 1);
  152. quickSort(array, values, ii + 1, high);
  153. }
  154. }
  155. public static void quickSort(long[] array, int[] values, int low, int high) {
  156. if (low < high) {
  157. long tmp = array[low];
  158. int tmpValue = values[low];
  159. int ii = low, jj = high;
  160. while (ii < jj) {
  161. while (ii < jj && array[jj] >= tmp) {
  162. jj--;
  163. }
  164. array[ii] = array[jj];
  165. values[ii] = values[jj];
  166. while (ii < jj && array[ii] <= tmp) {
  167. ii++;
  168. }
  169. array[jj] = array[ii];
  170. values[jj] = values[ii];
  171. }
  172. array[ii] = tmp;
  173. values[ii] = tmpValue;
  174. quickSort(array, values, low, ii - 1);
  175. quickSort(array, values, ii + 1, high);
  176. }
  177. }
  178. public static void quickSort(long[] array, long[] values, int low, int high) {
  179. if (low < high) {
  180. long tmp = array[low];
  181. long tmpValue = values[low];
  182. int ii = low, jj = high;
  183. while (ii < jj) {
  184. while (ii < jj && array[jj] >= tmp) {
  185. jj--;
  186. }
  187. array[ii] = array[jj];
  188. values[ii] = values[jj];
  189. while (ii < jj && array[ii] <= tmp) {
  190. ii++;
  191. }
  192. array[jj] = array[ii];
  193. values[jj] = values[ii];
  194. }
  195. array[ii] = tmp;
  196. values[ii] = tmpValue;
  197. quickSort(array, values, low, ii - 1);
  198. quickSort(array, values, ii + 1, high);
  199. }
  200. }
  201. public static void quickSort(long[] array, float[] values, int low, int high) {
  202. if (low < high) {
  203. long tmp = array[low];
  204. float tmpValue = values[low];
  205. int ii = low, jj = high;
  206. while (ii < jj) {
  207. while (ii < jj && array[jj] >= tmp) {
  208. jj--;
  209. }
  210. array[ii] = array[jj];
  211. values[ii] = values[jj];
  212. while (ii < jj && array[ii] <= tmp) {
  213. ii++;
  214. }
  215. array[jj] = array[ii];
  216. values[jj] = values[ii];
  217. }
  218. array[ii] = tmp;
  219. values[ii] = tmpValue;
  220. quickSort(array, values, low, ii - 1);
  221. quickSort(array, values, ii + 1, high);
  222. }
  223. }
  224. public static <T> void quickSort(int [] ids, T[] values, int low, int high) {
  225. if (low < high) {
  226. int tmp = ids[low];
  227. T tmpValue = values[low];
  228. int ii = low, jj = high;
  229. while (ii < jj) {
  230. while (ii < jj && ids[jj] >= tmp) {
  231. jj--;
  232. }
  233. ids[ii] = ids[jj];
  234. values[ii] = values[jj];
  235. while (ii < jj && ids[ii] <= tmp) {
  236. ii++;
  237. }
  238. ids[jj] = ids[ii];
  239. values[jj] = values[ii];
  240. }
  241. ids[ii] = tmp;
  242. values[ii] = tmpValue;
  243. quickSort(ids, values, low, ii - 1);
  244. quickSort(ids, values, ii + 1, high);
  245. }
  246. }
  247. public static <T> void quickSort(long [] ids, T[] values, int low, int high) {
  248. if (low < high) {
  249. long tmp = ids[low];
  250. T tmpValue = values[low];
  251. int ii = low, jj = high;
  252. while (ii < jj) {
  253. while (ii < jj && ids[jj] >= tmp) {
  254. jj--;
  255. }
  256. ids[ii] = ids[jj];
  257. values[ii] = values[jj];
  258. while (ii < jj && ids[ii] <= tmp) {
  259. ii++;
  260. }
  261. ids[jj] = ids[ii];
  262. values[jj] = values[ii];
  263. }
  264. ids[ii] = tmp;
  265. values[ii] = tmpValue;
  266. quickSort(ids, values, low, ii - 1);
  267. quickSort(ids, values, ii + 1, high);
  268. }
  269. }
  270. public static void quickSort(double[] x, double[] y, int from, int to, DoubleComparator comp) {
  271. int len = to - from;
  272. if (len < 7) {
  273. selectionSort(x, y, from, to, comp);
  274. } else {
  275. int m = from + len / 2;
  276. int v;
  277. int a;
  278. int b;
  279. if (len > 7) {
  280. v = from;
  281. a = to - 1;
  282. if (len > 50) {
  283. b = len / 8;
  284. v = med3(x, from, from + b, from + 2 * b, comp);
  285. m = med3(x, m - b, m, m + b, comp);
  286. a = med3(x, a - 2 * b, a - b, a, comp);
  287. }
  288. m = med3(x, v, m, a, comp);
  289. }
  290. double seed = x[m];
  291. a = from;
  292. b = from;
  293. int c = to - 1;
  294. int d = c;
  295. while (true) {
  296. int s;
  297. while (b > c || (s = comp.compare(x[b], seed)) > 0) {
  298. for (; c >= b && (s = comp.compare(x[c], seed)) >= 0; --c) {
  299. if (s == 0) {
  300. swap(x, c, d);
  301. swap(y, c, d);
  302. d--;
  303. }
  304. }
  305. if (b > c) {
  306. s = Math.min(a - from, b - a);
  307. vecSwap(x, from, b - s, s);
  308. vecSwap(y, from, b - s, s);
  309. s = Math.min(d - c, to - d - 1);
  310. vecSwap(x, b, to - s, s);
  311. vecSwap(y, b, to - s, s);
  312. if ((s = b - a) > 1) {
  313. quickSort(x, y, from, from + s, comp);
  314. }
  315. if ((s = d - c) > 1) {
  316. quickSort(x, y, to - s, to, comp);
  317. }
  318. return;
  319. }
  320. swap(x, b, c);
  321. swap(y, b, c);
  322. b++;
  323. c--;
  324. }
  325. if (s == 0) {
  326. swap(x, a, b);
  327. swap(y, a, b);
  328. a++;
  329. }
  330. ++b;
  331. }
  332. }
  333. }
  334. public static void quickSort(int[] array, float[] values, int low, int high) {
  335. if (low < high) {
  336. int tmp = array[low];
  337. float tmpValue = values[low];
  338. int ii = low, jj = high;
  339. while (ii < jj) {
  340. while (ii < jj && array[jj] >= tmp) {
  341. jj--;
  342. }
  343. array[ii] = array[jj];
  344. values[ii] = values[jj];
  345. while (ii < jj && array[ii] <= tmp) {
  346. ii++;
  347. }
  348. array[jj] = array[ii];
  349. values[jj] = values[ii];
  350. }
  351. array[ii] = tmp;
  352. values[ii] = tmpValue;
  353. quickSort(array, values, low, ii - 1);
  354. quickSort(array, values, ii + 1, high);
  355. }
  356. }
  357. private static int med3(double[] x, int a, int b, int c, DoubleComparator comp) {
  358. int ab = comp.compare(x[a], x[b]);
  359. int ac = comp.compare(x[a], x[c]);
  360. int bc = comp.compare(x[b], x[c]);
  361. return ab < 0 ? (bc < 0 ? b : (ac < 0 ? c : a)) : (bc > 0 ? b : (ac > 0 ? c : a));
  362. }
  363. private static void vecSwap(double[] x, int a, int b, int n) {
  364. for (int i = 0; i < n; ++b) {
  365. swap(x, a, b);
  366. ++i;
  367. ++a;
  368. }
  369. }
  370. private static void swap(double[] x, int a, int b) {
  371. double t = x[a];
  372. x[a] = x[b];
  373. x[b] = t;
  374. }
  375. public static void selectionSort(int[] a, int[] y, int from, int to, IntComparator comp) {
  376. for (int i = from; i < to - 1; ++i) {
  377. int m = i;
  378. int u;
  379. for (u = i + 1; u < to; ++u) {
  380. if (comp.compare(a[u], a[m]) < 0) {
  381. m = u;
  382. }
  383. }
  384. if (m != i) {
  385. u = a[i];
  386. a[i] = a[m];
  387. a[m] = u;
  388. u = y[i];
  389. y[i] = y[m];
  390. y[m] = u;
  391. }
  392. }
  393. }
  394. public static void selectionSort(double[] a, double[] y, int from, int to,
  395. DoubleComparator comp) {
  396. for (int i = from; i < to - 1; ++i) {
  397. int m = i;
  398. for (int u = i + 1; u < to; ++u) {
  399. if (comp.compare(a[u], a[m]) < 0) {
  400. m = u;
  401. }
  402. }
  403. if (m != i) {
  404. double temp = a[i];
  405. a[i] = a[m];
  406. a[m] = temp;
  407. temp = y[i];
  408. y[i] = y[m];
  409. y[m] = temp;
  410. }
  411. }
  412. }
  413. public static void main(String [] args) {
  414. int len = 100000000;
  415. double [] predicts = new double[len];
  416. double [] labels = new double[len];
  417. Random r = new Random();
  418. for(int i = 0; i < len; i++) {
  419. predicts[i] = 1.0;
  420. labels[i] = 1.0;
  421. }
  422. DoubleComparator cmp = new DoubleComparator() {
  423. @Override public int compare(double i, double i1) {
  424. if (Math.abs(i - i1) < 10e-12) {
  425. return 0;
  426. } else {
  427. return i - i1 > 10e-12 ? 1 : -1;
  428. }
  429. }
  430. @Override public int compare(Double o1, Double o2) {
  431. if (Math.abs(o1 - o2) < 10e-12) {
  432. return 0;
  433. } else {
  434. return o1 - o2 > 10e-12 ? 1 : -1;
  435. }
  436. }
  437. };
  438. while(len-- > -10) {
  439. System.out.println("len=" + len);
  440. quickSort(predicts, labels, 0, len, cmp);
  441. }
  442. }
  443. }