Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7013893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:26:59+00:00 2026-05-27T22:26:59+00:00

I tried sorting strings using bubblesort, but I dont know whether it makes any

  • 0

I tried sorting strings using bubblesort, but I dont know whether it makes any sense but can someone just help me figure out how to sort strings and let me know if my logic is correct? i have converted the strings to character just to check if they are in alphabetical order..eg app ban acc will be sorted to acc app and ban..can anyone give the logic to this problem.

import java.io.*;
import java.util.*;
class sort
{
    public static void main(String args[])throws IOException
    {

        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


        System.out.println("enter the strings");
        String str=br.readLine();
        StringTokenizer st=new StringTokenizer(str,",");
        String s1=st.nextToken();
        String s2=st.nextToken();
        String s3=st.nextToken();
        char ch1[]=s1.toCharArray();
        char ch2[]=s2.toCharArray();
        char ch3[]=s3.toCharArray();

        if ((ch1[0]<ch2[0])&&(ch1[0]<ch3[0])&&(ch2[0]<ch3[0]))
            for(int i=0;i<4;i++)
            {
                System.out.println(+ch1[i]);
                System.out.println(+ch2[i]);
                System.out.println(+ch3[i]);

            }
        else if((ch2[0]<ch1[0]&&ch2[0]<ch3[0]&&ch1[0]<ch3[0]) )
            for(int i=0;i<4;i++)
            {
                System.out.println(+ch2[i]);
                System.out.println(+ch1[i]);
                System.out.println(+ch3[i]);
            }
        else if((ch3[0]<ch1[0])&&(ch3[0]<ch2[0])&&ch1[0]<ch2[0])
            for(int i=0;i<4;i++)
            {
                System.out.println(+ch3[i]);
                System.out.println(+ch1[i]);
                System.out.println(+ch2[i]);
            }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:27:00+00:00Added an answer on May 27, 2026 at 10:27 pm

    Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements “bubble” to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, it is not efficient for sorting large lists; other algorithms are better. Wikipedia

    The following is the sort-cut way to do so.

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Arrays;
    
    final public class Main
    {
        public static void main(String[] args) throws IOException
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    
            System.out.print("Enter the strings:->");
            String str=br.readLine();
    
            String strArr[]=str.split(" ");//your sentence will be split into words.
            Arrays.sort(strArr);
    
            for(int i=0;i<strArr.length;i++)
            {
                System.out.println(strArr[i]);
            }
        }
    }
    

    If you wish, you can apply your own logic as follows.

    final public class Main
    {
        public static void main(String[] args) throws IOException
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    
            System.out.print("Enter the strings:->");
            String str=br.readLine();
    
            String strArr[]=str.split(" ");
    
            String temp;                                    
    
            for(int i = 0; i<strArr.length - 1; i++)        
            {                                               
                for(int j = 0; j<strArr.length - 1; j++) 
                {                                           
                    if(strArr[j].compareTo(strArr[j+1]) > 0)
                    {                                       
                        temp = strArr[j];                    
                        strArr[j] = strArr[j+1];              
                        strArr[j+1] = temp;                  
                    }                                       
                }
            }
    
            for(int i=0;i<strArr.length;i++)
            {
                System.out.println(strArr[i]);
            }
        }
    }
    

    In both the cases, I have assumed spaces as word separator and not commas , that you’re using in your example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Tried a bunch of things but I can't get it to work consistently amid
Tried examples from 'php.net' but don't understand what's the problem. Any suggestions? <?php $_SESSION['test']
Tried to map it from Preferences -> Settings -> Keyboard, but the key combo
tried to find the answer by googling and in MSDN but with no luck.
Tried to install some program, it gives above message requiring 1.1.4322. BUT as the
tried to research on that but sometimes I seem to lack some googling skills...
I am currently using displaytag for tabular data, but would like to create a
I've got problem with sorting my lucene.net index in .NET. I tried almost every
I'm sorting a list of species, but sometimes the list is large and it
I have a sort function which was working fine but then I tried to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.