fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String entry = "kisakye";
  13. System.out.println("Before sorting: "+ entry);
  14. String sorted = InsertionSort(entry);
  15. System.out.println("Insertion sort: "+ sorted);
  16.  
  17.  
  18. }
  19.  
  20. public static String InsertionSort(String word){
  21. char [] a = word.toCharArray(); // tranforms the string in to character array
  22. int n = a.length; //length of the character array
  23. for(int i=1; i<n; i++){ // checks for all the items in the array
  24. int j = i-1; //position of char below the char
  25. char cur = a[i];// current char
  26. while((j>=0) && (a[j]>cur))
  27. a[j+1] = a[j--]; // swap the chars
  28. a[j+1] = cur; //do not swap the chars
  29.  
  30. }
  31.  
  32. return new String(a);
  33. }
  34. }
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
Before sorting: kisakye
Insertion sort: aeikksy