2

This is what I've done so far; I was able to calculate the average, but I'm not sure how to find the median. I also know that I need to sort the array to make it easier.

 public class SAA {
     public static void main(String[] args) {
         int[] num = {60, 70, 82, 1216, 57, 82, 34, 560, 91, 86};
         int total = 0;
         for (int i = 0; i < num.length; i++) {
             if ((num[i] > 0) && (num[i] < 100)) {
                 total += num[i];
             }
         }
         System.out.println(total / 10);
     }
 }

This is my attempt at using bubble sort:

public class Bubblesort {
    public static void main(String[] args) {
        int[] num = {60, 70, 82, 1216, 57, 82, 34, 560, 91, 86};
        int temp = 0;
        int[] add = new int[num.length + 1];
        for (int i = 0; i < num.length; i++) {
            for (int j = i + 1; j < num.length; j++) {
                if (num[i] > num[j]) {
                    temp = num[i];
                    num[i] = num[j];
                    num[j] = temp;
                }
            }
        }
        for (int i = 0; i < num.length; i++) {
            System.out.print(num[i] + " ");
        }
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Lucy
  • 29
  • 8
  • I'm a beginner so I just need to know how to do it using bubble sort and a basic formula. I can't use the median function in Java. I've formulated a plan but I'm not fully sure how to do it; Sort the numbers from smallest to largest, write the total number of indices and make a for loop that goes through the code and looks for the index thats in the center – Lucy Apr 01 '21 at 15:00
  • Did you not search the Internet for ___java bubble sort___ ? – Abra Apr 01 '21 at 15:02
  • Why would you need a loop for the index at the center? You know the array's length so you can calculate that (and if there is no exact center you'd need to either choose or calculate the average of the 2 center indices). – Thomas Apr 01 '21 at 15:03
  • "I just need to know how to do it using bubble sort" - I'd say if you "just" need to know (i.e. use) something then `Arrays.sort()` would be the better option. I assume you _need_ to use bubble sort, do you? – Thomas Apr 01 '21 at 15:04
  • 1
    This is clearly a homework assignment and the OP hasn't even attempted to write the bubble sort yet. Start by writing your sort. Once the array is sorted, it's easy to find the middle value. – Phaelax z Apr 01 '21 at 15:07
  • No, I don't need to use bubble sort. I'm just not sure how to create a correct formula with my code to find the median. For the bubble sort part of it, I've already attempted it, I'm not sure if it's necessary to find the median, but if it isn't then I just want to know how to find the median without it. – Lucy Apr 01 '21 at 15:07
  • Sort your array. Then look up index value at array.length/2, that will contain your median value. If you have an even number of elements then it's an average of the two middle numbers. Also, your code above is not accurate for calculating the average. – Phaelax z Apr 01 '21 at 15:18
  • Alright thank you! I'll try that. – Lucy Apr 01 '21 at 15:26

2 Answers2

2

Median is the middle element of the sorted array if the array size is odd, else it is the average of the middle two elements. The following code snippet finds the median of the array.

public static void main(String[] args) {
        int[]num = {60, 70, 82, 1216, 57, 82, 34, 560, 91, 86};
        int median;
        int len = num.length;
        
        Arrays.sort(num); // sorts the array
        
        //check if the length is odd
        if(len%2 != 0)
            median = num[len/2];
        else // length is even
            median = (num[(len - 1) / 2] + num[len / 2])/2;
        System.out.println(median);
    }
Sr_33
  • 56
  • 1
  • 7
2
        int min = 0; 
        int max = 0; 
        
        for (int i = 0; i < numbers.length; i++)
        {
            for (int j = 0; j < numbers.length-1; j++)
            {
                min = Math.min(numbers[j], numbers[j+1]); //using min and max methods
                max = Math.max(numbers[j], numbers[j+1]); 
                numbers[j] = min; //placing the smaller value on the left
                numbers[j+1] = max; //placing the larger value on the right
            }
        }

This is the program to bubble sort an array in a short and simpler way, you should use this along with @Sr_33's answer as well!

TheSj
  • 376
  • 1
  • 11