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:
- The first half of the array is sorted in ascending order.
- 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
Input:
- The program reads the size of the array (
n) and then the elements of the array.
- The program reads the size of the array (
Sorting the First Half:
- A nested loop is used to sort the first half of the array (indices
0ton/2 - 1) in ascending order. - If an element at
arr[i]is greater thanarr[j], they are swapped to ensure smaller elements come first.
- A nested loop is used to sort the first half of the array (indices
Sorting the Second Half:
- A nested loop is used to sort the second half of the array (indices
n/2ton - 1) in descending order. - If an element at
arr[i]is smaller thanarr[j], they are swapped to ensure larger elements come first.
- A nested loop is used to sort the second half of the array (indices
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:
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:
Input:
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:
Key Points
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.
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.
- The program dynamically calculates the midpoint of the array (
Edge Cases:
- If the array size is odd, the first half will have one more element than the second half. This is because
n/2truncates 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).
- If the array size is odd, the first half will have one more element than the second half. This is because
Improvements
- Optimize Sorting:
- Replace the nested loop with efficient sorting algorithms like Arrays.sort() or custom logic using the Collections framework.
- 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