Reverse an array without using Array.Reverse()

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 715 Views - Last Post: 22 October 2017 - 10:28 AM Rate Topic: -----

#1 soldi3r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-September 17

Reverse an array without using Array.Reverse()

Posted 17 October 2017 - 07:51 AM

Few days back, I went for an interview. They just asked me to write code for reversing the array without using Array.Reverse() method. So, here I am sharing the code that might help someone.

for (int i=0; i<arr.Length / 2; i++)
{
int temp = arr[i];
arr[i] = arr[arr.Length  i  1];
arr[arr.Length  i  1] = temp;
}



Please let me know if there's any other way to do this. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Reverse an array without using Array.Reverse()

#2 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6374
  • View blog
  • Posts: 25,755
  • Joined: 12-December 12

Re: Reverse an array without using Array.Reverse()

Posted 17 October 2017 - 08:22 AM

As an aside, I'd prefer to store arr.Length in a variable (even though the compiler may be clever enough to recognise the optimisation).
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5885
  • View blog
  • Posts: 20,092
  • Joined: 05-May 12

Re: Reverse an array without using Array.Reverse()

Posted 17 October 2017 - 08:25 AM

Other than the hardcoded type on line 3, looks good. What if it is an array of Airplanes.
Was This Post Helpful? 1
  • +
  • -

#4 Thomas1965  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 275
  • Joined: 09-September 16

Re: Reverse an array without using Array.Reverse()

Posted 18 October 2017 - 12:30 AM

Another option would be:
static void Reverse (int[] data)
{
  int i = 0; // first index
  int j = data.Length - 1; // last index

  for (; i < j; ++i, --j)
  {
    int tmp = data[i];
    data[i] = data[j];
    data[j] = tmp;
  }
}


Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6374
  • View blog
  • Posts: 25,755
  • Joined: 12-December 12

Re: Reverse an array without using Array.Reverse()

Posted 18 October 2017 - 12:47 AM

That's the same algorithm ;) just slightly refactored.
Was This Post Helpful? 0
  • +
  • -

#6 Thomas1965  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 275
  • Joined: 09-September 16

Re: Reverse an array without using Array.Reverse()

Posted 18 October 2017 - 02:38 AM

Quote

That's the same algorithm ;)/> just slightly refactored.

Yes it is.
On one of the talks at CppCon I heard that int division is expensive and should be avoided if possible.
I guess it's true for C# as well.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5885
  • View blog
  • Posts: 20,092
  • Joined: 05-May 12

Re: Reverse an array without using Array.Reverse()

Posted 18 October 2017 - 05:25 AM

Division by powers of 2 is not expensive. The compiler is smart enough to bit shift rather than do the long division.
Was This Post Helpful? 0
  • +
  • -

#8 soldi3r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-September 17

Re: Reverse an array without using Array.Reverse()

Posted 18 October 2017 - 05:34 AM

View Postandrewsw, on 17 October 2017 - 08:22 AM, said:

As an aside, I'd prefer to store arr.Length in a variable (even though the compiler may be clever enough to recognise the optimisation).

Yes definitely it'll optimize the code and will work in the same fashion.

View PostThomas1965, on 18 October 2017 - 12:30 AM, said:

Another option would be:
static void Reverse (int[] data)
{
  int i = 0; // first index
  int j = data.Length - 1; // last index

  for (; i < j; ++i, --j)
  {
    int tmp = data[i];
    data[i] = data[j];
    data[j] = tmp;
  }
}



This is a bit confusing me to use --j in for loop.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5885
  • View blog
  • Posts: 20,092
  • Joined: 05-May 12

Re: Reverse an array without using Array.Reverse()

Posted 18 October 2017 - 05:46 AM

I don't think C# has a comma operator. So doing ++i, --j is not going to work...
Was This Post Helpful? 0
  • +
  • -

#10 soldi3r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-September 17

Re: Reverse an array without using Array.Reverse()

Posted 18 October 2017 - 06:25 AM

