I get the default value 0 when I try to insert the value into an array after sorting I

I have an integer array of size 4. I add elements to it using the add method. It is like an unsorted array. I sort it using the sorting method shown in the code below. The sort method puts the smallest number at position a [0]. When I try to add items after calling the sort method, I always get a return value of 0. Is there any way around this?

  import java.util.Arrays;

    public class Scrap {
    private static int[] array = new int[4];
    private static int i = 0;

    public static void main(String[] args) {
        Scrap pq = new Scrap();
        pq.add(4);
        pq.insert(3);
        pq.add(5);

        pq.sort();// smallest to largest sort method.
        // System.out.println(array[0]);
        pq.insert(1);
        pq.sort();
        int test = pq.Minimum();
        System.out.println("The smallest element of the array is " + test);
        pq.sort();
    }

    //
    public void add(int input) {
        insert(input);
    }

    // Method to insert number into the array.
    public void insert(int input) {
        array[i] = input;
        i++;
    }

    // Finding smallest number of the array.
    public int Minimum() {
        int a = array[0];
        return a;
    }

    // Sorts the array from smallest to largest integer
    public void sort() {
        int first, temp;
        for (int i = array.length - 1; i > 0; i--) {
            first = 0;
            for (int j = 1; j <= 1; j++) {
                if (array[j] > array[first])
                    first = j;
            }
            temp = array[first];
            array[first] = array[i];
            array[i] = temp;
        }

    }

    public int remove() {
        return delete();
    }

    public int delete() {
        return remove();
    }
    // Method to convert the array into a string for output
}
+4
source share
3 answers

The problem in a nutshell:

  • You start with an array of length 4.
    • At this moment, the array contains 4 zeros, that is: [0, 0, 0, 0]
  • You add 4, 3, and 5. These operations update the contents of the array to [4, 3, 5, 0].
  • . [0, 3, 4, 5]. [0, 5, 3, 4], , sort .
    • , 0. , 3 . (, , sort.)
  • , 1, 3, [0, 5, 3, 1].

, , size, sort [3, 4, 5, 0], 1 [3, 4, 5, 1]. , [1, 3, 4, 5], 1, , 0.

:

  • , private static int i = 0; private int size = 0;. i . size . static, .
  • sort. , . , array.size, size. ? size - Scrap, add insert.

:

  • add insert add.
  • remove delete. , , , ( )

.

Scrap pq :

[0, 0, 0, 0]

:

pq.add(4);
pq.insert(3);
pq.add(5);

:

[4, 3, 5, 0]

.

:

pq.sort();

:

[0, 5, 3, 4]

. , . . :

pq.insert(1);

:

[0, 5, 3, 1]

, , , . , . , .

+4

, sort ( , Arrays.sort). .

0s. 3 int, , :

0,3,4,5

, i . 3. , 1,

0,3,4,1

,

0,1,3,4

, , 0

+1

I don't think this is the most efficient way to sort an array. This can be done in just 1 cycle. Try sorting the array from smallest to largest.

int temp;
for(int i=0;i<array.length-1;i++){
if(array[i]>array[i+1]){
    temp=array[i];
    array[i]=array[i+1];
    array[i+1]=temp;
    i=-1;
}
    }
+1
source

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


All Articles