Insertion sort technique upon strings In Java ISC
Program to perform Insertion sort technique upon strings.
class Insertstring //start of class
{
public void accept (String str[]) //start of method
{
String key=" ";int j=0;
int n=str.length;
for( int i=1;i<n;i++) {
key= str [i];
j=i-1;
while ( j>=0 && key.compareTo(str[j])<0){
str[j+1]=str[j];
j--;
}
str[j+1]=key;
}
System.out.println("The output in ascending order");
for (int i=0;i<n;i++)
{
System.out.println(str[i]);
}
}//end of method
}//end of class
OUTPUT---
The output in ascending order
ANIK
IS
LOVE
class Insertstring //start of class
{
public void accept (String str[]) //start of method
{
String key=" ";int j=0;
int n=str.length;
for( int i=1;i<n;i++) {
key= str [i];
j=i-1;
while ( j>=0 && key.compareTo(str[j])<0){
str[j+1]=str[j];
j--;
}
str[j+1]=key;
}
System.out.println("The output in ascending order");
for (int i=0;i<n;i++)
{
System.out.println(str[i]);
}
}//end of method
}//end of class
OUTPUT---
The output in ascending order
ANIK
IS
LOVE
Comments
Post a Comment