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; } } }
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]
Isaac Robinson
Nigger what the fuck.
Austin Peterson
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
Bentley Bell
>Fast >Memory efficient YOU GONNA GET STABED
Michael Harris
Lmfao it's faster than the other solutions and there is plenty of memory nowdays
Benjamin Peterson
Sure, match me >there is plenty of memory nowdays How much memo an embedded controller does have?
Wyatt Cruz
About as much as you mom had in her head when you were conceived
Alexander Sullivan
Found the CS major
David Martin
# 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)
Angel Gomez
In Rust:
fn sort9(list: &mut [i64]) { list.sort_by_key(|&val| val == 9); }
Jason Smith
Happy to see a Perl solution, but I fixed it for you.
Here : eval unpack u=>q{_
Jack Murphy
Damn didn't think to use sort_by_key()... So wait how does this work exactly?
Jack Evans
>In Rust: > >fn sort9(list: &mut [i64]) { >list.sort_by_key(|&val| val == 9); >} Wait... won't this just move all 9's up?
Easton Price
Ah, I made a better version. Should also be faster ..
>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 ?
Daniel Cox
See
Isaiah Gray
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
Jonathan Clark
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".
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.
Jayden Lee
>all these shitty solutions taking linear extra space >some of them actually taking quadratic time Sup Forums is full of Pajeets as expected
Benjamin Roberts
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.
Isaiah Rogers
You're supposed to modify the input, not return a new one, so the Haskell version is already disqualified.
Lincoln Martinez
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.
Nathan Ramirez
#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;
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
Andrew Scott
What the hell do those symbols even mean? Lisp is indecipherable.
Josiah Miller
That will take linear space.
Ryder Phillips
>I don't know common procedures and keywords from this language's standard library, therefore nobody can understand it
Oliver Morales
No it won't, for the same reasons as the algorithm that doesn't use an IORef
William Turner
>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?
Juan Nelson
function arrayshit(arr) { var arr_9 = []; var arr_new = []; for(var i=0; i
Thomas Hughes
>while keeping the relative order of the other elements welp I guess you're dead
Alexander Sanchez
/thread
Aaron Gutierrez
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.
Jeremiah Sanders
*(!=9)
Anthony Wilson
[USER WAS STABBED FOR THIS POST]
Colton Jones
Slow Using var++ and not ++var Also run it 100000000 and show time
Chase Ortiz
NO PLEASE I DIDN'T REA-
Angel Ward
//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; } }
Justin Moore
Probably should have just shown the whole thing like this in the first place: public static void main(String[] args) {