Java Program on Bubble Sort
- Categories Java, Arrays in Java, Arrays
Write a program to input twenty names in an array. Arrange these names in descending order of alphabets using the bubble sort technique.
import java.util.Scanner; public class ArrangeNames { public static void main(String []args) { String a [] =new String [20]; Scanner scan=new Scanner(System.in); System.out.println("Please enter the twenty names"); for (int i = 0; i <= 19; i++) { a [i]= scan.next(); } String t; for (int i=0; i<19; i++ ) { for (int j = 0; j < 20 -i-1; j++) { if (a[j].compareTo (a[j+ 1]) >0) { t = a[j] ; a[j] =a[j+1]; a[j+1]= t; } } } System.out.println("Sorted element are:"); for (int k=19; k> 0; k--) { System.out.println(a[k]); } } }
You may also like
Single Linked List Programs in Java
28 August, 2021
Implementing Stack using Array in Java
28 August, 2021
Constructor Programs
3 July, 2021