2

I'm doing an algorithm course and here is the instructor's answer about how to reverse an array without using reverse js method:

function solution(arr) {
  for(var i=0; i < arr.length/2; i++) {
    var tempVar = arr[i]
    arr[i] = arr[arr.length - 1 - i]
    arr[arr.length - 1 - i] = tempVar
  }

  return arr
}

I did understand everything, EXCEPT this detail:

arr.length/2

In this line below:

 for(var i=0; i < arr.length/2; i++) {

what does it mean? What its purpose?

2
  • It's the limit for when the for loop stops. Are you familiar with for loops or is it just this algorithm which is not clear? Commented Jan 8, 2020 at 11:30
  • I'm familiar with loops.I didn't understand the purpose to divide for 2 the array length to solve the problem. Commented Jan 8, 2020 at 11:32

3 Answers 3

3

To reverse a string, you have to swap characters of first half of the string with the last half.

let str = 'abcde';

You have to swap a with e, b with d.

ab is the first half of the string. So simply run loop over the first half of the string and swap ith character with arr.length - 1 - ith character as below

var tempVar = arr[i]
arr[i] = arr[arr.length - 1 - i]
arr[arr.length - 1 - i] = tempVar
Sign up to request clarification or add additional context in comments.

2 Comments

If as you say For reverse a string, you have to swap first half of the string with the last half then for string abcdef your result will be cdeabc - which is not reversed string.
New edition which is For reverse a string, you have to swap characters of half of the string with the last half - also is not correct.
2

The algorithm swaps two elements that are on equal distance from both ends of the array. The number of operations needed is number_of_operations = number_of_elements / number_elements_operated_on and since it's doing two elements at once, that's number_of_elements / 2. And hence the reason to use arr.length / 2 as the limit of the for loop. Here is a representation of what happens.

Given an array [1, 2, 3, 4, 5, 6] then array.length is 6 and the following operations are performed:

//loop i = 0, 0 < 3 == true, execute
[1, 2, 3, 4, 5, 6] -> [6, 2, 3, 4, 5, 1]
 ^--------------^      ^--------------^

//loop i = 1, 1 < 3 == true, execute
[6, 2, 3, 4, 5, 1] -> [6, 5, 3, 4, 2, 1]
    ^--------^            ^--------^


//loop i = 2, 2 < 3 == true, execute
[6, 5, 3, 4, 2, 1] -> [6, 5, 4, 3, 2, 1]
       ^--^                  ^--^

//i = 3, 3 < 3 == false, loop stops

This works perfectly fine with odd number of elements, since there is going to just be one element in middle when you get to it.

Given an array [1, 2, 3, 4, 5] then array.length is 5 and the following operations are performed:

//loop i = 0, 0 < 2.5 == true, execute
[1, 2, 3, 4, 5] -> [5, 2, 3, 4, 1]
 ^-----------^      ^-----------^

//loop i = 1, 1 < 2.5 == true, execute
[5, 2, 3, 4, 1] -> [5, 4, 3, 2, 1]
    ^-----^            ^-----^


//loop i = 2, 2 < 2.5 == true, execute
[5, 2, 3, 4, 1] -> [5, 4, 3, 2, 1]
       ^                  ^

//i = 3, 3 < 2.5 == false, loop stops

Comments

1

Algorithm start with first and last element and swap them. Next it take the second element from the begin and from the end and swap them. And etc it swap all the elements with same distance from center.

So algorithm go to the center of array from both sides. And it need only half of length of array from both side to proceed. So that statement arr.length/2 actually the expression which is a half of length.

Which is used as limit of the loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.