• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting arrays in specific ways

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends,

I am once again stuck at a certain problem.

The goal is to compare to arrays including numbers and give out the larger array (arr) with the number in the same order as in array 2 with the numbers missing in array 2 at the very end .
I tried to come up with a solution as below but don't really make progress as I am kind of lost...

Help will be appreciated!

 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Michael Grünau
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something like this;

{2,2,2,1,4,3,3,9,6,7,19}

So numbers which hare included in both arrays at the beginning and the numbers missing at the second one at the end.
I noticed that in my array b there are only numbers which are equal but not the missing ones....

There will be a few better ways to deal with the problem but I don't know how I can combine some loops to give me what I want.
My idea was the following:

- take the first number of the second array
- check with the number in the first one
- if included but them at the start of the new array
- if not but them at the end or something like i+1 from the array

Loop through all numbers from the second array?
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Michael Grünau
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry typo, should be:

arr =  {2,3,1,3,2,4,6,7,9,2,19};
arr2 = {2,1,4,3,9,6}

result = {2,2,2,1,4,3,3,9,6,7,19}  so same structure as array2 with the missing numbers included in array1 but not in array 2 at the end.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

with the numbers missing in array 2 at the very end

No mention if these numbers placed at the end need to be in ascending order or if the original order must be maintained, or it is up to the discretion of the developer.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Grünau wrote:Sorry typo, should be:

arr =  {2,3,1,3,2,4,6,7,9,2,19};
arr2 = {2,1,4,3,9,6}

result = {2,2,2,1,4,3,3,9,6,7,19}  so same structure as array2 with the missing numbers included in array1 but not in array 2 at the end.


You've changed arr2, it originally started with '12'. Which is correct?
 
Michael Grünau
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Original order as in array1...
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you allowed to use Lists and Comparators?
 
Michael Grünau
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunatley not, only java.lang.*.

So basic loops etc.
 
Michael Grünau
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My Main problem at the moment is that I don't know how I can treat the numbers not included in the second array.

Code at the moment is as below;



For my understanding this will put the numbers included in the first and second array in an extra array in the right order, but now the numbers not included in array 2 are missing.
Is there a way to add them at the end or how can I solve this issue?
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To make your code a little easier to follow, make a method

Then you can make two extra arrays:

Now for each element in longestArray:
if that element is in the shorter array, then inBoth[indexInBoth++] = element;
else onlyInA[indexOnlyInA++] = element;
When finished, add all the elements from onlyInA to inBoth and you are done.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the key here is SORT. If you first write a simple bubble sort routine it would normally sort by the numeric values, but in your case we'd want to sort by the index of the value in the arr2 array, and if the value is not in arr2, then return an index of Integer.MAX_VALUE.

Try that on for size and see what you think. A properly implemented bubble sort will leave the remaining values in their original order.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:To make your code a little easier to follow, make a method


For sorting I would use this variation
 
Michael Grünau
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all thank you very much for the quick help.

Can somebody give me an easy example for the use of an index?
Or any good links with an explanation?
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Grünau wrote:Can somebody give me an easy example for the use of an index?
Or any good links with an explanation?


In your code, i, j, and k are all indexes (the proper plural is "indices", FYI).  So you're already using them, even if you didn't know it.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Grünau wrote:Sorry typo, should be:

arr =  {2,3,1,3,2,4,6,7,9,2,19};
arr2 = {2,1,4,3,9,6}

result = {2,2,2,1,4,3,3,9,6,7,19}  so same structure as array2 with the missing numbers included in array1 but not in array 2 at the end.



Try This & Please Revert.




Just Print array of b to show the output.
Staff note (Junilu Lacar) :

Dixit Patel. Welcome to the Ranch!

We don't want CodeRanch to be a Code Mill, so we deleted the solution you provided. Please help by giving tips and advice instead of solution code.

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP - we've (other moderators and I) been playing around with different ways to solve this problem but I think I'll share my strategy with you because it's pretty straightforward, in my opinion. Of course, I'm biased but oh well. Anyway, here's how I approached it.

The approach is kind of like a modified selection sort except we're using the second array to determine the ordering of elements instead of their natural ordering (1, 2, 3, 4, etc.). I call that array the order. I refer to the array we want to reorder simply as numbers.

Using your data, numbers = {2, 3, 1, 3, 2, 4, 6, 7, 9, 2, 19} and order = {2, 1, 4, 3, 9, 6}

I need to keep track of the numbers that are already in the desired order. I'll use sorted to represent the index where I want to put the next number we want to put in proper order. Initially, that index will be 0.

{2, 3, 1, 3, 2, 4, 6, 7, 9, 2, 19}
 ↑
 sorted = 0

For each element, n, in the order array, I'll do this:
Starting from sorted, iterate over the rest of numbers:
if the current element in numbers is equal to n then I'll move that value down to the spot pointed to by sorted and shift every number in between up.

That's pretty much it. Let's see how it works with your data.

So iterating over order, the first value of n will be 2. This means I'm going to find all the elements in numbers equal to 2 and rotate them to where sorted currently points to and then shifting everything in between towards the end of numbers. The first 2 is already in place, so no shifting is done but still we increment sorted to 1 because that's the next spot that we'll be moving a reordered number into.

