write a program in java to accept 50 students name and arrange them in ascending order by using bubble sort
Answers
Answered by
8
heya friend,
the program is given below:
import java.util.Scanner;
class Ascendingnames
{
public static void main(String[] args)
{
String temp;
Scanner s = new Scanner(System.in);
String names[] = new String[40];
System.out.println("Enter names");
Scanner s1 = new Scanner(System.in);
System.out.println("Enter all the names:");
for(int i = 0; i < 40; i++)
{
names[i] = s1.nextLine();
}
for (int i = 0; i < 40; i++)
{
for (int j = i + 1; j < 40; j++)
{
if (names[i].compareTo(names[j]))>0 )
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
System.out.print("Names in Ascending Order:");
for (int i = 0; i < 40; i++)
{
System.out.println(names[i] + ",");
}
}
}
the program is given below:
import java.util.Scanner;
class Ascendingnames
{
public static void main(String[] args)
{
String temp;
Scanner s = new Scanner(System.in);
String names[] = new String[40];
System.out.println("Enter names");
Scanner s1 = new Scanner(System.in);
System.out.println("Enter all the names:");
for(int i = 0; i < 40; i++)
{
names[i] = s1.nextLine();
}
for (int i = 0; i < 40; i++)
{
for (int j = i + 1; j < 40; j++)
{
if (names[i].compareTo(names[j]))>0 )
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
System.out.print("Names in Ascending Order:");
for (int i = 0; i < 40; i++)
{
System.out.println(names[i] + ",");
}
}
}
jedi1:
thx bro
Similar questions