Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting an array 1

Status
Not open for further replies.

Brellium

Technical User
Oct 1, 2003
4
US
HELP!!!!

Sorry couldn't resist

I'm trying to take an array and flag numbers out of that array that fit a pattern (but not remove them).

What I need to do is "flag" every nth number in the array such as every 6th, then run a second pattern that tags every 8th. On top of that each of these numbers start at a differring place in the array (but an offset should take care of that). While everything seems doable I have not seen any documentation thats shows how to do the most important part, that is flagging the numbers in the pattern.

Any help would be appreaciated.
 
I am confused. the subject says "Sorting an array" but i don't see any question about sorting an array.

Also what is "flag"? I am not aware of any standard definition for that term.

-pete
 
Lets try this:

//Building the array

public class ArrayNumber {
public static void main(String[] args) {
int[] numberArray;

numberArray = new int[100000];

for (int i = 0; i < numberArray.length; i++) {
numberArray = i +1;
System.out.print(numberArray + &quot;\n&quot;);
}
System.out.println();
}
}
I guess what I'm looking to do is build a second array using copy, copying every nth number to that second array and then reruning it for a different nth number.

I just can't seem to find a valid arguement in copy that instructs copy to do this so I guess there would have to another way.

After that I can just compare the two arrays.
 
Well it is difficult for me to understand what your requirements and goals are. This is starting to look like a course assignment from school.

The best feel I can get so far is that you want to generate an index, or perhaps more than one, into the primary collection.

So the first index (second array) would contain an entry for every nth element of the source where the value is the index in the source array. You could create one of these for each index you needed.

However in your posted source, the values of the source array are actually just duplicates of the index they are located at, offset by one, so the need for an index in that case is obviated since every 5th element would contain a multiple of 5 and every 8th element would contain a multiple of 8 etc.


-pete
 
I wish I was taking a class, this would be much easier.

This is part of a primality sieve and sadly the hard way of doing it, this system involves about (n/110)^1/2 calculations. The other way while doable has some log determining the steps taken and would be much more difficult to run (rather program).

Success first elegance later.
 
To build the second array, code a for loop, like in your posted code, but instead of starting i at 0 and incrementing i with 1, start at the offset you want to start and increment with &quot;n&quot;.

Do the same for your third array.
Code:
for (int i = offset; i < numberArray.length; i=i+n) { ...
=========
This code is not completely correct, it will (probably) give an &quot;index out of range&quot; at the end, but you can fix that.
 
...
...

You know, that's just too easy, <sarcasm> but I'm trying to to do this in the most difficult way I can imagine. </sarcasm>

I think I can figure the rest out.

Infact since everything else depends on an algorithum I can just have n the result, this solves about 5 problems at once.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top