java - Bubble Sort (2 for loops vs 1) + Int Array to String Array? -
I have a homework assignment for Java class and I need some help. I have sorted a bubble in the assignment, it works wonders, but I wrote a version for a loop, however, one does not work for the loop version. Anyone know why? It's just for kick and giggles, which is not necessary for the assignment.
My second question is how can I convert a Sir array (filled with cast intents to random functions) into a string array. I know that the solution involves the "Arrays" library, but I do not remember that in the classroom and it will be used in the final form. Any ideas?
If you are interested in people, then this code is:
import static java.lang.Math.random; Public class main {public static zero main (string [] args) {int intarray [] = new int [50]; String Strawre [] = New String [50]; For (int i = 0; i & lt; intarray.length; i ++) {intere [i] = (int) (1000 * random ()); } (Int i = 0; i & lt; intarray.length; i ++) {System.out.print (intarray [i] + ""); } Bubble-ess 2 (intere); Println (); (Int i = 0; i & lt; intarray.length; i ++) for {System.out.print (intarray [i] + ""); } {} (Constant = 0; j & lt; array. Lanti - 1; j ++) {if (array [j] & gt; array [i]) {int temp = array [i]; Array [i] = array [ja]; Array [j] = temporary; }}}} Public static void BubbleSort2 (int array []) {for (int i = 1; i & lt; array.length; i ++) {if (array [(i - 1)]> gt; array [ I]) {int temp = array [(i - 1)]; Array [(i - 1)] = array [i]; Array [i] = temporary; }}}}}
The worst case performance of bubbles sorted (O ^ 2 ).
Every time you loop through the list, you are taking the maximum element into the correct position in the array (the last). In the worst case scenario, you have to do this for each element in the list, this is the reason why you have an external loop, thus ensuring that you take each element to its correct location.
In BubbleSort2
, you are essentially taking maximum space to the right place, because you are taking it to the position till the situation.
For your second question, a very easy way to do this is to create only one size string []
:
string [ ] StringArray = new string [intarray.length];
Then, we can fill this array with the contents of our Sir array. We find it to can do this by looping through the int []
are
(int i = 0; i & lt; intArray.length; i ++ ) {StringArray [i] = interstate [i] + ""; }
This is a thief through every integer in your int
and then converts them to a string, and they string [] Stores in
.
Comments
Post a Comment