Programming Alogorithm Series: Reverse an Array

Reverse an Array is one of the most common programming algorithm questions that is asked in many tech interviews. Here is a quick way with sample code:

using System;

System;

namespace ReverseArray
{
/// <summary>
/// Reverse an Array quickly
/// </summary>

ReverseArray
{
/// <summary>
/// Reverse an Array quickly
/// </summary>

class Program
{
   static void Main(string[] args)
   {
      int[] array = new int[] {1,2,3,4,5,6,7,8,9};
      // Print before reverse
      for (int i = 0; i < array.Length; i++)
      {
         Console.Write(array[i]);
      }
      Console.ReadLine();
 

class Program
{
   static void Main(string[] args)
   {
      int[] array = new int[] {1,2,3,4,5,6,7,8,9};
      // Print before reverse
      for (int i = 0; i < array.Length; i++)
      {
         Console.Write(array[i]);
      }
      Console.ReadLine();
 

      ReverseArrayClass.ReverseArray(array);
     // Print after reverse
     for (int i = 0; i < array.Length; i++)
     {
        Console.Write(array[i]);
     }
     Console.ReadLine();
     }
}

///
<summary>
/// Array Reverse Alogorithm
/// </summary>

class ReverseArrayClass
{
   public static void ReverseArray(int[] array)
   {
      int tempInt;
      for (int i=0; i<(array.Length/2); i++)
      {
         tempInt = array[i];
         array[i] = array[array.Length -i-1];
         array[array.Length -i-1] = tempInt;
      }
   }
}
}

class ReverseArrayClass
{
   public static void ReverseArray(int[] array)
   {
      int tempInt;
      for (int i=0; i<(array.Length/2); i++)
      {
         tempInt = array[i];
         array[i] = array[array.Length -i-1];
         array[array.Length -i-1] = tempInt;
      }
   }
}
}
Published Monday, September 18, 2006 11:20 PM by sanjeebsarangi
Filed under:

Comments

Monday, February 25, 2008 2:16 AM by sas

# re: Programming Alogorithm Series: Reverse an Array

we are only sumarray use in with reverse array

Tuesday, July 19, 2011 5:47 AM by Prada handbags outlet

# re: Programming Alogorithm Series: Reverse an Array

I have been a regular visitor of this site and I love reading blogs posted here. They are truly very well written, precise and to the point. Thanks.

Tuesday, July 19, 2011 11:22 PM by cheap herve leger dress

# re: Programming Alogorithm Series: Reverse an Array

I am so thrilled for having found your site.

Thursday, July 21, 2011 2:41 AM by cheap Louis Vuitton handbags

# re: Programming Alogorithm Series: Reverse an Array

Nice post. My friend John told me about this blog some weeks ago but this is the first time Im visting. Ill undoubtedly be back.

Monday, July 25, 2011 6:07 AM by Louis Vuitton bags oulet

# re: Programming Alogorithm Series: Reverse an Array

I just read through the full article of yours and it was quite good. nice information!First place in google results&Thank you.

Friday, July 29, 2011 9:30 PM by wholesale caps

# re: Programming Alogorithm Series: Reverse an Array

I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often.<a href="www.wholesale-caps-shop.com/" title="wholesale caps">wholesale caps</a>

Sunday, July 31, 2011 11:49 PM by Monster beats studio

# re: Programming Alogorithm Series: Reverse an Array

I always enjoy your posts, your real, and some times funny as well. keep it up!<a href="www.cheapmonsterbeatsheadphones.com/" title="Monster beats studio">Monster beats studio</a>

Monday, August 08, 2011 8:30 AM by ZerneOrataDit

# re: Programming Alogorithm Series: Reverse an Array

You actually make it appear really easy together with your presentation however I to find this topic to be actually one thing which I think I'd never understand. It sort of feels too complicated and very wide for me. I am looking ahead in your next submit, IЎ¦ll attempt to get the hang of it!

Monday, August 08, 2011 9:13 PM by north face jackets outlet

# re: Programming Alogorithm Series: Reverse an Array

Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog.[url=www.northfacejacketsoutletstore.com]north face jackets outlet[/url]

Monday, September 05, 2011 11:51 PM by hooher tod

# re: Programming Alogorithm Series: Reverse an Array

Yes there should realize the reader to RSS my feed to RSS commentary, quite simply

Tuesday, November 08, 2011 1:05 AM by Buy OEM software online

# re: Programming Alogorithm Series: Reverse an Array

tWNg3L Totally agree with you, about a week ago wrote about the same in my blog..!!

Leave a Comment

(required) 
(required) 
(optional)
(required)