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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s