File tree Expand file tree Collapse file tree 1 file changed +10
-6
lines changed
program/c/src/oracle/model Expand file tree Collapse file tree 1 file changed +10
-6
lines changed Original file line number Diff line number Diff line change 1111 * thinking of a as 1-based array. Fortunately, BPF compiler optimizes that for us.
1212 */
1313void heapsort (int64_t * a , uint64_t n ) {
14+ if (n <= 1 ) return ;
15+
1416 /*
1517 * This is a bottom-up heapify which is linear in time.
1618 */
17- for (int i = n / 2 - 1 ; i >= 0 ; i -- ) {
19+ for (uint64_t i = n / 2 - 1 ;; -- i ) {
1820 int64_t root = a [i ];
19- int j = i * 2 + 1 ;
21+ uint64_t j = i * 2 + 1 ;
2022 while (j < n ) {
21- if (j + 1 < n && a [j ] < a [j + 1 ]) j ++ ;
23+ if (j + 1 < n && a [j ] < a [j + 1 ]) ++ j ;
2224 if (root >= a [j ]) break ;
2325 a [(j - 1 ) / 2 ] = a [j ];
2426 j = j * 2 + 1 ;
2527 }
2628 a [(j - 1 ) / 2 ] = root ;
29+
30+ if (i == 0 ) break ;
2731 }
2832
29- for (int i = n - 1 ; i > 0 ; i -- ) {
33+ for (uint64_t i = n - 1 ; i > 0 ; -- i ) {
3034 int64_t tmp = a [0 ];
3135 a [0 ] = a [i ];
3236 a [i ] = tmp ;
3337
3438 int64_t root = a [0 ];
35- int j = 1 ;
39+ uint64_t j = 1 ;
3640 while (j < i ) {
37- if (j + 1 < i && a [j ] < a [j + 1 ]) j ++ ;
41+ if (j + 1 < i && a [j ] < a [j + 1 ]) ++ j ;
3842 if (root >= a [j ]) break ;
3943 a [(j - 1 ) / 2 ] = a [j ];
4044 j = j * 2 + 1 ;
You can’t perform that action at this time.
0 commit comments