{2, 3, 1, 3, 2, 4, 6, 7, 9, 2, 19}
    ↑
    sorted = 1

Continuing our search for n (2) in numbers, we find another in numbers[4]

             numbers[4]
             ↓
{2, 3, 1, 3, 2, 4, 6, 7, 9, 2, 19}
    ↑
    sorted = 1

We need to move it to where sorted is pointing and shift all those numbers in between to the right:

      →  →  →
{2, 2, 3, 1, 3, 4, 6, 7, 9, 2, 19}
    ↖ ← ← ← ⤶
    ↑
    sorted = 1

We increment sorted again and now it's 2. We look for another element in numbers that's equal to n (2). We find another in numbers[9].

                            numbers[9]
                            ↓
{2, 2, 3, 1, 3, 4, 6, 7, 9, 2, 19}
       ↑
       sorted = 2

We rotate that 2 down to where sorted is pointing and shift everything in between to the right.

         →  →  →  →  →  →  →
{2, 2, 2, 3, 1, 3, 4, 6, 7, 9, 19}
       ↖ ← ← ← ← ← ← ← ← ← ⤶
       ↑
       sorted = 2

Once again, we increment sorted to 3. (We basically increment sorted each time we move a number down to that spot in numbers)

Since there are no more 2s in numbers to move, we get the next number in the order array: {2, 1, 4, 3, 9, 6}. So, n is now 1.

Starting from where sorted currently points, we scan numbers for n (1). We find it in numbers[4].

             numbers[4]
             ↓
{2, 2, 2, 3, 1, 3, 4, 6, 7, 9, 19}
          ↑
          sorted = 3

We move 1 and shift the number(s) in between to the right.

            →
{2, 2, 2, 1, 3, 3, 4, 6, 7, 9, 19}
           ↖⤶
          ↑
          sorted = 3

We increment sorted to 4 and find no other one. So we get the next n from order:  {2, 1, 4, 3, 9, 6}. n is now 4.

                   numbers[6]
                   ↓
{2, 2, 2, 1, 3, 3, 4, 6, 7, 9, 19}
             ↑
             sorted = 4

Move and shift right:

               →  →
{2, 2, 2, 1, 4, 3, 3, 6, 7, 9, 19}
              ↖ ← ⤶
             ↑
             sorted = 4

We increment sorted to 5, and get the next number to find from order:  {2, 1, 4, 3, 9, 6}. n is now 3. We find that all the 3s are already in place but we still follow the same procedure and increment sorted for each 3 we find that's already in place.

{2, 2, 2, 1, 4, 3, 3, 6, 7, 9, 19}
                ↑
                sorted = 5

{2, 2, 2, 1, 4, 3, 3, 6, 7, 9, 19}
                   ↑
                   sorted = 6

{2, 2, 2, 1, 4, 3, 3, 6, 7, 9, 19}
                      ↑
                      sorted = 7

                            n == 9
                            ↓
{2, 2, 2, 1, 4, 3, 3, 6, 7, 9, 19}
                      ↑
                      sorted = 7

                        →  →
{2, 2, 2, 1, 4, 3, 3, 9, 6, 7, 19}
                       ↖ ← ⤶
                      ↑
                      sorted = 7

{2, 2, 2, 1, 4, 3, 3, 9, 6, 7, 19}
                         ↑
                         sorted = 8, n = 6

{2, 2, 2, 1, 4, 3, 3, 9, 6, 7, 19}
                            ↑
                            sorted = 9, n = 6

That's the last n from order[]!

We're DONE!

{2, 2, 2, 1, 4, 3, 3, 9, 6, 7, 19} -- the rightmost are missing from order
                            ↑
 |⟸ . . ordered . .  ⟹|  sorted


Since we just shift numbers that are not sorted to the right, we maintain their original order relative to each other. When we run out of numbers in order, then everything in numbers before the sorted index will be arranged in the proper order and everything from sorted onwards will be the values in numbers that are missing from order, in their original order.

Pretty neat, right?

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dixit. Patel wrote:
Try This & Please Revert.


Dixit: (again) Welcome to the Ranch!

To reiterate the note I added to your first post, CodeRanch is NotACodeMill (←that's a link, click it) so I deleted the solution code you provided.

I tried out the code you gave and for the most part it works. However, in the case where the numbers array contains all missing numbers, that is, there are no values that are common to the two arrays, the code you gave fails. I got this:

assertion error: array contents differ at index [3], expected: <3> but was: <4>.

You might want to look into that case.

This is the test I ran that failed:

The array your code produced was this: {2, 3, 1, 4, 6, 7, 9, 19, 0, 0, 0, 0, 0}
 
Michael Grünau
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

last time I checked I was not able to post a reply to the topic.

Thank you very much for the detailed help, I tried it out and it worked perfectly!!!


Topic can be closed!
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What "it" did you try out? The algorithm I described or some code that was subsequently deleted by a moderator? In case it was solution code posted in this thread that got deleted, none of them were totally correct as far as I recall. If you implemented the algorithm I described, feel free to share your work so people can give feedback or otherwise learn from it as well.
 
reply
    Bookmark Topic Watch Topic
  • New Topic