QUICK

QUICK

WRITE A FUNCTION IN YOUR FAVORITE LANGUAGE THAT BY GIVEN ARRAY OF TYPE INT THAT WILL PUT ALL 9s AT THE END OF THE ARRAY WHILE KEEPING THE RELATIVE ORDER OF THE OTHER ELEMENTS

IT SHOULD BE AS FAST AS POSSIBLE AND USE AS LITTLE SPACE AS POSSIBLE

OR THIS BIRD IS GONNA STAB YOU

I will start
void sortN(int* a, int aSize) {
int i = 0;
while (a[i] != 9)
++i;
int posN = i;

for (i < aSize; ++i) {
if (a[i] != 9) {
swap(a[i], a[posN]);
++posN;
}
}
}

Other urls found in this thread:

amberbit.com/blog/2014/6/12/calling-c-cpp-from-ruby/
twitter.com/SFWRedditGifs

(defun sage (list &optional acc)
(cond ((endp list) acc)
((= (car list) 9) (sage (cdr list) (cons 9 acc)))
(t (cons (car list) (sage (cdr list) acc)))))

def nineSort(arr):
isNine = lambda x : x == 9
return sorted(arr, key = isNine)

or iteratively and in-place
(defun kisama (list)
(do ((l list) acc)
((endp (cdr l)) (rplacd l acc) list)
(cond ((= (car l) 9)
(rplaca l (cadr l))
(rplacd l (cddr l))
(push 9 acc))
(t (pop l)))))

BIRDMIN NO

n=x=>x.sort(a=>a==9)

WERK OR YOU DIE

here's one that's really readable and reasonably fast. 2 or 3 passes needed depending on implementation of append
(define (nines2end list)
(append (filter (λ (elem) (not (= elem 9)))
list)
(filter (λ (elem) (= elem 9))
list)))

i can't program this is all i have

Fear it.

