leoperd 83d3234fe2 Update BubbleSort.c (#343)
Update BubbleSort.c
2019-10-12 15:40:42 +05:30

31 lines
806 B
C

/* 1st way: Using 2 pointers */
int* sortedSquares(int* A, int ASize, int* returnSize){
int i, start = 0, end = ASize - 1;
int *res = malloc(ASize * sizeof(int));
*returnSize = ASize;
for (i = ASize - 1; i >= 0; i--) {
if (abs(A[start]) > A[end]) {
res[i] = A[start] * A[start];
start++;
} else {
res[i] = A[end] * A[end];
end--;
}
}
return res;
}
/* 2nd way: Using qsort */
int cmpval(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
int* sortedSquares(int* A, int ASize, int* returnSize) {
int *res = malloc(ASize * sizeof(int));
for (int i = 0; i < ASize; i++)
res[i] = A[i] * A[i];
*returnSize = ASize;
qsort(res, ASize, sizeof(int), cmpval);
return res;
}