// Method-I: (Using our own algorithm)
// Mehod-II: (Using our own algorithm)
- public class SortStringArrayByOwnAlgorithm {
- public static void main(String[] args) {
- String s[] = { "Java", "C++", "Python", "ruby", "Perl", "ishaan" };
- String temp;
- for (int i = 0; i < s.length; i++) {
- for (int j = 0; j < s.length-1; j++) {
- if (s[j].compareTo(s[j+1]) > 0) {
- temp = s[j];
- s[j] = s[j+1];
- s[j+1] = temp;
- }
- }
- }
- for (String str : s) {
- System.out.print(str + " ");
- }
- }
- }
// Mehod-II: (Using our own algorithm)
- public class SortStringArrayByOwnAlgorithm {
- public static void main(String[] args) {
- String s[] = { "Java", "C++", "Python", "ruby", "Perl", "ishaan" };
- String temp;
- for (int i = 0; i < s.length; i++) {
- for (int j = i + 1; j < s.length; j++) {
- if (s[i].compareTo(s[j]) < 0) {
- temp = s[i];
- s[i] = s[j];
- s[j] = temp;
- }
- }
- }
- for (String str : s) {
- System.out.print(str + " ");
- }
- }
- }
Output: ruby ishaan Python Perl Java C++
// Mehod-III: (Using pre-defined API)
- public class SortStringArrayAlgorithmExe {
- public static void main(String[] args) {
- String s[] = { "Java", "C++", "Python", "ruby", "Perl", "ishaan" };
- Arrays.sort(s);
- for (String str : s) {
- System.out.print(str + " ");
- }
- }
- }
Output: C++ Java Perl Python ishaan ruby
No comments:
Post a Comment