View PostSkydiver, on 18 October 2017 - 05:46 AM, said:

I don't think C# has a comma operator. So doing ++i, --j is not going to work...

Absolutely, that's the part making me confused.
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6374
  • View blog
  • Posts: 25,755
  • Joined: 12-December 12

Re: Reverse an array without using Array.Reverse()

Posted 18 October 2017 - 07:48 AM

I'm puzzled also. I thought there was a comma operator, but there isn't, but the code works...

Apparently C# has a statement-expression-list which, for all intents, is the same as the comma operator (although it does not work in for-conditionals - which would be pointless anyway).

Added: Well we can't do this: int x = 4, 5; so I gather/guess that statement-expression-list is restricted to variable declarations and the initialisation and iteration parts of for loops.
Was This Post Helpful? 0
  • +
  • -

#12 GazinAtCode  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 69
  • Joined: 26-September 16

Re: Reverse an array without using Array.Reverse()

Posted 18 October 2017 - 08:39 AM

View Postandrewsw, on 17 October 2017 - 08:22 AM, said:

As an aside, I'd prefer to store arr.Length in a variable (even though the compiler may be clever enough to recognise the optimisation).


Yeah, it seems that it does.

And here's a type-neutral version:

void Reverse<T>(T[] arr)
{
    for (int i = 0; i < arr.Length / 2; i++)
    {
        T temp = arr[i];
        arr[i] = arr[arr.Length - i - 1];
        arr[arr.Length - i - 1] = temp;
    }
}


Was This Post Helpful? 0
  • +
  • -

#13 Thomas1965  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 275
  • Joined: 09-September 16

Re: Reverse an array without using Array.Reverse()

Posted 18 October 2017 - 08:41 AM

Quote

I don't think C# has a comma operator.

Quote

The precise rules are to be found in the section The for statement in the C# Language Specification. We see that the third and last "component" in a for (;; ) statement is defined as a statement-expression-list, and a statement-expression-list is defined, in a recursive way, as a list of statement expressions separated by the character , (comma).

Source: https://stackoverflow.com/a/18800537
Was This Post Helpful? 1
  • +
  • -

#14 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5885
  • View blog
  • Posts: 20,092
  • Joined: 05-May 12

Re: Reverse an array without using Array.Reverse()

Posted 18 October 2017 - 09:09 AM

Looks like C# 7.0 supports it... I don't get any compilation errors with:
using System;

class Program
{
    static void Main(string[] args)
    {
        for(int i = 0, j = 17; i < j; i++, j--)
        {
            Console.WriteLine($"{i} {j}");
        }
    }
}



So it's not a matter of having a comma operator like in C, C++, and Javascript, but rather the language definition as noted by Thomas1965 above. From the C# Language Specification:

Quote

8.8.3 The for statement
The for statement evaluates a sequence of initialization expressions and then, while a condition is true, repeatedly executes an embedded statement and evaluates a sequence of iteration expressions.
for-statement:
    for   (   for-initializeropt   ;   for-conditionopt   ;   for-iteratoropt   )   embedded-statement

for-initializer:
    local-variable-declaration
    statement-expression-list

for-condition:
    boolean-expression

for-iterator:
    statement-expression-list

statement-expression-list:
    statement-expression
    statement-expression-list   ,   statement-expression


(Sorry I only have version 5.0 handy. I know I have copies of 6.0 and 7.0 drafts somewhere but I can't find them right now.)
Was This Post Helpful? 0
  • +
  • -

#15 soldi3r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-September 17

Re: Reverse an array without using Array.Reverse()

Posted 18 October 2017 - 03:09 PM

View PostSkydiver, on 18 October 2017 - 09:09 AM, said:

Looks like C# 7.0 supports it... I don't get any compilation errors with:
using System;

class Program
{
    static void Main(string[] args)
    {
        for(int i = 0, j = 17; i < j; i++, j--)
        {
            Console.WriteLine($"{i} {j}");
        }
    }
}


So, it's an update in C# 7.0. But unfortunately I am not having that.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2