r/dailyprogrammer • u/rya11111 3 1 • May 04 '12
[5/4/2012] Challenge #48 [easy]
Take an array of integers and partition it so that all the even integers in the array precede all the odd integers in the array. Your solution must take linear time in the size of the array and operate in-place with only a constant amount of extra space.
Your task is to write the indicated function.
15
Upvotes
7
u/huck_cussler 0 0 May 04 '12
Kinda like a quicksort:
2
u/n1ncha May 04 '12
2
u/emcoffey3 0 0 May 04 '12
Solution in C#:
Here is another solution in C# using LINQ (this one returns a new array instead of modifying the existing array):
2
u/juanfeng May 04 '12
C
2
u/EvanHahn May 07 '12
The first Python I've ever written:
Criticism welcome.
1
u/[deleted] May 04 '12 edited May 04 '12
Python:
1
u/[deleted] May 04 '12
1
u/Fustrate 0 0 May 04 '12
Python:
No idea if it meets the time/in-place/space requirements.
1
u/gjwebber 0 0 May 04 '12
Here's my go in Python:
1
u/StorkBaby 0 0 May 04 '12
Not sure about the linear time / in-place requirement:
edit: returned c1+c2 previously
1
u/[deleted] May 04 '12
Hate perl having to pass arrays as references...
1
u/grammaticus May 04 '12
Python:
Edit: formatting
1
u/wbyte May 05 '12
Go:
1
u/bh3 May 05 '12
Not stable (but not a requirement), but should take one pass and is in-place, C:
1
u/FataOne 0 0 May 05 '12
1
u/Maristic May 05 '12
Perl one liner (alas, nothing clever as far as Perl-ish features used):
1
u/[deleted] May 05 '12 edited May 05 '12
JavaScript
Okay, I'm proud of this one because it didnt take too much troubleshooting. Please please, any feedback is welcome. But is this linear time? I basically take the odd number and swap it with a next or later number in the array if it tests as even. So technically, the operation to move the odd number gets faster as you move you forward in the array because there are fewer numbers left to test in the loop to see if the swap should take place.
1
u/monoki May 05 '12
not sure if this fits the requirements, can someone see?
1
u/yesweclam May 05 '12
In Ruby
1
u/Alborak May 05 '12 edited May 05 '12
Java, using a Turing machine style approach:
public class OddEvenSort {
}
1
u/ret0 May 05 '12
C (c99):
1
u/baijuke May 05 '12
Python:
1
u/jfrios 0 0 May 05 '12
Python:
1
u/cstb May 05 '12
1
u/[deleted] May 07 '12
JavaScript: Okay, try #2.
This time, I only have one loop. The algorithm iterates, basically ignores odd numbers, but when it finds an even number, it puts it at the beginning and the array. It remembers how many even numbers were found so that it can accurately place the latter even numbers in the array back at the right spot towards the beginning. Again, only been coding for a short time so feedback is 100% beloved.
1
u/ArriMurri May 07 '12
In Scala:
1
u/EvanHahn May 07 '12
Ruby:
1
u/maloney7 May 07 '12
Javascript:
1
u/loonybean 0 0 Jun 17 '12
Python:
1
u/[deleted] Oct 06 '12 edited Oct 06 '12
JavaScript - linear my ass.
1
u/[deleted] Feb 15 '22
Python.
thought I would try recursion instead of for loops
partition([1,1,1,1,1],0)
0
u/[deleted] May 04 '12
[deleted] — view deleted comment
0
u/[deleted] May 04 '12
[deleted] — view deleted comment
1
u/[deleted] May 04 '12
[removed] — view removed comment