icse-promo

Question

Write a program to input twenty names in an array. Arrange these names in descending order of alphabets , using the bubble sort technique. 

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;

public class program8
{
    public static void main(String[] args) 
    {
        int i=0,j=0;
        String temp="";
        Scanner sc = new Scanner(System.in);
        String[] names = new String[20];

        /* Input names */
        System.out.println("Enter 20 names");
        for (i = 0; i < 20; i++) 
        {
            names[i] = sc.nextLine();
        }

        /* Sort names using bubble sort technique */
        for (i = 0; i < names.length-1; i++)
        {
            for (j = 0; j < (names.length - i-1); j++)
            {
                if (names[j].compareTo(names[j+1]) < 0)
                {
                    temp = names[j];
                    names[j] = names[j+1];
                    names[j+1] = temp;
                }
            }
        }

        /* Print  sorted names */    
        System.out.println("Sorted names: ");
        for (i = 0; i < names.length; i++)
        {
            System.out.print(names[i]+"   ");
        }
    }
}


				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *