Skip to content

Instantly share code, notes, and snippets.

@hongphuc5497
Last active March 23, 2022 17:40
Show Gist options
  • Save hongphuc5497/09de59edeae7a3b3d839cc0d4177cc07 to your computer and use it in GitHub Desktop.
Save hongphuc5497/09de59edeae7a3b3d839cc0d4177cc07 to your computer and use it in GitHub Desktop.
def reverse_array(arr)
temp = []
lastIndex = arr.length - 1
arr.length.times do
temp << arr[lastIndex]
lastIndex -= 1
end
return temp
end
def reverse_array_with_recursion(arr, first_index, last_index)
return arr if first_index > last_index
temp = arr[first_index]
arr[first_index] = arr[last_index]
arr[last_index] = temp
return reverse_array_with_recursion(arr, first_index + 1, last_index - 1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment