Hello!
I know this excercise is for this moment turbo-hard! I'm sharing with you my flow-chart for my solution - waiting for your comments :)
This solution works great! Full code for request
This diagram looks like an overkill for this task - extracting the data in the way MAP->LIST->ARRAY->LIST is really unnecessary and it makes your solution too complex. To solve it is enough to define two lists to sort the values and additionaly and array to keep an order. The proposal of the solution can be as below:
publicstaticvoidsortuj(String[] tablica){ArrayList<Integer> integers =newArrayList<>();ArrayList<String> strings =newArrayList<>();boolean[] order =newboolean[tablica.length];//boolean array(to keep track of an order of given items)// splitting data into two separate lists(strings, integers) + boolean array(true for strings, false for ints)for(int i =0; i < tablica.length; i++){if(isLiczba(tablica[i])){
integers.add(Integer.parseInt(tablica[i]));
order[i]=false;}else{
strings.add(tablica[i]);
order[i]=true;}}//sorting strings listCollections.sort(strings,newComparator<String>(){@Overridepublicintcompare(String arg0,String arg1){if(isWiekszeOd(arg0, arg1)){return1;}return-1;}});//sorting integers listCollections.sort(integers,Collections.reverseOrder());//assigning sorted values to an arrayfor(int i=0, strI=0, intI=0; i < tablica.length; i++){if(order[i]){
tablica[i]= strings.get(strI++);}else{
tablica[i]=String.valueOf(integers.get(intI++));}}}
That's still somewhat complex.
At that time we already coded some basic sort algorithm several times (bubble- or selection sort - except you were super smart and used Collections or Arrays class build in functionality). This time you could do the same and just code a simple bubble sort with the two nested loops... but with one lil difference... if the outer loops iteration is matching an int, then only sort when the inner loops iteration also is an int. If the inner loops iteration is a string, then skip. Preceed similar if the outer loops iteration is matching a string, then just sort if the inner loops iteration is also a string. That's it.
That way you'd need no additional collection/ array. However the complexity is O(n^2) while your solutions complexity is O(n log n). The downside is the memory consumption orig + 3 lists + 2 for sort (mergesort needs twice the mem). So your mem usage would be at least 6 times the bubble sort approaches mem usage.
btw. sorting the string list could easily be done with just Collections.sort(strings); No need to write a Comparator.
if CG insists on the usage of isWiekszeOd then just try to call it somewhere in your code
isWiekszeOd(0, 0); and throwing away the result.
or you make it a lambda
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.
Collections.sort(strings);
No need to write a Comparator. if CG insists on the usage of isWiekszeOd then just try to call it somewhere in your code isWiekszeOd(0, 0); and throwing away the result. or you make it a lambda