📜 ⬆️ ⬇️

Sort by bubble in Qualcomm code

Funny user found today shared user fj333 with Reddit . Understanding the proprietary code of Qualcomm Technologies for Android that appeared a year ago, he discovered that an unknown programmer had decided to use bubble sorting in the production code in order to find ... the maximum in the array.

You can view the source file using the link on Github or under the cut, and any owner of the device with Qualcomm Snapdragon 200 MSM8610 running Android can rate it at work.

As is known to anyone who is familiar with sorting algorithms, bubble sorting is a learning algorithm, and is usually not used in industrial code due to its inefficiency ; The fact is that in the worst and average cases it has complexity O (n 2 ) , besides its capacitive complexity in this case is O (n) . Who this did not convince - even Barack Obama does not recommend the use of bubble sorting.
')
And all this without taking into account the fact that a simple search would be enough for finding the maximum.

#ifndef ABS #define ABS(x) (((x) < 0) ? -(x) : (x)) #endif /*============================================================================== * Function: bubblesort * * Description: Subroutine for sorting 1-D array elements * * Parameters: double *x ---> input one-dimensional array * int n ---> size of input array * int *m ---> indices of sorted elements *============================================================================*/ void bubblesort(double *x, int n, int *m) { int i, j, t1; double t2; for(i = 0; i < n; i++) m[i] = i; for(i = 0; i < n; i++) { for(j = 0; j <n-1; j++) { if(x[j] < x[j+1]) { t2 = x[j+1]; x[j+1] = x[j]; x[j] = t2; t1 = m[j+1]; m[j+1] = m[j]; m[j] = t1; } } } } /* bubblesort */ /*============================================================================== * Function: absmax * * Description: * * Parameters: double *x ---> input one-dimensional array * int n ---> size of input array *============================================================================*/ double absmax(double *x, int n) { int j, *l; int index = 0; double *y; l = (int *)malloc(n * sizeof(int)); if (NULL == l) { CDBG("%s: Error mem alloc for l.\n", __func__); return -1; } y = (double *)malloc(n * sizeof(double)); if (NULL == y) { free(l); CDBG("%s: Error mem alloc for y.\n", __func__); return -1; } for(j = 0; j < n; j++) y[j] = ABS(x[j]); bubblesort(y, n, l); index = l[0]; free(l); free(y); return ABS(x[index]); } 

Did the Code Review? This story is silent ...

Source: https://habr.com/ru/post/333782/


All Articles