Results 1 to 3 of 3
Thread: Array Sort Question
- 04-18-2012, 12:11 AM #1
Senior Member
- Join Date
- Oct 2011
- Posts
- 115
- Rep Power
- 0
Array Sort Question
Ok if I have an array that looks like this:{3,4,3,2,7,5,2,1}
I want to split the numbers in the array into sums of 9. So I start with the first index of the array and add it to the second, third, fourth, etc...until I get 9. If a total that I get is greater than 9 (e.g. 3 + 4 + 3 is greater than 9) then I want the current index to be moved to the end of the array and the for the other indices to be shifted left.
For example with my given array:
3 + 4 = 7 + 3 = 10.
So 3 needs to be sent to the end of the array pushing the other indices forward, like so: {3,4,2,7,5,2,1,3}
How would I do something like that? I'm not opposed to a Collections.swap approach. Below is a start of what I am try to attempt:
Java Code:int temp = 0, temp2 = 0; for(int i = frontPointer; i < backPointer + 1; i++){ for(int j = backPointer; j > frontPointer; j--){ temp = item.get(j); temp2 = item.get(i); item.set(i, item.get(i + 1)); item.set(j, temp2); item.set(j-1, temp); } }
- 04-18-2012, 12:30 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
- 04-18-2012, 01:14 AM #3
Senior Member
- Join Date
- Oct 2011
- Posts
- 115
- Rep Power
- 0
Similar Threads
-
heap sort question
By stuckonjava in forum New To JavaReplies: 18Last Post: 03-15-2012, 04:18 PM -
Need help with Array.sort()
By Kinney.j in forum New To JavaReplies: 1Last Post: 10-16-2011, 07:51 AM -
Selection sort question
By eoins2345 in forum New To JavaReplies: 11Last Post: 08-25-2011, 06:14 AM -
Question with bubble sort
By Metastar in forum New To JavaReplies: 22Last Post: 09-13-2010, 07:25 AM -
Array sort
By Jeremy720 in forum New To JavaReplies: 2Last Post: 10-08-2008, 12:41 AM
Bookmarks