Sort the array in ASC and DESC
Sort the array in ASC and DESC
a = {1, 4, 5, 7}:
1.After sorting in ascending order using Arrays.sort(a), the array becomes a = {1, 4, 5, 7}.
2.Array Sorting: The Arrays.sort(a) method is used to sort the array a in ascending order.
3.Printing Sorted Array: The sorted array is then printed to the console using System.out.println(Arrays.toString(a)).
4.Reversing Array (Descending Order): A new array named descendingorder is created to store the reversed (descending) order of the sorted array. A loop is used to iterate through the sorted array and populate the descendingorder array in reverse order.
Let's apply this with the current values:
- When
i = 0,descendingOrder[0] = a[4 - 1 - 0] = a[3] = 7.
- When
i = 1,descendingOrder[1] = a[4 - 1 - 1] = a[2] = 5.
- When
i = 2,descendingOrder[2] = a[4 - 1 - 2] = a[1] = 4.
- When
i = 3,descendingOrder[3] = a[4 - 1 - 3] = a[0] = 1.
5.Printing Reversed Array: The reversed array is then printed to the console using System.out.println(Arrays.toString(descendingorder)).


Comments
Post a Comment