## unable to sort java array in descending order in two steps : first from index 0 to 12, second from index 13 to 24

bubble sort

binary search

selection sort

merge sort

write statements to declare the array and assign the values to the corresponding positions.

sorting algorithms

quick sort

Sorry, it may be a dumb question, but I'm a total a beginner : When I try to execute the following program, it compiles, but I get the following error:

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at Numbers.main(Numbers.java:6)"

Can someone help?

public class Numbers { public static void main (String[] args) { int [] harry = new int [25]; for (int i = 0, n = 12; i < harry.length; i++, n--) { if (i % 2 > 0) { harry[n] = i; } } for (int j = 0, m = 25; j < harry.length; j++, m--) { if (j % 2 == 0) { harry[m] = j; } } for (int k = 0; k < harry.length; k++) { System.out.println(harry[k]); } } }

Besides having to initialize `m`

to `harry.length - 1`

(since 25 is out of bounds), you should only decrement `n`

and `m`

when you use them (i.e. when you assign a value to the `harry`

array):

int [] harry = new int [25]; for (int i = 0, n = 12; i < harry.length; i++) { if (i % 2 > 0) { harry[n] = i; n--; } } for (int j = 0, m = harry.length - 1; j < harry.length; j++) { if (j % 2 == 0) { harry[m] = j; m--; } }

Otherwise, `n`

will become negative before the first loop is done, and `m`

will become too small.

**Chapter 7: Arrays,** We need a way to declare many variables in one step and then be able to store and Like Strings, arrays use zero-based indexing, that is, array indexes start with 0. Write a program that declares a double array of length 4, prints the values, We could have combined the first two loops into one loop, but it is cleaner to do� Please refer to Array.sort article. Java Program to Sort Array in Descending Order using Temp Variable. This Java program allows the user to enter the size and Array elements. Next, it will sort the array element in descending order using For Loop.

You have this kind of runtime exception because you try to get harry[-1], but it's impossible. You will also see this exception in the second for, because you set m = 25, but indexces of your array are from 0 to 24. Set n=harry.length-1 and m = harry.length-1, then your program will work.

**Sorting,** We will end up with a sorted list in the second array. We immediately see two drawbacks to this sorting algorithm. Firstly it may be impossible to declare an array large enough on some systems. The first problem is solved by using linked lists, attached to each array index. We start with an array of 5 counters set to zero. To sort the contents of an ArrayList in descending order. Create an ArrayList. Sort the contents of the ArrayList using the sort() method of the Collections class.; Then, reverse array list using the reverse() method of the Collections class.

On the following line,

for (int j = 0, m = 25; j < harry.length; j++, m--) {

m = 25, it should be 25-1, otherwise it will throw index out of bound exception

**Implementing binary search of an array (article),** Let's see how to think about binary search on a sorted array. Yes If an element is at index 0 then it is the first element in the array. Looking through the numbers in order like this is a linear search. Did you see how many steps that took? We update the value of min to 12 + 1, or 13, and we leave max unchanged at 24. The previous tutorial was all about sorting an array in ascending order. In this post we are going to learn how to sort an array in Descending (Reverse) Order. Example. Here we have two arrays, one is integer array and another one is String array. We are sorting both the arrays in reverse order.

I am not sure if I understand everything but I believe if you do this your code should work perfectly:

Use `if (i % 2 > 0 && n >= 0)`

instead of `if (i % 2 > 0)`

and use `if (j % 2 == 0 && m<=24)`

instead of `if (j % 2 == 0)`

Hope this helps.

**Java Basic Programming Exercises,** Practice with solution of exercises on Java basic: examples on Write a Java program to divide two numbers and print on the screen. Go to Input second number: 24 Input the first number : 12 Input the second number: 13 Result: 13 Write a Java program to find the index of a value in a sorted array. In this java tutorial, we are sorting an array in ascending order using temporary variable and nested for loop. We are using Scanner class to get the input from user. Java Example: Program to Sort an Array in Ascending Order. In this program, user is asked to enter the number of elements that he wish to enter.

**Sort all even numbers in ascending order and then sort all odd ,** Once the array is partitioned, sort left and right parts individually. This step C++ program sort array in even and odd manner. To do two way sort. First sort order. void twoWaySort( int arr[], int n). {. // Current indexes from left and right Find first odd number from left side. while (arr[l] % 2 != 0) {. l++;. k++;. }. I have a homework assignment to sort an array in ascending order. Obviously, this is to be done manually without using any kind of sort() function.. I figured to do it, I would need two for loops: the first one will loop through the existing array and create a temporary value with the value and index of the array.

**ArrayList in java with example programs,** Arraylist class implements List interface and it is based on an Array data structure . so the method updated the element present at the index 0 (which is the first We are then adding two more elements in the ArrayList using method add(int index, In the following example we have sorted a list of String type alphabetically,� sort() method is a java.util.Arrays class method.. Syntax: public static void sort(int[] arr, int from_Index, int to_Index) arr - the array to be sorted from_Index - the index of the first element, inclusive, to be sorted to_Index - the index of the last element, exclusive, to be sorted This method doesn't return any value.

**Arrays,** Making an array in a Java program involves three distinct steps: We always refer to the first element of an array a[] as a[0], the second as a[1], the second value as a[2], and so forth, but starting the indexing with 0 has 4 9 2 11 18 25 2 9 3 5 7 10 12 19 21 3 8 1 6 4 6 13 20 22 23 5 7 14 16 17 24 1 8 15� Minimum number of swaps required to sort an array of first N number; Find XOR of all elements in an Array; Efficiently merging two sorted arrays with O(1) extra space and O(NlogN + MlogM) Count of subsets with sum equal to X using Recursion; Split the given string into Primes : Digit DP; Number of pairs in an array with the sum greater than 0

##### Comments

- I'm not sure what you're trying to do, but changing
`m = 25`

to`m = harry.length-1`

will fix at least the first issue with your code.