ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

In-place Array Reversal

Visual Basic .NET

Goal -- WPM

Ready
Exercise Algorithm Area
1Public Module ArrayReversal
2
3' Reverses an array in-place.
4' This means the original array is modified directly without creating a new one.
5Public Sub ReverseArrayInPlace(ByVal arr() As Integer)
6' Check for null or empty array, or array with only one element.
7' In these cases, no reversal is needed.
8If arr Is Nothing OrElse arr.Length <= 1 Then
9Return
10End If
11
12Dim left As Integer = 0
13Dim right As Integer = arr.Length - 1
14
15' Loop until the left pointer crosses or meets the right pointer.
16' The loop invariant is that the elements outside the range [left, right] are already reversed.
17While left < right
18' Swap the elements at the left and right pointers.
19Dim temp As Integer = arr(left)
20arr(left) = arr(right)
21arr(right) = temp
22
23' Move the pointers towards the center.
24left += 1
25right -= 1
26End While
27End Sub
28
29' Helper function to print array elements for demonstration.
30' Not part of the core reversal algorithm but useful for testing.
31Public Sub PrintArray(ByVal arr() As Integer)
32If arr Is Nothing Then
33Console.WriteLine("Array is Nothing.")
34Return
35End If
36
37If arr.Length = 0 Then
38Console.WriteLine("[]")
39Return
40End If
41
42Console.Write("[")
43For i As Integer = 0 To arr.Length - 2
44Console.Write(arr(i) & ", ")
45Next
46Console.WriteLine(arr(arr.Length - 1) & "]")
47End Sub
48
49' Example usage (can be called from Main or elsewhere).
50Public Sub ExampleUsage()
51Dim testArray1() As Integer = {1, 2, 3, 4, 5}
52Console.WriteLine("Original Array 1:")
53PrintArray(testArray1)
54ReverseArrayInPlace(testArray1)
55Console.WriteLine("Reversed Array 1:")
56PrintArray(testArray1)
57
58Dim testArray2() As Integer = {10, 20, 30, 40}
59Console.WriteLine("\nOriginal Array 2:")
60PrintArray(testArray2)
61ReverseArrayInPlace(testArray2)
62Console.WriteLine("Reversed Array 2:")
63PrintArray(testArray2)
64
65Dim emptyArray() As Integer = {}
66Console.WriteLine("\nOriginal Empty Array:")
67PrintArray(emptyArray)
68ReverseArrayInPlace(emptyArray)
69Console.WriteLine("Reversed Empty Array:")
70PrintArray(emptyArray)
71
72Dim singleElementArray() As Integer = {42}
73Console.WriteLine("\nOriginal Single Element Array:")
74PrintArray(singleElementArray)
75ReverseArrayInPlace(singleElementArray)
76Console.WriteLine("Reversed Single Element Array:")
77PrintArray(singleElementArray)
78End Sub
79
80End Module
Algorithm description viewbox

In-place Array Reversal

Algorithm description:

This function reverses the elements of an array in-place, meaning it modifies the original array directly without allocating any additional memory for a new array. It achieves this by swapping elements from the beginning and end of the array, moving inwards. This is a common operation for data manipulation and is efficient in terms of memory usage.

Algorithm explanation:

The `ReverseArrayInPlace` function utilizes two pointers, `left` starting at the beginning of the array and `right` starting at the end. The algorithm iteratively swaps the elements pointed to by `left` and `right` and then moves `left` one step forward and `right` one step backward. The loop continues as long as `left` is less than `right`. The loop invariant is that all elements outside the range `[left, right]` have already been swapped into their final reversed positions. The time complexity is O(n), where n is the number of elements in the array, because each element is swapped at most once. The space complexity is O(1) because the reversal is performed in-place, using only a constant amount of extra space for the temporary variable used during swapping. Edge cases such as null arrays, empty arrays, and arrays with a single element are handled by the initial conditional check, preventing any operations on invalid inputs and ensuring correctness.

Pseudocode:

Function ReverseArrayInPlace(array):
  If array is null or has 0 or 1 element, return.
  Initialize left pointer to 0.
  Initialize right pointer to length of array - 1.
  While left < right:
    Swap element at left with element at right.
    Increment left pointer.
    Decrement right pointer.
  End While
End Function