Insert and sort in ascending order

I am trying to insert 5 numbers at a time to create an array in ascending order, but I have a problem with my third number and so on. The test should insert: The number is less than the first element. The number is greater than the last element. The number between the first and last elements. The number is already in the array. Here is my code:

import java.util.Scanner;
public class InsertInOrder {

    public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    int [] a=new int[5];
    int numberofelements=0;
    System.out.print("Number to insert: ");
    int numtoinsert=input.nextInt();
    if (numtoinsert!=0)
    {
        a[0]=numtoinsert;
        ++numberofelements;
        System.out.print("Array is now: ");
        System.out.println(a[0]);
    }
    while(numberofelements<a.length)
    {
        System.out.print("Number to insert: ");
        numtoinsert=input.nextInt();
       if ((numtoinsert<a[numberofelements]) || (numtoinsert==a[numberofelements]))
           {
              for(int i=0;i<numberofelements;i++)
              {
                  a[i]=a[i+1];
              }
           }
    else
    a[numberofelements]=numtoinsert;
    numberofelements++;
    System.out.print("Array is now: ");
    for(int i=0;i<numberofelements;i++)
        {
            System.out.print(a[i]+" ");
        }
      System.out.println();
    }
    System.out.print("\nArray is now full");
    }

}

My code output:

Number to insert: 5
Array is now: 5
Number to insert: 2
Array is now: 5 2 
Number to insert: 7
Array is now: 5 2 7 
Number to insert: 4
Array is now: 5 2 7 4 
Number to insert: 5
Array is now: 5 2 7 4 5 

Array is now full
+4
source share
2 answers

This line:

if ((numtoinsert<a[numberofelements]) || (numtoinsert==a[numberofelements]))

Checks for more or less in your code.

However, numberofelementsa constant in each particular iteration.

Repeat the loop again and iterate through all the current elements of the array.

Good luck

EDIT

, , , , , , . , , .

for:

for(int i=0;i<numberofelements;i++)
          {
              a[i]=a[i+1];
          }

, .

.

, . A .

+1

.

 if ((numtoinsert<a[numberofelements]) || (numtoinsert==a[numberofelements]))
 {
     for(int i=0;i<numberofelements;i++)
     {
          a[i]=a[i+1];
     }
 }

, , ;) [5; 5] [5; 5] ;)

:

  • , - .

:

(A) (B). B . .

:

if (numtoinsert < a[numberofelements-1]) { //If the PREVIOUS value is bigger
    int i = numberofelements; //start at the current index
    //then read from right to left 
    //   while not at the beginning (i > 0) 
    //   AND the value is smaller than the value at the current position.
    while(i > 0 && numtoinsert < a[i-1]) { 
        a[i] = a[i-1]; //Set the left value to the right.
        i--; //Decrement the index
    }
    a[i] = numtoinsert; //Then add the new value to the 
}

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] a = new int[5];
        int numberofelements = 0;
        System.out.print("Number to insert: ");
        int numtoinsert = input.nextInt();
        if (numtoinsert != 0) {
            a[0] = numtoinsert;
            ++numberofelements;
            System.out.print("Array is now: ");
            System.out.println(a[0]);
        }
        while (numberofelements < a.length) {
            System.out.print("Number to insert: ");
            numtoinsert = input.nextInt();
            if (numtoinsert <= a[numberofelements-1]) {
                int i = numberofelements;
                while(i > 0 && numtoinsert < a[i-1]) {
                    a[i] = a[i-1];
                     i--;
                }
                a[i] = numtoinsert;
            } else {
                a[numberofelements] = numtoinsert;
            }
            numberofelements++;
            System.out.print("Array is now: ");
            for (int i = 0; i < numberofelements; i++) {
                System.out.print(a[i] + " ");
            }
            System.out.println();
        }
        System.out.print("\nArray is now full");
    }
0

Source: https://habr.com/ru/post/1660150/


All Articles