SHARE
TWEET

bb

a guest Jul 21st, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2. public class BubbleSort {
  3.   public static void main(String[] args) {
  4.     Scanner in = new Scanner(System.in);
  5.     System.out.println("Enter array length");
  6.     double average;
  7.     int i, m, m1, med2, temp = 0;
  8.     int x = in .nextInt();
  9.     int arr[] = new int[x];
  10.     for (i = 0; i < x; i++)
  11.       arr[i] = in .nextInt();
  12.    
  13.     System.out.println("Before Sorting");
  14.     for (i = 0; i < x; i++) {
  15.       System.out.print(arr[i] + " ");
  16.     }
  17.     System.out.println();
  18.    
  19.    
  20.     for (i = 0; i < x; i++) {
  21.       for (int j = 1; j < (x - i); j++) {
  22.         if (arr[j - 1] > arr[j]) {
  23.           temp = arr[j - 1];
  24.           arr[j - 1] = arr[j];
  25.           arr[j] = temp;
  26.         }
  27.       }
  28.     }
  29.    
  30.     System.out.println("After Sorting");
  31.     for (i = 0; i < x; i++)
  32.       System.out.print(arr[i] + " ");
  33.    
  34.     System.out.println();
  35.    
  36.     System.out.print("The average is: ");
  37.     if (x < 3) {
  38.       if (x == 2) {
  39.         System.out.println(((arr[0] + arr[1]) / 2));
  40.       } else
  41.         System.out.println(arr[0]);
  42.     } else if (x % 2 == 0) {
  43.       m1 = (x / 2) - 1;
  44.       med2 = (x / 2);
  45.       average = (arr[m1] + arr[med2 + 1] + arr[m1 - 1] + arr[med2]) / 4.0;
  46.       System.out.print(average);
  47.     } else { m = ((x + 1) / 2) - 1;
  48.       average = (arr[m] + arr[m + 1] + arr[m - 1]) / 3.0;
  49.       System.out.print(average);
  50.      
  51.     }
  52.    
  53.     System.out.println();
  54.   }
  55. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top