I'm currently having difficulty understanding the following logic for Sorted Square Algorithm. I listed the code in question. Could someone help me break down the logic for the following algorithm?
func sortedSquareAlgorithm(_ array: [Int]) -> [Int] {
//Initilizing Empty Array.
//?? variable sortedSquares initilizing empty tupil ??
var sortedSquares = Array(repeating: 0, count: array.count)
//Pointers are initilized for left and right side of array
var smallerValueIdx = 0
var largeValueIdx = array.count - 1
// stride method is initilized
//?? what is this line codes purpose? ??
for idx in stride(from: array.count - 1, through: 0, by: -1) {
//constant variale is declared for each pointer
let smallerValue = array[smallerValueIdx]
let largerValue = array[largeValueIdx]
//?? what is abs in this line code ??
if abs(smallerValue) > abs(largerValue) {
sortedSquares[idx] = smallerValue * smallerValue
smallerValueIdx = 1
} else {
sortedSquares[idx] = largerValue * largerValue
largeValueIdx -= 1
}
}
return sortedSquares
}
CodePudding user response:
I have added comments to the each line of the code.
func sortedSquareAlgorithm(_ array: [Int]) -> [Int] {
/* Initialising an empty array.
Array size is equal to the size of the input array.
Array contains value 0 in all the places.
*/
var sortedSquares = Array(repeating: 0, count: array.count)
// pointer for the first element of the array.
var smallerValueIdx = 0
//pointer for the last element of the array
var largeValueIdx = array.count - 1
// stride returns a sequence from a starting value toward, and possibly including, an end value, stepping by the specified amount.
for idx in stride(from: array.count - 1, through: 0, by: -1) {
//assign the first element of the input array to smallerValue
let smallerValue = array[smallerValueIdx]
//assign the last element of the input array to smallerValue
let largerValue = array[largeValueIdx]
//abs -> returns the absolute value of the given number.
/* Square values of all positive and negative numbers are positive.
So you have to compare the absolute number of each element to decide which has the larger square value.
*/
if abs(smallerValue) > abs(largerValue) {
//if the smallerValue is largest among two the squre of that will be added to the end of the unsorted array.
//smallerValue * smallerValue gives the square value of that smallerValue
sortedSquares[idx] = smallerValue * smallerValue
smallerValueIdx = 1
} else {
//if the smallerValue is smallest among two the squre of that will be added to the beginning of the unsorted array.
sortedSquares[idx] = largerValue * largerValue
largeValueIdx -= 1
}
}
return sortedSquares
}
Following is an explanation of how the algorithm will work if the array [3, -1, 5]
is given as the input
sortedSquareAlgorithm([3,-1,5])
/*
input_array = [3, -1, 5]
sortedSquares = [0,0,0]
smallerValueIdx = 0
largeValueIdx = 2
idx will return the sequence [2,1,0]
inside for loop
iteration 1 ---> idx = 2
smallerValue = array[0] = 3
largerValue = array[2] = 5
abs(3) > abs(5) ---> 3 > 5 ---> false //so else part will execute
sortedSquares[2] ---> 5 * 5 = 25
largeValueIdx = 2-1 = 1
// after the 1st iteration sortedSquares = [0,0,25]
iteration 2 ---> idx = 1
smallerValue = array[0] = 3
largerValue = array[1] = -1
abs(3) > abs(-1) ---> 3 > 1 ---> true //so if part will execute
sortedSquares[1] ---> 3 * 3 = 9
smallerValue = 0 1 = 1
// after the 2nd iteration sortedSquares = [0,9,25]
iteration 3 ---> idx = 0
smallerValue = array[1] = -1
largerValue = array[1] = -1
abs(-1) > abs(-1) ---> 1 > 1 ---> false //so else part will execute
sortedSquares[0] ---> -1 * -1 = 1
largeValueIdx = 1-1 = 0
// after the 3rd iteration sortedSquares = [1,9,25]
end of for loop
returns sortedSquares = [1,9,25]
*/
update
you can also get the required out put using the following code.
let array = [-1,2,5,3,-2]
let squaredArray = array.map({ number in number * number }).sorted()
print(squaredArray)
I did not compare the performance of both codes. But this one looks more simpler to me.