mirror of
https://github.com/TheAlgorithms/C
synced 2025-04-22 13:16:14 +03:00
30 lines
802 B
C
30 lines
802 B
C
/* 1st way: Using 2 pointer */
|
|
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;
|
|
}
|