» » Sorting string without using string Methods?


  1. package com.Instanceofjava; 
  2. public class SortString {
  3.  
  4. public static void main(String[] args) {
  5.  
  6. String original = "edcba";
  7. int j=0;
  8. char temp=0;
  9.  
  10.   char[] chars = original.toCharArray();
  11.  
  12.   for (int i = 0; i <chars.length; i++) {
  13.  
  14.       for ( j = 0; j < chars.length; j++) {
  15.  
  16.        if(chars[j]>chars[i]){
  17.             temp=chars[i];
  18.            chars[i]=chars[j];
  19.            chars[j]=temp;
  20.        }
  21.  
  22.    }  
  23.  
  24. }
  25.  
  26. for(int k=0;k<chars.length;k++){
  27. System.out.println(chars[k]);
  28. }
  29.  
  30. }
  31.  
  32. }





Output:
  1. abcde


Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu