mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-26 21:58:56 +01:00
Merge pull request #96 from jsca-kwok/jk-sorted-squares
Add algorithm for returning sorted squares
This commit is contained in:
commit
27d725d8de
10 changed files with 89 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
*.DS_Store
|
|
@ -7,6 +7,7 @@
|
|||
* [Find All Duplicates In An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_all_duplicates_in_an_array.rb)
|
||||
* [Find The Highest Altitude](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_the_highest_altitude.rb)
|
||||
* [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb)
|
||||
* [Sort Squares of an Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sort_squares_of_an_array.rb)
|
||||
* Binary Trees
|
||||
* [Inorder Traversal](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/inorder_traversal.rb)
|
||||
* [Invert](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/invert.rb)
|
||||
|
|
87
data_structures/arrays/sort_squares_of_an_array.rb
Normal file
87
data_structures/arrays/sort_squares_of_an_array.rb
Normal file
|
@ -0,0 +1,87 @@
|
|||
# Arrays - Sorted Squares
|
||||
|
||||
# Algorithm challenge description:
|
||||
# Given an integer array nums sorted in non-decreasing order
|
||||
# return an array of the squares of each number sorted in non-decreasing order.
|
||||
# Input: [4, -1, -9, 2]
|
||||
# Output: [1, 4, 16, 81]
|
||||
|
||||
#
|
||||
# Approach 1: is using Ruby function (for sure)!
|
||||
#
|
||||
|
||||
def sorted_squares(nums)
|
||||
nums.map! { |num| num**2 }.sort
|
||||
end
|
||||
print(sorted_squares([4, -1, -9, 2]))
|
||||
|
||||
#
|
||||
# Approach 2: is using bubble sort
|
||||
#
|
||||
|
||||
def bubble_sort(array)
|
||||
array_length = array.size
|
||||
return array if array_length <= 1
|
||||
loop do
|
||||
swapped = false
|
||||
(array_length - 1).times do |i|
|
||||
if array[i] > array[i + 1]
|
||||
array[i], array[i + 1] = array[i + 1], array[i]
|
||||
swapped = true
|
||||
end
|
||||
end
|
||||
break unless swapped
|
||||
end
|
||||
array
|
||||
end
|
||||
|
||||
#
|
||||
# Time complexity: O(n logn), where n is the length of the array.
|
||||
# Space complexity: O(n) or O(logn)
|
||||
#
|
||||
|
||||
def sorted_squares(nums)
|
||||
# This takes O(n)
|
||||
nums.map! { |num| num**2 }
|
||||
# This can take Ο(n logn)
|
||||
bubble_sort(nums)
|
||||
end
|
||||
print(sorted_squares([4, -1, -9, 2]))
|
||||
|
||||
#
|
||||
# Approach 3: solving without ruby sort method. Using two-pointers
|
||||
#
|
||||
# Time complexity: O(n), where n is the length of the array.
|
||||
# Space complexity: O(n), if you take output into account and O(1) otherwise.
|
||||
#
|
||||
def sorted_squares(nums)
|
||||
p1 = 0
|
||||
p2 = nums.length - 1
|
||||
# since we're returing the result in ascending order,
|
||||
# we'll fill in the array from the end
|
||||
max_index = p2
|
||||
output = []
|
||||
while p1 < p2
|
||||
nums1_square = nums[p1] * nums[p1]
|
||||
nums2_square = nums[p2] * nums[p2]
|
||||
if nums1_square < nums2_square
|
||||
output[max_index] = nums2_square
|
||||
p2 -= 1
|
||||
elsif nums1_square > nums2_square
|
||||
output[max_index] = nums1_square
|
||||
p1 += 1
|
||||
else
|
||||
output[max_index] = nums1_square
|
||||
max_index -= 1
|
||||
output[max_index] = nums2_square
|
||||
p1 += 1
|
||||
p2 -= 1
|
||||
end
|
||||
max_index -= 1
|
||||
end
|
||||
# to account for any remaining value left in the input array
|
||||
output[max_index] = nums[p1] * nums[p2] if p1 == p2
|
||||
output
|
||||
end
|
||||
|
||||
print(sorted_squares([4, -1, -9, 2]))
|
Loading…
Reference in a new issue