How to build a case sensitive bubble sort and selection sort for a 2 dim array


Here is what i currently have but it does not work properly

static void bubblesort(String[][] Array){
        String datae = null;
        String temps;
        String tempse;
        
        	
	for(int x=1;x<i;x++)
		{
		for(int y=0;y<i-x;y++)
			{
			if(Array[y][1].compareTo(Array[y+1][1])>0)
				{
				temps=Array[y][1];
				Array[y][1]=Array[y+1][1];
				Array[y+1][1]=temps;

                                tempse=Array[y][0];
				Array[y][0]=Array[y+1][0];
				Array[y+1][0]=tempse;

                                tempse=Array[y][2];
				Array[y][2]=Array[y+1][2];
				Array[y+1][2]=tempse;
				
				}
				}
				}

      System.out.println("Bubble Sorted");
       for (int z = 0; z < i; z++){
           datae = Array[z][0];
           Array[z][1]= datae.substring(0, 1).toLowerCase();
           System.out.println(Array[z][2] + ":" + Array[z][1]+":" +Array[z][0]);


       }
    }

    //selection sort method
    static void selectionSort(String[][] arrays)
	    {
        String data = null;
	        for (int e = 1; e < i; e++) {

	            // find the index of the ith smallest value
	            int s = e-1;
	            for (int j = e; j < i; j++) {
	                if (arrays[j][0].compareTo(arrays[s][0]) < 0) {
	                    s = j;
	                }
                      
	            }

	            // swap the ith smallest value into entry i-1
	            String temp = arrays[e-1][0];
	            arrays[e-1][0] = arrays[s][0];
	            arrays[s][0] = temp;
                    
                    temp = arrays[e-1][2];
	            arrays[e-1][2] = arrays[s][2];
	            arrays[s][2] = temp;


	        }

                System.out.println("Selection Sorted");
       for (int z = 0; z < i; z++){
           data = arrays[z][0];
           arrays[z][1]= data.substring(0, 1).toLowerCase();
           System.out.println(arrays[z][2] + ":" + arrays[z][1]+":" +arrays[z][0]);

       }

	    }

Recommended Answers

All 17 Replies

I am confused about the String[][] arrays parameter in each method. What is the point of the outer (or inner) array? Remember that in Java, a String is a built in type, not like C where it is an array of characters.

A very very brief web search (using Google: bubble sort in java) found some very professional looking code. You should probably look the same way, and understand what that code is doing before you write your own. If you just copy someone's code, it is plagiarism, which is wrong. If you look at two or three and then write your own with the knowledge you gained, it is research which is right and good (and to be honest, you must mention what you did, and the places you looked: Give credit to the people who made your life easier).

Bear in mind that one thing you are learning as a student is how to do research. As a programmer, I spend a lot of time looking for answers that other people have already found. I just spent about an hour this morning on such a search, for example. I found the answer too! I love the internet!

As to your question: On lines 7 and 43 you make reference to a variable i that has never been declared. I'm reasonably sure that the compiler has issued an error about that (or something else that it found first). Next time you ask a question, please be a little more specific. "It does not work" is a large universe of possible problems. "The compiler said 'some actual error message' but I don't understand what to do about it" is a much better question.

Your professor is not mine, of course, but I once was given an open exam that included a question something like: "Find and prove a closed form approximation for calculating large Fibonacci numbers" (or maybe it gave the formula and told us to prove it?). My office mate had just recently published a paper (with that professor) doing just that, and I knew of it from talking to him. I got full credit on that question for just making a reference to the published paper. When the tests were handed back, the prof pointed out that my answer was not only the briefest, but also the best.

i is declared in my main class

My code works properly it's just that it is not case sensitive...how do i edit it to be so?

The doc for compareTo() says: "Compares two strings lexicographically."
Can you show an example where the results are not case sensitive?

This was my output see how it's not in order because of the Case is different

Bubble Sorted
1:b:Baba
2:e:Eggs
4:g:Good
0:b:baba
3:d:dodo

Selection Sorted
1:b:Baba
2:e:Eggs
4:g:Good
0:b:baba
3:d:dodo

it's not in order

What do you mean by "in order"?
Do you know the order of the letters?
'A' is before 'Z' which is before 'a' which is before 'z'
ALL uppercase letters sort before ALL lower case letters.

my proff said the output should be as follows

User types:
apple
Apple
Zone
apple
done
You display:
As entered
0:a:apple
1:a:Apple
2:z:Zone
3:a:apple
Bubble Sorted
0:a:apple
1:a:Apple
3:a:apple
2:z:Zone
Selection Sorted
1:a:Apple
0:a:apple
3:a:apple
2:z:Zone


but when i input the same info i get:

As entered
0:a:apple
1:a:Apple
2:z:Zone
3:a:apple
Bubble Sorted
1:a:Apple
2:z:Zone
0:a:apple
3:a:apple
Selection Sorted
1:a:Apple
2:z:Zone
0:a:apple
3:a:apple

Does your prof says that the ordering should be: 'A' then 'a' then 'B' then 'b' etc
The 2 dimensions that you show is confusing. Can you show what the ordering should be for single characters?

That makes it a more interesting problem. You will have to write your own compare routine to get that order.

yes norm it is supposed to be just like that! how do i do that?

Your code uses the compareTo method. Replace that with your code that will do the compare and return the results you want.

Lol If I could I would but I don't know how :-D

Get the source for the String compareTo method and study that. It comes with the JDK.

Here's two ideas:
1)Create an array of 256(or 128) elements and fill it with the values representing the order of the index to the array. For example array would have the value of the letter 'a'. Use those values to compare the chars in two strings.
2)Create a String with the chars in the order you want to sort. Use the indexOf() value as the sorting value for the letter. For example: str.indexOf('a') would be the value for 'a'

Holy Moly! @NormR1 is suggesting a lot of work. Much easier: In your code replace this method name: "compareTo" with the method named "compareToIgnoreCase". I believe that will solve your immediate need.

"Case sensitive" means "pays attention to case" which for ASCII characters means that 'A' < 'Z' < 'a' < 'z'. We talk about "ignore case" or "fold case" to mean something equivalent to replacing all the letters with their upper (or lower) case equivalent before doing the comparison. In an 'ignore case' situation, 'A' compares equal to 'a', 'Z' compares equal to 'z' and the usual alphabetical order is used for these 'folded case/ignore case' characters.

Read the OPs requirements for sort order: 'a' must come before 'A'
Does ignoreCase do that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.