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

Comments

Popular posts from this blog

Sort Boundary Elements Of A Matrix

Lucky Number In Java ISC

Program to fill a square matrix of size ‘n*n” in a circular fashion (clockwise) with natural numbers from 1 to n*n, taking ‘n’ as input.