This site requires JavaScript, please enable it in your browser!
Greenfoot back
jacquelinereilly
jacquelinereilly wrote ...

2017/12/3

How to call this method

jacquelinereilly jacquelinereilly

2017/12/3

#
this is something I got online, and also used my own code, but I am having some trouble calling the method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package bubblesorting;
 
/**
 *
 * @author Jacqueline Reilly
 */
public class BubbleSorting {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
 
        // Source code starts here
        //Declaring one dimensional array
        int[] list = new int[6];
 
        //Loop that generates random numbers in the range 1 - 100
        for (int i = 0; i < list.length; i++) {
            list[i] = (int) (Math.random() * 100 + 1);
        }
 
        //Printing a display statement
        System.out.println("Order before sorting: ");
 
        //Displaying the numbers before sorting
        //The loop will run 6 times, since that is the length of the array
        for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + "  ");
        }
 
        System.out.println(" ");
 
        //Calling the bubble sort method. Calling array to sort values in array
        //It is going to sort the values in the array called list
        bubbleSort(list);
 
        //Printing display statements
        System.out.println(" ");
 
        System.out.println("Order after sorting: ");
 
        //Displaying the numbers after the sorting
        //The loop will run 6 times, since that is the length of the array
        for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + "  ");
        }
    }
 
    //Method for the bubble sort algorithm
    public int[] bubbleSort(int[] list) {
         
        // Variable declarations
        int i, x, swap;
        //i is loop control variable for outer loop
        //x is loop control variable for inner loop
        //swap is for the swaps
        for (i = 0; i < list.length - 1; i++) { //the outer loop will run from 0 to the lenght of list -1
            for (x = 0; x < list.length - 1 - 1; x++) { //inner loop will from 0 to length of list - 1 - i
                //i is the number of items that are already sorted (the number of complete iterations)
                if (list[x] > list[x + 1]) { //Comparing the element on left to the element to the right of it.
                    //if it is greater than item on the right, the swap happens.
                    //Swapping elements
                    swap = list[x]; //the swap equals that element it is swapping
                    list[x] = list[x + 1]; //that element is now going to equal that element +1 so it will look at the element that is right next to it
                    list[x + 1] = swap; //this is swapping the element to the right
                }
 
            }
        }
        return list; //Returning sorted list of numbers
    }
 
}
jacquelinereilly jacquelinereilly

2017/12/3

#
line 36 is the error when calling the method
danpost danpost

2017/12/3

#
jacquelinereilly wrote...
line 36 is the error when calling the method
What is the error message?
jacquelinereilly jacquelinereilly

2017/12/3

#
non-static method bubbleSort(int) cannot be referenced from a static context
Super_Hippo Super_Hippo

2017/12/3

#
You can either make the method static or you have to create a BubbleSorting object and call the method on it.
jacquelinereilly jacquelinereilly

2017/12/3

#
changing the method to static worked - Not sure why I never thought of that, Thank you! Just another thing - it does not do it properly. It is supposed to use the bubble sort to sort numbers from least to greatest, or greatest to least , either or, but when I run it, it only displays a list of random numbers. Than 0's for other parts. Not sure if it is something to do with the random number generator. I changed the code to this due to assignment requirements. Ignore the previous one :) thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package bubblesorting;
 
//Import packages
import java.util.Random;
 
/**
 *
 * @author Jacqueline Reilly
 */
public class BubbleSorting {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
 
        // Source code starts here
        //Declaring one dimensional array
        int[] list = new int[6];
 
        //Loop that generates random numbers in the range 1 - 100
        for (int i = 0; i < list.length; i++) {
            System.out.print(getRandomNum() + "  ");
             
        }
 
        System.out.println(" ");
 
        //Printing a display statement
        System.out.println("Order before sorting: ");
 
        //Displaying the numbers before sorting
        //The loop will run 6 times, since that is the length of the array
        for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + "  ");
        }
 
        System.out.println(" ");
 
        //Calling the bubble sort method. Calling array to sort values in array
        //It is going to sort the values in the array called list
        bubbleSort(list);
 
        //Printing display statements
        System.out.println(" ");
 
        System.out.println("Order after sorting: ");
 
        //Displaying the numbers after the sorting
        //The loop will run 6 times, since that is the length of the array
        for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + "  ");
        }
    }
 
    //Method for random number generator
    public static int getRandomNum() {
        Random rand = new Random();
        return rand.nextInt(100) + 1;
    }
 
    //Method for the bubble sort algorithm
    public static int[] bubbleSort(int[] list) {
 
        // Variable declarations
        int i, x, swap;
        //i is loop control variable for outer loop
        //x is loop control variable for inner loop
        //swap is for the swaps
        for (i = 0; i < list.length - 1; i++) { //the outer loop will run from 0 to the lenght of list -1
            for (x = 0; x < list.length - 1 - 1; x++) { //inner loop will from 0 to length of list - 1 - i
                //i is the number of items that are already sorted (the number of complete iterations)
                if (list[x] > list[x + 1]) { //Comparing the element on left to the element to the right of it.
                    //if it is greater than item on the right, the swap happens.
                    //Swapping elements
                    swap = list[x]; //the swap equals that element it is swapping
                    list[x] = list[x + 1]; //that element is now going to equal that element +1 so it will look at the element that is right next to it
                    list[x + 1] = swap; //this is swapping the element to the right
                }
 
            }
        }
        return list; //Returning sorted list of numbers
    }
 
}
danpost danpost

2017/12/3

#
The array returned by 'bubbleSort' goes to never-never land. Change line 42 to the following:
1
list = bubbleSort(list);
jacquelinereilly jacquelinereilly

2017/12/3

#
Thank you, that worked. The only problem now is that it only sorts them sometimes. It does least to greatest only sometimes when I run it. I am not sure if it something to do with the number generator
danpost danpost

2017/12/3

#
Your sorting algorithm is flawed. You need to swap toward the starting point -- not away from it (or, the loops need to iterate in opposite directions). So, start with the second element (iterating to the last) and swap toward the front if less than the one before it. As you have it now, the next value may be greater, but the value after it may be less (meaning, some disorder still remains).
jacquelinereilly jacquelinereilly

2017/12/4

#
Okay. so should I change line 77 to:
1
list[x] = list[x + 1 | x - 1];
I think that may be wrong
danpost danpost

2017/12/4

#
jacquelinereilly wrote...
Okay. so should I change line 77
Need to change more than that.
1
2
3
4
5
6
7
8
9
for (int i=1; i<list.length; i++)
{
    for (int j=i-1; j>= 0 && list[j] > list[j+1]; j--)
    {
        int swap = list[j];
        list[j] = list[j+1];
        list[j+1] = swap;
    }
}
jacquelinereilly jacquelinereilly

2017/12/4

#
Thank you!
You need to login to post a reply.