Computer Science, asked by yoyosuraj, 1 year ago

write a java program to store salary of 10 persons and display the salary in descending order using sorting method.

Answers

Answered by QGP
4
import java.util.*;
public class Salary
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        String[] names = new String[10];
        int[] sal = new int[10];

        for(int i=0;i<10;i++)
        {
            System.out.print("Enter Name of Person "+(i+1)+" : ");
            names[i] = sc.next();

            System.out.print("Enter Salary of "+names[i]+" : ");
            sal[i] = sc.nextInt();
            
            System.out.println();
        }
        
        int n = sal.length;  
        int temp = 0;  
        String ts = "";
        for(int i=0; i < n; i++)
        {  
            for(int j=1; j < (n-i); j++)
            {  
                if(sal[j-1] > sal[j])
                {    
                    temp = sal[j-1];  
                    sal[j-1] = sal[j];  
                    sal[j] = temp;
                    
                    ts = names[j-1];
                    names[j-1] = names[j];
                    names[j] = ts;
                }  

            }  
        }  
        System.out.println("\n\nSalaries in Decreasing Order are: \n\n");
        for(int i=9;i>=0;i--)
        {
            System.out.println(names[i]+" has a salary of "+sal[i]);
        }

    }

}

QGP: Hope it helps.
QGP: Bubble Sort is used as the sorting algorithm
yoyosuraj: can you please send me the varible description
QGP: Oh okay. names[] is the array containing the list of names of persons.
QGP: sal[] is the array containing salaries of respective persons
QGP: sc is the Scanner Object. Used for input purpose
QGP: i and j are loop variables. Used various times for different purposes
QGP: temp is a temporary integer variable, used for swapping sal[] elements while applying Bubble Sort. ts has the same purpose, but is used for swapping names in names[]
QGP: I think that's it.
yoyosuraj: thanks
Similar questions