• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loops for parallel arrays  RSS feed

 
Phillip O'Sullivan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone ... a newbie question, college assignment. I don't know how to loop my arrays, so when I sort them (highest/lowest) the 2nd array corresponds to the sorted array

 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Philip,

whenever you swap two beerStrength-elements (beerStrengths[i] < beerStrengths[j])
also swap the corresponding beerNames.

Greetz,
Piet
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you been told to use parallel arrays? That is usually dreadful design and error‑prone. If you are going to sort, why don't you create a Beer class, then you can have an array of Beers.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is going to be better design but it will take a lot longer. You may not have the time to implement everything I am telling you at present.

Are you doing your own sorting or using Arrays#sort? If you aren't using Arrays#sort, then remove the import for Arrays, because you are not using it. Similarly remove the Scanner which you appear not to be using.

If you are doing your own sorting, then don't write the sorting code in our main method. Write a utility class, with all its method public staticBefore you manage to write the sortArray(Object[]) method, you need a bit more information. You can find that from the Collections part of the Java® Tutorials. That tells you some of the pitfalls to avoid too.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, and forget about arrays! Use a TreeSet, with the comparison function thrown in
as lambda! That'll teach that teacher!

(only joking)


This reply was related to Campbells first reply. Campbells second reply
may be lessons ahead of where Philip is. Curious about Philips reaction.

Greetz,
Piet
 
Phillip O'Sullivan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys ... thanks for the input so far.

Yes we have to use arrays so I'm stuck with this.

Thanks Piet ... but I don't know how to swap the corresponding names

 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are still with me, you can create a Beer class and sort the beer array
MyUtilities.sortArray(beerArray);
If you read the Java® Tutorials section you will know that you can implement Comparable or create a Comparator. So the sortArray method will have to have Comparables. You will have to do some casting and you end up with the following test or something similar
if (((Comparable)arr[i]).compareTo((Comparable)arr[j]) > 0) ...
I am not telling you any details about sorting algorithms now.
If you get the greater than the wrong way round and end up checking for less than zero, or exchange the two objects to compare, the array will sort backwards.
If you manage to pass something with isn't Comparable, you will suffer an Exception. The same Exception happens in Arrays#sort, too.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote: . . . lessons ahead of where Philip is. . . .
Which is why I said he may not have time to implement this now.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Philip,

well, I reckon: if you are a bit further on the way to Java mastership, have another look
at what Campbell wrote, because it makes perfect sense.

But what I meant is:

you swap thw beerStrength-elements in this way:

you swap element i and element j.

Now, look at your beerNames-array. To keep the names
in correct order, you should now also swap element i and element j
of this beerNames array. Can you figure out how to do this?

Greetz,
Piet
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phillip O'Sullivan wrote: . . .
Thanks Piet ... but I don't know how to swap the corresponding names

You can swap two elements in the strength array like thisor like this:-All you have to do to swap the other array is to run the same code in tandem;And you can double up the code in the 3‑version similarly. I shall add one line as a hint. Make sure to do the two swaps together so you have the same values for i and j.
 
Phillip O'Sullivan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THis is my assignment

1) Sort by Beer Strength
a) By Highest Strength
b) By Lowest Strength
c) Return to main menu

2) Sort by Beer Name
a) By Name Ascending
b) By Name Descending
c) Return to main menu

3) Search by Beers
a) Greater or equal than a Certain Strength
b) Less or equal than a Certain Strength
c) By Beer Name. Note that the beer name search is a free text search. The keyword search is ignores case. The keyword must be greater than 3 characters in length.
d) Return to main menu


So I went with loops for the sorting rather than Array.sort.

My basic layout is a Switch statement with a new Method for each requirement

P.S. This is my first year so my understanding is very limited
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Magnus Magnussen always used to say
I've started, so I'll finish.
Now, making a beer class implement Comparable is a little awkward. I can think of two ways to do it. One is to use the > operator. If you get the > the wrong way round, your array will sort backwards.There is a shorter way to write that with :? but you may not be familiar with :? yet.

You might be tempted to subtract to get the difference but that won't work. If you have one beer at 4.7% and one at 4.3% you get a difference of 0.4% and you have to return an int, so you have to cast. Whichever way you try casting those three values, you will end up with the result being 0, and that is incorrect. What you can do with doubles however, is remember that there is a “wrapper class” corresponding to double and that implements Comparable, and you can make use of the fact that you already have a correct implementation. Change double in line 3 to Double, and we can now use this version of compareToYou can leave all your other occurrences of double unchanged because the JVM can automatically convert between doubles and Doubles both ways. In fact in Java8 you might even be able to leave line 3 unchanged but I am not certain.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays#sort not Array#sort.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still think you might do well to write a utility class with sort methods. I think you will get more marks for writing your own bubble‑sort algorithm than for using Arrays#sort.

Even though bubble‑sort is notoriously inefficient.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few moments ago, I wrote: . . . bubble‑sort is notoriously inefficient.
For small arrays like yours with 10 elements, however, that isn't actually true. If you look up Arrays#sort (as in link in previous post of mine) you find it uses merge sort (or quicksort). For small arrays with less than about 50 elements, bubble‑sort is as fast or faster than the more sophisticated sorting algorithms.

Of course the most inefficient type of sorting is called BogosortOnly to read over a drink. It has several other names: the bogo- bit is short for bogus.
 
Phillip O'Sullivan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone especially Piet ... I have solved the problem ... but not without your help



I knew it would be something simple but didn't have a clue where to go. Thank you all again.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Of course the most inefficient type of sorting is called BogosortOnly to read over a drink. It has several other names: the bogo- bit is short for bogus.

Interestingly enough, in my dialect (Bisaya/Visayan) the word "bogo" means "stupid". Go figure
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to be curious, i suppose for Java 7 onwards Arrays.sort doesn't use merge sort for objects. It uses now Tim Sort.

And the possible cut-off my algorithm team told us is around 10 elements.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:Just to be curious, i suppose for Java 7 onwards Arrays.sort doesn't use merge sort for objects. It uses now Tim Sort.

From the API JavaDocs:
Implementation note: The sorting algorithm is a Dual-Pivot Quicksort by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm offers O(n log(n)) performance on many data sets that cause other quicksorts to degrade to quadratic performance, and is typically faster than traditional (one-pivot) Quicksort implementations.

And you meant Timsort (I'm sure Tim Peters would be tickled though and wouldn't mind the way you wrote it )
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu, you are referring to the array of primitive type. Thanks for the correction for the name, it is Timsort. See the API docs for Object as below.



public static void sort(Object[] a)

The implementation was adapted from Tim Peters's list sort for Python ( TimSort). It uses techiques from Peter McIlroy's "Optimistic Sorting and Information Theoretic Complexity", in Proceedings of the Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, January 1993.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote: . . . in my dialect (Bisaya/Visayan) the word "bogo" means "stupid". Go figure
If that is an IndoEuropean language, there is the possibility that the two words are etymologically related.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:Junilu, you are referring to the array of primitive type.

Ah, I didn't scroll down enough... ok, thanks for the clarification.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your welcome. In between as you suggested in other thread(http://www.coderanch.com/t/647639/design/Distributed-Reservation-Management-System-DRMS#2985516) i am trying to work on my project using TDD. Do you mind to provide your feedback in that thread?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If that is an IndoEuropean language, there is the possibility that the two words are etymologically related.

[off-topic]
It could very well be. I know that in Bahasa, the word is bodoh and that part of the region was colonized by the Dutch and the British. We were a colony of Spain for over 3 centuries from the 1500s. I doubt that our bogo has Spanish roots though.
[/off-topic]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!