0% found this document useful (0 votes)
12 views1 page

Bubble Sort for Name Arrangement

The document contains a Java program that inputs five names into an array and sorts them in ascending order using the bubble sort technique. It prompts the user to enter the names and then displays the sorted list. The program utilizes the compareToIgnoreCase method for case-insensitive sorting.

Uploaded by

sanjay
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

Bubble Sort for Name Arrangement

The document contains a Java program that inputs five names into an array and sorts them in ascending order using the bubble sort technique. It prompts the user to enter the names and then displays the sorted list. The program utilizes the compareToIgnoreCase method for case-insensitive sorting.

Uploaded by

sanjay
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

/**

* Write a program to input ten names in an array. Arrange these names in ascending
order of letters, using the bubble sort technique.
Sample Input:
Rohit, Devesh, Indrani, Shivangi, Himanshu, Rishi, Piyush, Deepak, Abhishek,
Kunal, …..
Sample Output:
Abhishek, Deepak, Devesh, Himanshu, Indrani, Kunal, Piyush, Rishi, Rohit,
Shivangi, ….
*/
import [Link];

public class ArrangeNames


{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
String names[] = new String[5];
[Link]("Enter 5 names:");
for (int i = 0; i < [Link]; i++)
{
names[i] = [Link]();
}

//Bubble Sort
for (int i = 0; i < [Link] - 1; i++)
{
for (int j = 0; j < [Link] - 1 - i; j++)
{
if (names[j].compareToIgnoreCase(names[j + 1]) > 0)
{
String temp = names[j + 1];
names[j + 1] = names[j];
names[j] = temp;
}
}
}

[Link]("\nSorted Names");
for (int i = 0; i < [Link]; i++)
{
[Link](names[i]);
}
}
}

Common questions

Powered by AI

The bubble sort technique arranges an array of strings in alphabetical order by repeatedly stepping through the list, comparing each pair of adjacent items, and swapping them if they are in the wrong order, which means the current string should come after the next string lexicographically. This is done until no swaps are needed, indicating that the list is sorted. In the given program, this process is repeated for (n-1) passes, where n is the number of strings, with fewer comparisons as the larger strings 'bubble' to their correct positions at the end of the array .

You might also like