19 Apr 2024

Increasing-Decreasing in Array

Increasing-Decreasing in Array 

the first half sorted in ascending order and the second half sorted in descending order.

Sorting an Array into Increasing and Decreasing Order in Java

In this post, we will learn how to divide an array into two parts:

  1. The first half of the array is sorted in ascending order.
  2. The second half of the array is sorted in descending order.

We’ll implement this functionality in Java using nested loops for sorting. Let’s dive into the code and the explanation.

Input:

 6

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

Output:

1 2 3 6 5 4

the arrays is divided into two parts first half in ascending order and second half in decending order

6

[7 2 0 1 4,2]

0 1 2 7 4 2

import java.util.*;

class HelloWorld {

    public static void main(String[] args) {

        Scanner sc=new Scanner(System.in);

        int n=sc.nextInt();

        int arr[]=new int[n];

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

            arr[i]=sc.nextInt();

        }

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

            for(int j=i+1;j<n;j++){

                if(arr[i]>arr[j]){

                    int temp=arr[j];

                    arr[j]=arr[i];

                    arr[i]=temp;

                }

            }

        }

        for(int i=n/2;i<n;i++){

            for(int j=i+1;j<n;j++){

                if(arr[i]<arr[j]){

                    int temp=arr[j];

                    arr[j]=arr[i];

                    arr[i]=temp;

                }

            }

        }

        for(int a:arr){

            System.out.print(a+" ");

        }

    }

}

How the Code Works

  1. Input:

    • The program reads the size of the array (n) and then the elements of the array.
  2. Sorting the First Half:

    • A nested loop is used to sort the first half of the array (indices 0 to n/2 - 1) in ascending order.
    • If an element at arr[i] is greater than arr[j], they are swapped to ensure smaller elements come first.
  3. Sorting the Second Half:

    • A nested loop is used to sort the second half of the array (indices n/2 to n - 1) in descending order.
    • If an element at arr[i] is smaller than arr[j], they are swapped to ensure larger elements come first.
  4. Output:

    • The modified array is printed, where the first half is in ascending order and the second half is in descending order.

Example Input and Output

Input:


6 1 2 3 4 5 6

Process:

  • Array: [1, 2, 3, 4, 5, 6]
  • First half sorted in ascending order: [1, 2, 3]
  • Second half sorted in descending order: [6, 5, 4]

Output:


1 2 3 6 5 4

Input:

6 7 2 0 1 4 2

Process:

  • Array: [7, 2, 0, 1, 4, 2]
  • First half sorted in ascending order: [0, 1, 2]
  • Second half sorted in descending order: [7, 4, 2]

Output:


0 1 2 7 4 2

Key Points

  1. Sorting Logic:

    • The program uses a basic nested loop for sorting, which has a time complexity of O(n²). While this is simple, it may not be optimal for very large arrays.
  2. Dynamic Half Splitting:

    • The program dynamically calculates the midpoint of the array (n/2) to divide it into two halves, regardless of the size of the array.
  3. Edge Cases:

    • If the array size is odd, the first half will have one more element than the second half. This is because n/2 truncates the decimal in integer division.
    • Example: For an array of size 5, the first half will contain indices [0, 1] (two elements), and the second half will contain indices [2, 3, 4] (three elements).

Improvements

  1. Optimize Sorting:
    • Replace the nested loop with efficient sorting algorithms like Arrays.sort() or custom logic using the Collections framework.
  2. Handle Odd-Sized Arrays:
    • If needed, ensure an even split by explicitly defining the midpoint or handling the extra element in one of the halves.

Conclusion

This program provides a simple way to divide an array into two parts:

  • The first part sorted in ascending order.
  • The second part sorted in descending order.

While it uses basic sorting logic, it’s a great starting point to understand array manipulation in Java. Feel free to try it out and optimize it further!


No comments:

Post a Comment