sub endnines {
@j = grep { $_ != 9 } @_;
push @j, 9 while ($#_ > $#j);
return @j
}

use std::prelude::*;
use std::cmp::*;

fn sort(list: &mut [i8]) {
list.sort_by(|a, b| {
if *a == 9 { return Ordering::Greater }
a.cmp(b)
});
}

[A(A~=9) A(A==9)]


fa/g/s don't know about Matlab

+1

I always loved the fact that solutions written in python are the most concise in these threads.

/ / hello world9

NOT LINEAR COMPLEXITY
YOU ARE GETTING STABBED BOY

push9s = uncurry (++) . partition (

post test cases next time, maybe in 1st answer :^)

>for (i < aSize; ++i)
Won't compile. Go stab yourself.

Bird, pls
def sort9(some_list):
return list(filter(lambda x: x != 9, some_list)) + list(filter(lambda x: x == 9, some_list))

[CODE]
#include
#include

void check_pointer(void* p)
{
if (!p)
{
printf("Memory allocation failed!");
return exit(-1);
}
}

int *sort_nines_last(int* unsorted_array, int n)
{
int *sorted_array = malloc(sizeof(int)*n);
check_pointer(sorted_array);
int non_nine_pos = 0, nine_pos = n-1;
for (int i = 0; i < n; i++)
{
if (unsorted_array[i] != 9)
sorted_array[non_nine_pos++] = unsorted_array[i];
else
sorted_array[nine_pos--] = unsorted_array[i];
}
return sorted_array;
}

void print_array(int* a, int n)
{
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
}

int main()
{
int a[] = {1, 6, 2, 9, 2, 4, 9, 0, 9, 5, 6, 7, 3, 9, 2, 5, 9, 1, 2};
int n = sizeof a / sizeof a[0];
printf("Unsorted array: "); print_array(a, n); putchar('\n');
printf("Sorted array: "); print_array(sort_nines_last(a, n), n); putchar('\n');

return 0;
}
[/CODE]

Nigger what the fuck.

Yeah, you are right, fixed and optimized
void sortN(int* a, int aSize) {
int i = -1;
while (a[++i] != 9) { };
int posN = i;
for (;i < aSize; ++i)
if (a[i] != 9)
swap(a[i], a[posN++]);
}

Match me, with my shitty i3

>Fast
>Memory efficient
YOU GONNA GET STABED

Lmfao it's faster than the other solutions and there is plenty of memory nowdays

Sure, match me >there is plenty of memory nowdays
How much memo an embedded controller does have?

About as much as you mom had in her head when you were conceived

Found the CS major

# Ruby (version with "stabby lambda")
no_9 = ->(i) {i!=9}
a = [1,9,5,9,2,9,4]
b = a.select(&no_9).concat a.reject(&no_9)

In Rust:

fn sort9(list: &mut [i64]) {
list.sort_by_key(|&val| val == 9);
}

Happy to see a Perl solution, but I fixed it for you.

Here :
eval unpack u=>q{_

Damn didn't think to use sort_by_key()... So wait how does this work exactly?

>In Rust:
>
>fn sort9(list: &mut [i64]) {
>list.sort_by_key(|&val| val == 9);
>}
Wait... won't this just move all 9's up?

Ah, I made a better version.
Should also be faster ..

[1,2,9,5,9,2,9,4].inject([]) { |a,b| b==9 ? a.push(b) : a.unshift(b)}

val == 9 produces a bool.
Together with true > false, all 9's are greater than every other number due to this and are moved to the end of the list.

Hahaha, that looks lyke typcal Perl code to me..

You Haskell fags are the only ones that write more concise code that we Rubyists.. Ó_ô

Is that a Haskell standard library or some "special way" of list processing?

Test it with {9, 9, 9, 9, 0, 5, 3, 9, 9, 0}
sorting it 100000000 times and post time
I am genuinely interested in the speed of that

defmodule Nine do
def main(list) do
Enum.partition(list, fn(x) -> x == 9 end) |> (fn({nines, orig}) -> [orig | nines] end).() |> List.flatten
end
end


iex(10)> Nine.main([1, 2, 3, 7, 4, 8, 3, 7, 4, 9, 3, 9, 9, 9, 3, 2, 5, 1, 0, 9, 8])
[1, 2, 3, 7, 4, 8, 3, 7, 4, 3, 3, 2, 5, 1, 0, 8, 9, 9, 9, 9, 9]

I've been stabbed by this bird so many god damn times

let sep n l =
let () f (a, b) = (f) a b in
(@) List.partition (() n) l

...

sep 9 [1;2;3;9;0]
=> [1;2;3;0;0]

GET A BOOK AND LEARN TO CODE

[1;2;3;0;0] should read [1;2;3;0;9]

Oh wow. Thats fucking beautiful. Executable pseudocode wins again.

>swap
wouldn't that ruin the relative order of the other elements?
so for example if you have
1241951212
you should get
1231512129

with swap you get
1231251219
or not
?

See

Ahh, sorry!
I just found out I messed it up, because if always push values at the new array, I reverse the order of all "non 9" elements..

Here is a new solution, it's even more readable, but I'm not sure about the speed.

[1,2,9,5,9,2,9,4].partition{ |i| i!=9 }.flatten

This solution is very close to the question (logical wise), but it might be a tad slower than this: Because "partition" returns a two-dimensional array (all "true" values at the first slot, all "false" values at the second slot).

"Flatten" take this two-dimensional array and puts is into a new one (one-dimensional).


To be honest, Ruby isn't made for superoptimized code, but for Readability while being "reasonable fast".


If I really think this sorting would be a bottle neck in my code, I would rather use C for this specific task:
>amberbit.com/blog/2014/6/12/calling-c-cpp-from-ruby/

Typical haskell program
{-# LANGUAGE BangPatterns #-}

(?????)=0
(????????) = null
(?????????) = replicate
(??????????) = 9
(???????????) = head
(????????????) = (==)
(?????????????) = (+)
(??????????????) = tail
(???????????????) = (:)
(????????????????) = 1

(??????)(?)(??)(???)=if(?)then(??)else(???)
(?)(??)=(??)((?)(??))
(???)=(????)(?????)
(????)=(?)(???????)
(???????)(?)(??)(???)=(??????)((????????)(???))((?????????)(??)(??????????))((??????) ((????????????)(??????????)((???????????)(???)))((?) ((?????????????)(??)(????????????????))((??????????????)(???)))((???????????????) ((?\
??????????)(???)) ((?) (??) ((??????????????)(???)))))


Output:
λ> (???) [1,2,4,5,9,9,94,3,345,9,534]
[1,2,4,5,94,3,345,534,9,9,9]

messed up copying, should be
{-# LANGUAGE BangPatterns #-}

(?????)=0
(????????) = null
(?????????) = replicate
(??????????) = 9
(???????????) = head
(????????????) = (==)
(?????????????) = (+)
(??????????????) = tail
(???????????????) = (:)
(????????????????) = 1

(??????)(?)(??)(???)=if(?)then(??)else(???)
(?)(??)=(??)((?)(??))
(???)=(????)(?????)
(????)=(?)(???????)
(???????)(?)(??)(???)=(??????)((????????)(???))((?????????)(??)(??????????))((??????) ((????????????)(??????????)((???????????)(???)))((?) ((?????????????)(??)(????????????????))((??????????????)(???)))((???????????????) ((???????????)(???)) ((?) (??) ((??????????????)(???)))))

??
???
???
What?

Here is the assignment in Pajeet Java.

package stuff;

import java.util.Random;

public class ArraySortOnNine {

public int[] sortOnNine(int[] array) {
int nines = 0;
for (int i = 0; i < array.length; i++)
if (array[i] == 9)
nines++;
int[] nine = new int[nines];
int[] normal = new int[array.length - nines];

int j = 0;
for (int i = 0; i < array.length; i++)
if (array[i] != 9)
normal[j++] = array[i];

for (int i = 0; i < nine.length; i++)
nine[i] = 9;

return concat(normal, nine);
}

public void printArray(int[] test) {
for (int i = 0; i < test.length; i++) {
System.out.print(test[i] + ",");
}
System.out.println("");
}

public void arraySwap(int index1, int index2, int[] array) {
int temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}

public int[] concat(int[] a, int[] b) {
int aLen = a.length;
int bLen = b.length;
int[] c = new int[aLen + bLen];
System.arraycopy(a, 0, c, 0, aLen);
System.arraycopy(b, 0, c, aLen, bLen);
return c;
}
}


It can do 100 000 arrays with length 10 in about 5000 millis.

>all these shitty solutions taking linear extra space
>some of them actually taking quadratic time
Sup Forums is full of Pajeets as expected

The haskell one posted earlier actually does not. The cons cells of the input list become garbage at the same time as new ones are constructed for the output, so it runs in constant space.

You're supposed to modify the input, not return a new one, so the Haskell version is already disqualified.

Ok, instead you can take as input a mutable cell (IORef) containing the list to be modified. Then use the described algorithm and put it in the IORef.

Before doing that you need to put a dummy value in the IORef so that it doesn't keep references to the old list.

#include
#include

void swap(int* array, int left, int right) {
int temp = array[left];
array[left] = array[right];
array[right] = temp;
}

void sortnine(int* array, int size) {
int left = 0;
int right = size - 1;

while(1) {
while(left < size && array[left] != 9) {
left++;
}
while(right >= 0 && array[right] == 9) {
right--;
}
if(left >= right) {
break;
}
swap(array, left, right);
}
}

void printarray(int* array, int size) {
int n;

for(n = 0; n < size; n++) {
printf("%d ", array[n]);
}
}

int main(void) {
int array[] = {3,9,4,5,1,1,1,9,0,7};
int size = sizeof(array) / sizeof(int);

sortnine(array, size);
printarray(array, size);

return EXIT_SUCCESS;
}

Super fast C master race

What the hell do those symbols even mean? Lisp is indecipherable.

That will take linear space.

>I don't know common procedures and keywords from this language's standard library, therefore nobody can understand it

No it won't, for the same reasons as the algorithm that doesn't use an IORef

>You have to memorize dozens of obtuse abbreviations and syntax (or lack thereof) before you can make any sense of the code, because fuck readability amirite?

function arrayshit(arr) {
var arr_9 = [];
var arr_new = [];
for(var i=0; i

>while keeping the relative order of the other elements
welp I guess you're dead

/thread

function toTheBack(arr){
let filtered = arr.filter(num => num != 9);
for(var counter= filtered.length; counter < arr.length; counter++)
filtered.push(9)
return filtered;
}


First filters out the 9s, then checks the diff in size(i.e. how many 9s have been filtered out), then adds that amount of 9s to the end.

*(!=9)

[USER WAS STABBED FOR THIS POST]

Slow
Using var++ and not ++var
Also run it 100000000 and show time

NO PLEASE I DIDN'T REA-

//Method call
pushToEnd(array, 9);

// Method
public static void pushToEnd(int[] array, int target) {
int count = 0;

for(int i = 0; i < array.length; i++) {
if(array[i] != target) {
array[count++] = array[i];
}
}

while (count < array.length) {
array[count++] = target;
}
}

Probably should have just shown the whole thing like this in the first place:
public static void main(String[] args) {

int[] array = { 1, 2, 9, 3, 4, 9, 5, 6, 9, 7, 8 };

System.out.println("Order before:");
for(int i=0; i

def ninesToEnd(arr):

for i in range(len(arr)):
if arr[i] == 9:
arr.append(arr.pop(i))

return arr

Clojure:

(sort-by #(= % 9) input)

Why would u use anything else?

def put_nines_in_the_lines(a):
out_array = [el for el in a if el!=9]
out_array+=[9 for i in range(len(a) - len(out_array))]
return out_array

still don't know how to linecode

stand back

template
void move_to_end(Container &container, typename Container::value_type value)
{
const auto size = container.size();
for(typename Container::size_type i = 0; i < size; ++i)
{
if(container[i] != value) continue;

for(auto j = i + 1; j < size; ++j)
{
if(container[j] == value) continue;

std::swap(container[i], container[j]);
break;
}
}
}


0.284891s for 1M repetitions

void sort_nine(size_t n, int arr[static n])
{
int *ptr, *sav;

for (ptr = arr, sav = ptr; ptr < arr + n; ++ptr) {
if (*ptr != 9) {
*sav = *ptr;
++sav;
}
}

for (; sav < ptr; ++sav) {
*sav = 9;
}
}

pls no bird... I'm too cute to die :3 :3

My java implementation:

int[] result = new int[input.length];
int count =0;
int indexCount = 0;
for(int i=0;i

Let's did it:
N = 50
L = np.random.randint(0, 100, N)
L[np.random.choice(np.arange(N),int(0.2*N))] = 9
print(L)

nines_count = 0
i = 0
j = 0
while i < len(L) - nines_count and j < len(L):
if L[j] == 9:
nines_count += 1
elif j == 0 or j > i:
L[i] = L[j]
i += 1
j += 1
print(nines_count)
L[-nines_count:] = 9
print(L)

Doesn't that have horrible run time?

What? It's O(n).
Also, the cache locality is fine.

@echo off
Shutdown -s

...

see

SLOW

That's some fast shit
.5 for 100M repetitions

fuck......

idk what settlings ideone uses, ill give some results in a bit from my own machine

I am testing with -o3

alright on my own shitty 2.2GHz machine i get 0.109006s

def s(l:List[Int]) ={
l.filter(_ != 9) ++ l.filter(_ == 9)
}

void sortN(int *a, int aSize)
{
int i, j=aSize-1, k;

for (i = 0; i < j; i++)
if (a[i] == 9)
{
for (; a[j] == 9; j--);

for (k = i; k < j; k++)
a[k] = a[k+1];
a[j] = 9;
}
}

1.6 sec
How many runs and which algh is yours

test

1M runs of

int a;

So about 10 seconds for 100m runs. You can do better user.

>1.6 sec
Are you retarded?

Testing it on 100m runs

undoubtably, though i went by the "quick" in the op, so i didnt look too much into the problem outside of the naive solution

N-NO BIRDO I-I-I D-DON'T HOW PROGRAM

W-WAIT NO STOP NOO AAAAAAAAAHHH