Assigning multiple values to multiple variables in a single line

semore_1267

I'm trying to write the Java equivalent of the following Python Inversion-Sort algorithm:

import numpy as np

def main(items):

    for i in range(1, len(items)):
        j = i
        while j > 0 and items[j] < items[j-1]:
            items[j], items[j-1] = items[j-1], items[j]
            j -= 1

    print(items)

main(np.array([4, 78, 23, 24, 56, 7, 9]))

This is the Java version:

import java.util.Arrays;

public class Sorters {

    public static void main(String args[]) {

        Sorters sort = new Sorters();
        int[] items = {4, 78, 23, 24, 56, 7, 9};
        sort.insertionSort(items);
    }

    public void insertionSort(int[] items) {

        for(int i=1 ; i<items.length ; i++) {
            int j = i;
            while(j>0 && items[j] < items[j-1]) {
                items[j] = items[j-1]; // These two lines are
                items[j-1] = items[j]; // causing the error
                j -=1;
            }
        }
    System.out.println("Sorted array: " + Arrays.toString(items));
    } 
}

I've narrowed the issue down to the two lines that are commented as such, above (in the Java method).

If I give the Python function this array: [4, 78, 23, 24, 56, 7, 9] (for example), everything works fine. However, if I give the same array to the Java method, I get this in return: [4, 78, 78, 78, 78, 78, 78].

Could someone tell me how to write the Java equivalent of Python's items[j], items[j-1] = items[j-1], items[j]? Explanations welcomed. Thanks.

MaxG

That's because you need to use a temp variable to store one of the values when you swap between the items[j] with items[j-1]. It should be something like that:

int temp = items[j];
items[j] = items[j-1];
items[j-1] = temp;

What happens is that you lose the original value so each iteration of the loop you get to copy into items[j] the value of items[j-1].

That's how you got your output.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Assigning multiple values to multiple variables in a single line

From Dev

Assigning multiple column values in a single row of pandas DataFrame, in one line

From Dev

Assigning multiple values in a single statement

From Dev

Assigning multiple variables to multiple values at once R?

From Dev

Assigning multiple values to a single column in MySQL

From Dev

Displaying multiple variables on single line?

From Dev

Comparing multiple values and assigning associated variables

From Dev

assigning categoical values to multiple variables in r

From Dev

Assigning values to multiple declarations on same line

From Dev

Assigning multiple values on one line in Ruby using ||=

From Dev

Assigning a single value to multiple AngularJs $scope variables same as in regular JavaScript?

From Dev

Assigning values to multiple names

From Java

Dockerfile: Setting multiple environment variables in single line

From Dev

PHP; Manipulation of Multiple Variables on Single Line?

From Dev

Assigning multiple values from a single input statement disregarding whitespace

From Dev

Extracting multiple values from variable and assigning new variables

From Dev

Iterating Through Multiple PHP Variables, Assigning Values To Each For SQL Statement

From Dev

Trouble assigning multiple nodejs exports return values to variables

From Dev

Assign a single value to multiple variables in one line in Rust?

From Dev

Aligning (formatting) output in C++ // Multiple variables in a single line

From Dev

how to write multiple variables to a single LCD display line?

From Dev

Declaring multiple variables in single line + Angular 2 & TypeScript

From Dev

Aligning (formatting) output in C++ // Multiple variables in a single line

From Dev

In python what is difference between assign a variable in single line and assigning a variable in multiple lines

From Dev

Line chart for multiple Variables

From Dev

Passing multiple values to a Perl program in a single command line argument

From Dev

How do I input multiple values from a single line?

From Dev

Multiple values for single key

From Dev

Multiple values for single key

Related Related

  1. 1

    Assigning multiple values to multiple variables in a single line

  2. 2

    Assigning multiple column values in a single row of pandas DataFrame, in one line

  3. 3

    Assigning multiple values in a single statement

  4. 4

    Assigning multiple variables to multiple values at once R?

  5. 5

    Assigning multiple values to a single column in MySQL

  6. 6

    Displaying multiple variables on single line?

  7. 7

    Comparing multiple values and assigning associated variables

  8. 8

    assigning categoical values to multiple variables in r

  9. 9

    Assigning values to multiple declarations on same line

  10. 10

    Assigning multiple values on one line in Ruby using ||=

  11. 11

    Assigning a single value to multiple AngularJs $scope variables same as in regular JavaScript?

  12. 12

    Assigning values to multiple names

  13. 13

    Dockerfile: Setting multiple environment variables in single line

  14. 14

    PHP; Manipulation of Multiple Variables on Single Line?

  15. 15

    Assigning multiple values from a single input statement disregarding whitespace

  16. 16

    Extracting multiple values from variable and assigning new variables

  17. 17

    Iterating Through Multiple PHP Variables, Assigning Values To Each For SQL Statement

  18. 18

    Trouble assigning multiple nodejs exports return values to variables

  19. 19

    Assign a single value to multiple variables in one line in Rust?

  20. 20

    Aligning (formatting) output in C++ // Multiple variables in a single line

  21. 21

    how to write multiple variables to a single LCD display line?

  22. 22

    Declaring multiple variables in single line + Angular 2 & TypeScript

  23. 23

    Aligning (formatting) output in C++ // Multiple variables in a single line

  24. 24

    In python what is difference between assign a variable in single line and assigning a variable in multiple lines

  25. 25

    Line chart for multiple Variables

  26. 26

    Passing multiple values to a Perl program in a single command line argument

  27. 27

    How do I input multiple values from a single line?

  28. 28

    Multiple values for single key

  29. 29

    Multiple values for single key

HotTag

Archive