write a program to input and store
the weigth of ten people. Sort & display
them is descending order by using Bubble
sort
Answers
Answer:
import java.util.*;
public class Main
{
static void bubble_sort(int[] weights){
int n = weights.length;
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if(weights[i] > weights[j]){
int temp = weights[i];
weights[i] = weights[j];
weights[j] = temp;
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[10];
System.out.println("Enter weights of all 10 students-");
for(int i = 0; i < arr.length; i++){
arr[i] = sc.nextInt();
}
System.out.println("BEFORE SORTING");
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
bubble_sort(arr);
System.out.println("\nAFTER SORTING");
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
}
}
Explanation: