if you want to remove an article from website contact us from top.

    write a program to reverse a given array by swapping the elements within it

    Mohammed

    Guys, does anyone know the answer?

    get write a program to reverse a given array by swapping the elements within it from screen.

    Reverse An Array In Java

    This tutorial will explain how to Reverse an Array in Java. Learn the various methods of Reversing an Array in Java with the help of code examples.

    Reverse An Array In Java – 3 Methods With Examples

    Last Updated: August 7, 2022

    Reversing an Array is one of the Crucial Operations in Java. In this tutorial, we will Learn how to Reverse an Array in Java:

    Sometimes programmers need to process arrays starting with the last element, in that case, it is always efficient to reverse the array so that the first element is placed at the last position in the array, and the second element is placed at the second last position in the array and so on until the last element is at the first index.

    => Check Out The Ultimate Java Training Series Here

    Let’s consider an array as shown below:

    After applying the reverse functionality, the resultant array should be like:

    What You Will Learn:

    Printing Array In Reverse Order

    Reverse An Array Using ArrayList

    Reverse An Array Using Traditional For Loop

    In-place Reversal Of Array

    Frequently Asked Questions

    Conclusion Recommended Reading

    Printing Array In Reverse Order

    Alternatively, if we want to print the array in the reverse order, without actually reversing it, then we can do that just by providing a for loop that will start printing from the end of the array. This is a good option as long as we just want to print the array in reverse order without doing any processing with it.

    The following program prints the array in reverse order.

    import java.util.*;

    import java.util.stream.*;

    public class Main {

    public static void main(String[] args) {

    Integer[] intArray = {10,20,30,40,50,60,70,80,90};

    //print array starting from first element

    System.out.println("Original Array:");

    for(int i=0;i

    System.out.print(intArray[i] + " ");

    System.out.println();

    //print array starting from last element

    System.out.println("Original Array printed in reverse order:");

    for(int i=intArray.length-1;i>=0;i--)

    System.out.print(intArray[i] + " ");

    } }

    Output:

    This is a feasible option to print the array only.

    Java provides various methods to actually reverse the indices of elements in the array. Enlisted below are the various methods that we are going to discuss in detail in this tutorial.

    Using ArrayList reverse method

    Using traditional for loop

    Using in-place reversal

    Reverse An Array Using ArrayList

    Reversing an array in Java can be done using the ‘reverse’ method present in the collections framework. But for this, you first need to convert an array to a list as the ‘reverse’ method takes the list as an argument.

    The following program reverses an array using the ‘reverse’ method.

    import java.util.*; public class Main {

    /*function reverses the elements of the array*/

    static void reverse(Integer myArray[])

    {

    Collections.reverse(Arrays.asList(myArray));

    System.out.println("Reversed Array:" + Arrays.asList(myArray));

    }

    public static void main(String[] args)

    {

    Integer [] myArray = {1,3,5,7,9};

    System.out.println("Original Array:" + Arrays.asList(myArray));

    reverse(myArray); } }

    Output:

    In this program, we use the reverse function on an array by changing it into the list.

    In a similar manner, we can also reverse a string array as shown in the following example.

    Example:

    import java.util.*; public class Main {

    /*function reverses the elements of the array*/

    static void reverse(String myArray[])

    {

    Collections.reverse(Arrays.asList(myArray));

    System.out.println("Reversed Array:" + Arrays.asList(myArray));

    }

    public static void main(String[] args)

    {

    String [] myArray = {"one", "Two", "Three", "Four", "Five", "Six","Seven"};

    System.out.println("Original Array:" + Arrays.asList(myArray));

    reverse(myArray); } }

    Output:

    The above program defines a string array. By converting it to the list and using the reverse method on it, we reverse the array.

    Reverse An Array Using Traditional For Loop

    Yet another approach for reversing the array is to write a separate method to reverse an array in which you can have a new array and put the elements of the original array into this new array in a reverse manner.

    Check the following implementation.

    public class Main {

    static void reverse_array(char char_array[], int n)

    {

    char[] dest_array = new char[n];

    int j = n;

    for (int i = 0; i < n; i++) {

    dest_array[j - 1] = char_array[i];

    j = j - 1; }

    System.out.println("Reversed array: ");

    for (int k = 0; k < n; k++) {

    System.out.print(dest_array[k] + " ");

    } }

    public static void main(String[] args)

    {

    char [] char_array = {'H','E','L','L','O'};

    System.out.println("Original array: ");

    for (int k = 0; k

    System.out.print(char_array[k] + " ");

    }

    System.out.println();

    reverse_array(char_array, char_array.length);

    स्रोत : www.softwaretestinghelp.com

    Reverse array by swapping in C

    Reverse array by swapping in C

    Forget Code C Reverse array by swapping

    #include

    int main() {

    int array[100], n, c, t, end;

    scanf("%d", &n); end = n - 1;

    for (c = 0; c < n; c++) {

    scanf("%d", &array[c]);

    }

    for (c = 0; c < n/2; c++) {

    t = array[c];

    array[c] = array[end];

    array[end] = t; end--; }

    printf("Reversed array elements are:\n");

    for (c = 0; c < n; c++) {

    printf("%d\n", array[c]);

    } return 0; }

    स्रोत : forgetcode.com

    Write a program to reverse an array or string

    A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

    Write a program to reverse an array or string

    Difficulty Level : Basic

    Last Updated : 08 Sep, 2020

    Given an array (or string), the task is to reverse the array/string.

    Examples :

    Input : arr[] = {1, 2, 3}

    Output : arr[] = {3, 2, 1}

    Input : arr[] = {4, 5, 1, 2}

    Output : arr[] = {2, 1, 5, 4}

    Recommended: Please solve it on “ ” first, before moving on to the solution.

    Iterative way :

    1) Initialize start and end indexes as start = 0, end = n-1

    2) In a loop, swap arr[start] with arr[end] and change start and end as follows :

    start = start +1, end = end – 1

    Another example to reverse a string:

    Below is the implementation of the above approach :

    C++

    // Iterative C++ program to reverse an array

    #include

    using namespace std;

    /* Function to reverse arr[] from start to end*/

    void rvereseArray(int arr[], int start, int end)

    { while (start < end) {

    int temp = arr[start];

    arr[start] = arr[end];

    arr[end] = temp; start++; end--; } }

    /* Utility function to print an array */

    void printArray(int arr[], int size)

    {

    for (int i = 0; i < size; i++)

    cout << arr[i] << " ";

    cout << endl; }

    /* Driver function to test above functions */

    int main() {

    int arr[] = {1, 2, 3, 4, 5, 6};

    int n = sizeof(arr) / sizeof(arr[0]);

    // To print original array

    printArray(arr, n); // Function calling

    rvereseArray(arr, 0, n-1);

    cout << "Reversed array is" << endl;

    // To print the Reversed array

    printArray(arr, n); return 0; }

    C

    Java

    Python

    C#

    C Java Python C# PHP

    Output :

    1 2 3 4 5 6 Reversed array is 6 5 4 3 2 1

    Time Complexity : O(n)Recursive Way :

    1) Initialize start and end indexes as start = 0, end = n-1

    2) Swap arr[start] with arr[end]

    3) Recursively call reverse for rest of the array.

    Below is the implementation of the above approach :

    C++

    // Recursive C++ program to reverse an array

    #include

    using namespace std;

    /* Function to reverse arr[] from start to end*/

    void rvereseArray(int arr[], int start, int end)

    { if (start >= end) return;

    int temp = arr[start];

    arr[start] = arr[end];

    arr[end] = temp;

    // Recursive Function calling

    rvereseArray(arr, start + 1, end - 1);

    }

    /* Utility function to print an array */

    void printArray(int arr[], int size)

    {

    for (int i = 0; i < size; i++)

    cout << arr[i] << " ";

    cout << endl; }

    /* Driver function to test above functions */

    int main() {

    int arr[] = {1, 2, 3, 4, 5, 6};

    // To print original array

    printArray(arr, 6); // Function calling

    rvereseArray(arr, 0, 5);

    cout << "Reversed array is" << endl;

    // To print the Reversed array

    printArray(arr, 6); return 0; }

    C

    Java

    Python

    C#

    C Java Python C# PHP

    Output :

    1 2 3 4 5 6 Reversed array is 6 5 4 3 2 1

    Time Complexity : O(n)Another Approach: Using Python List slicing

    Python3

    def reverseList(A): print( A[::-1])

    # Driver function to test above function

    A = [1, 2, 3, 4, 5, 6]

    print(A)

    print("Reversed list is")

    reverseList(A)

    Output:

    [1, 2, 3, 4, 5, 6] Reversed list is [6, 5, 4, 3, 2, 1]

    Please write comments if you find any bug in the above programs or other ways to solve the same problem.

    स्रोत : www.geeksforgeeks.org

    Do you want to see answer or more ?
    Mohammed 15 day ago
    4

    Guys, does anyone know the answer?

    Click For Answer