![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Posts: 52
Rep Power: 4
![]() |
Possible Alogrithm??
Hey everyone. I have to write a method that takes an array or intergers as its only argument. I then must manipulate the values in the array in such a way that the smaller numbers are on one side and the larger number are on the other side.
The example my professor gave was: x = [19,42,38,-2,17,42,3,18,99,0,19,11] amount of numbers = 12 random number from group = 19 the array of "x" must then trun into: x = [1,-2,17,2,18,0,19,38,99,42,19,42] essentially the middle number 19 is like a kid. Everyone smaller then him to the left everyone bigger to the right (they don't have to be sorted). The only resriction i have is that i can only be in one array and our only hint was that it can be done in about 15 lines of code. My psuedo code is something like this: start program create new array named "x" fill "x" with 12 random numbers --not sure how he's moving the numbers-- display the output of the array has anyone heard of such an alogrith or can someone give me some general clues on how to go about this? I don't want someone to do it for me though otherwise i'll never learn! -NightShade |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3
![]() |
Research sorting algorithms.
|
|
|
|
|
|
#3 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,107
Rep Power: 5
![]() |
Quote:
1, -2, 17, 2, 18, 0, 19, 19, 38, 99, 42, 42Having said that, the simplest way I can think of would be a variant of the bubble sort. Basically, you'd iterate through the array, comparing each value to your middle value. You'd also need to know where it was in relation to this middle value (ie, before or after). If it was smaller, you would 'bubble' it to the left (unless it was already there), and if it was larger, you'd bubble it to the right (again, unless it was already there). If it was equal, you'd need to move it into an adjacent position, but I think you'd find it easier to do this after all the other numbers were handled, though you could make the algorithm a little smarter, and have it shuffle the other elements past all instances of the middle number (this would obviously require more work). In case you're unfamiliar with what I mean by bubble sort and bubbling, think of it this way: you have a vertical stack of numbers, and the smaller ones rise (or bubble) to the top. Repeat this process enough, and the array becomes sorted. There are lots of variants, and you don't need to move the smallest to the top (you can use whatever comparison criteria you like, so long as you're consistent).
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
|
#4 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3
![]() |
From the example, this looks really similar to quicksort, only without the recursive calls.
|
|
|
|
|
|
#5 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
int temp = array[x]; array[x] = array[y]; array[y] = temp; List<Integer> list = Arrays.asList(array); int temp = list.remove(x); list.add(y, temp); array = list.toArray(); |
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: May 2006
Posts: 28
Rep Power: 0
![]() |
As previously mentioned, you want to use a quicksort algorithm without the recursive calls. THis algorithm uses the first number as the pivot and essentially, everything smaller goes on the left of it, everything bigger goes on the right.
The reason you dont need the recursive calls is that your teacher does not require the array to sort. How this will work is you take the pivot (call it p), and starting from position 1 (call it n) compare it to p IF n > p -> leave the number where it is. n = n+1 IF n < p -> shift all number starting from n-1, down to p to the right. and put n where p was. n= n+1. IF n = p, (you didnt really specify where it needs to go.. but im guessing treat it as n > p. So an example of this working is: [5][4][6][11][3][1][7] [4][5][6][11][3][1][7] [4][5][6][11][3][1][7] [4][5][6][11][3][1][7] [4][3][5][6][11][1][7] [4][3][1][5][6][11][7] [4][3][1][5][6][11][7] As far as i can remmeber, this is how 1 iteration of quicksort works... and will get the job done for you. Hope I helped NSchnarr |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|