Monday, 15 March 2010

java - Bubble Sort with compare method not running -


This is a follow up to my previous question that I had asked yesterday. I have written a bubble sort method which analyzes an array of strings and returns the number of similarities, which provides the alphabet in order to order the method (assignment is to write several sorting methods and each Graph a number of comparisons to compare one, to see which is the most efficient).

This is my code:

  public static zero main (string [] args) {string [] test_array = {"bill", "fill", "Adam" }; / * System.out.println (compare ("orange", "boogie")); * / System.out.println (Bubble sort (test_array)); } Comparison of public static int (string A, string b) {int len ​​= Math.min (a.length (), b.length ()); // Looping through every character, if CH is less than chb, then method 1 returns, and so on. For {int i = 0; i & lt; len; i ++} {char cha = a.charAt (i); Four chb = b.chart (i); If (cha leftibbs; CHB) {return -1; } And if (Ch & gt; chb) {return1; }} // Now we eat for the length of the word because it can be the same word if (a.length () & lt; b.length ()) -1 return; Else if (a.length ()> gt; b.length ()) back 1; // appears to be the same string, then return 0. And return 0; } Public Static Ent Bubble Sort (string [] test_array) {Boolean swap = true; // variable to track the number of comparisons Int compNumber = 0; While (swap == true) {swapped = false; (Int i = 1; i & lt; test_array.length; i ++) for {// compn number compNumber ++; If (test_array [I-1], test_array [i]) & gt; 0) {// change variable by using a floating variable string logical = test_array [i-1]; Test_array [i-1] = test_array [i]; Test_array [i] = floating; Swap = true; } Else {swapped = true; }}} Return COMPNumber; }  

So the comparison method (given by array) compares two stars and determines whether they are in alphabetical order (return -1) or not (1 return) , Or the same word (return 0). Then, the bubble sort method compares the method to go through the array, and it then creates a switch, whereas the number of loops in my Vampan-NAB variable is computed (hence the number of comparisons).

It is compiled exactly, but it does not stop running unfortunately and does not return anything. I have waited 5 minutes so I have decided that something is wrong, maybe with my loop I am unable to find any problem, which has manipulated the parameters of many times, can someone help me? thank you in advanced.

"swap" will always be correct, so looping will never end. I think that you put swap = wrong in the second description


No comments:

Post a Comment