• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array sorting  RSS feed

 
Alex Chun
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good morning!
I have a challenge. Given two arrays - classes (String[]) and their run time (long[]). I need to sort classes from high speed to low speed. If some classes have the same run time then it must be sorted alphabetically.
I can't decide the same run time classes case. Here is incorrect sorting below.



Compilation results:

[Cat, Dog, Apple, ---, Orange, tree, Application]
[10, 7, 123123, 1, 123123, 423, 12]
[---, Dog, Cat, Application, tree, Orange, Orange]
[1, 7, 10, 12, 423, 123123, 123123]

Process finished with exit code 0

 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Problem does not sound very clear. Also it feels that you chose wrong variable names, because they are at least confusing.

According to you, "run time" means "modificationDates" and you want to sort them by "speed". Shouldn't you choose something meaningful?
Alex Chun wrote:classes (String[]) and their run time (long[]). I need to sort classes from high speed to low speed.


tempMD, tempCN also could be better chosen. As after looking at them, I cannot understand what are they intended to mean (well, probably tempModificationDates and tempClassNames).

Also, after you copied and tried to "sort" arrays, you missed 1 element, "Apple", it seems it has been replaced by "Orange".

Very difficult to step in, since it looks over complicated in describing the problem and what has to be done.
 
Alex Chun
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. I've chosen another way. I've chosen OOP =)
"Run time" means the time of the last opening of this class. No correctly, sorry. I need sort classes by this time.
And now my released code is below. Have a look please, guys. Help to beginner. I'm learning fastly and hard-workingly.







Output:
324 : sadas
3424455334 : qwe
4324324 : 0asdas

But I have not got my results yet.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please avoid long lines because they are difficult to read. I have broken your line 8 into three parts, and you can see how to do it.

Why are you using a Long rather than a long in the Searcher class?
Why have you got an empty constructor in the Searcher class? I think you should delete it.
Parallel arrays in line 15? They are error‑prone. What if the two arrays are not the same length. Also you might do well to regard that method as a factory method and make it static.
Why are you using parallelSort in line 25 rather than ordinary Arrays#sort?
There is a much neater and shorter way to implement the Comparator.

But all those are minor points; if you manage to get your parallel arrays the same size, I should think that program will actually sort your array for you.

Have you tried dispensing with the Comparator as a class and using a λ?
 
Alex Chun
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please avoid long lines because they are difficult to read.

exactly, corrected.

Why are you using a Long rather than a long in the Searcher class?

really I don't know, corrected.


exactly, corrected.


marked method arrayOfSearcher such as static.

Parallel arrays in line 15? They are error‑prone. What if the two arrays are not the same length.

As I understand to throw IOEception, isn't?

Why are you using parallelSort in line 25 rather than ordinary Arrays#sort?

Because of challenge conditions. There are may be until 100000 classes. This case is better use parallelSort.

Have you tried dispensing with the Comparator as a class and using a λ?

I don't understand what you mean. I use Comparator the only way.

if you manage to get your parallel arrays the same size

do you mean String[] classNames, long[] modificationDates?
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Chun wrote: . . . corrected. . . . corrected. . . . corrected. . . .
marked method arrayOfSearcher such as static.
I presume you know why that factory method is probably better static?
. . .
As I understand to throw IOEception, isn't?
An IndexOutOfBoundsException, more likely.
. . . There are may be until 100000 classes. This case is better use parallelSort. . . .
In that case parallelSort probably runs faster, yes.
I don't understand what you mean. I use Comparator the only way.
Comparator is a Functional Interface. That means you can convert it to a function. Haven't got time to explain any more now, I am afraid.
. . . do you mean String[] classNames, long[] modificationDates?
Yes.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 30 - 34. Notice something wrong?
Where this 12 comes from? shouldn't be instead value taken from parameter? "start" is not used.

 
Alex Chun
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I presume you know why that factory method is probably better static?
. . .

Because we need this array once for class neither for every object

An IndexOutOfBoundsException, more likely.
. . .
Exactly.

if you manage to get your parallel arrays the same size[/qoute] . . . do you mean String[] classNames, long[] modificationDates?...Yes.


If it is input data how I can do it?
And if these arrays would not be the same size so it's generated exception.
I have no exception I have invalid results.
 
Alex Chun
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Lines 30 - 34. Notice something wrong?
Where this 12 comes from? shouldn't be instead value taken from parameter? "start" is not used.



My challenge: I need implement the interface.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Chun wrote:
Myself wrote:I presume you know why that factory method is probably better static?
. . .

Because we need this array once for class neither for every object

. . .
No. Because you may need to create such an array before any instances exist.
 
Alex Chun
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's done) Thank you all guys.
PS. Campbell, I respect you.
PPS. Better method "refresh" to mark static, really? But it's non-static the challenge condition (interface ISearcher).

And one method else =).

I've implemented method "guess" in the same class.
This method is reffered to interface ISearcher above.
Question: Could not I use collections?

 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a method is in an interface (which I didn't notice) then it is non static (Well, in Java8 you can have static methods in interfaces.).

If you are linking first letters to words, how about a Map<Character, List<String>>? That will save you the problem of going through the array every time you run that method.
If you have the List of words sorted you can use a binary search.
 
Alex Chun
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If a method is in an interface (which I didn't notice) then it is non static (Well, in Java8 you can have static methods in interfaces.).

If you are linking first letters to words, how about a Map<Character, List<String>>? That will save you the problem of going through the array every time you run that method.
If you have the List of words sorted you can use a binary search.


No, I've got a whole input String start by conditions.
I must find all classes names which begin with String "start" and regularize it alphabetically. Classes names are in the array. Array as a field of class "Searcher".
Is my algorithm optimal here?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!