Bubble Sort (String)

import java.io.*;
class BubbleSort_Strings
{
public static void main() throws IOException
{
InputStreamReader x= new InputStreamReader(System.in);
BufferedReader y= new BufferedReader(x);

String ARR[]=new String[10];
int i,j;
String temp=””;

System.out.println(“Enter 10 Strings/Words in an array”);
for(i=0;i<10;i++)
{
ARR[i]=y.readLine();
}

for(i=0;i<10-1;i++)
{
for(j=0;j<10-i-1;j++)
{
if(ARR[j].compareTo(ARR[j+1])>0)
{
temp=ARR[j];
ARR[j]=ARR[j+1];
ARR[j+1]=temp;
}
}
}

System.out.println(“The sorted string array is :”);
for(i=0;i<10;i++)
{
System.out.println(ARR[i]+” “);
}
}
}

Leave a comment