This is week one of the programming golf contests. The rules and other information are posted here: Weekly Programming Contest

This weeks challenge: is to write a method that takes in a non-empty primitive integer array and returns an int array with the numbers in order from lowest to highest.

public static int[]s(int[]n){for(int i=1,p,a,b;i<n.length;)for(p=i++;p>0;){a=n[p--];b=n[p];a=a<b?(n[p+1]=b)+(n[p]=a):0;}return n;}

Character count: 130

Tested using random number generator making int arrays of 10000 positive/negative integers. Tested using comparison to output of Arrays.sort. Ran test 1000 times, 100% success

Based off an insert sort I wrote ages ago in VB.

Hereās the expanded version:

public static int[] sort(int[] array) {
for (int iUp = 1; iUp < array.length; iUp++) {
for (int iDown = iUp; iDown > 0; iDown--) {
int thisNum = array[iDown];
int prevNum = array[iDown - 1];
if (thisNum < prevNum) {
array[iDown] = prevNum;
array[iDown - 1] = thisNum;
}
}
}
return array;
}

*Edited some derp extra characters
*Edit again; add expanded version
*Edit now I know we can modify the input array, reduce code further

You are allowed to modify the array. The only thing that matters is that a sorted version of the array is returned. I updated the thread to reflect this.

Iāll see what I can do with good old standard C? Nah, gonna try python bubble sort first.

My first entry with a codegolf. 159 characters for a simple bubble sort in python. More characters than @Lemonous, but I used a different concept and perhaps it could have been more optimized.

def s(a):
n=1
while n:
n=0
for i in range(0,len(a)-1):
if a[i]>a[i+1]:a[i],a[i+1],n=a[i+1],a[i],1
return a

yes, is based off of pseudocode stolen from wikipedia, but it works and I understand it ;-;. Hold up, though. Over the weekend I may try a completely different approach using a complete different sorting function, thatās perhaps a bit more advanced than bubble.

EDIT: removed unnecessary spaces and changed whitespace from 4 spaces to a tab. Reduced to 122 characters.

Thanks! Tomorrow Iāll try a java solution using a completely different sorting method and recursion (speed and memory consumption donāt matter here). Do I have to take into account negative numbers? Because, if not, I may be able to breach the 100 character line.

Copied an ugly āprocedureā from Pascal(Delphi) demos:

procedure sort(l,r: longint);
var
i,j,x,y: longint;
begin
i:=l;
j:=r;
x:=a[(l+r) div 2];
repeat
while a[i]<x do
inc(i);
while x<a[j] do
dec(j);
if not(i>j) then
begin
y:=a[i];
a[i]:=a[j];
a[j]:=y;
inc(i);
j:=j-1;
end;
until i>j;
if l<j then
sort(l,j);
if i<r then
sort(i,r);
end;

This is one of the most efficient sorts. (might be similar to TimSort in Java) It is using recursion.
Java Version: (Hand written, might not compile)

static void sort(int[] input) {
sort(input, 1, input.length);
}
static void sort(int[] input, int start, int end) {
int left = start;
int right = end;
int mid = input[(left + right) / 2];
do {
while (input[left] < mid) {
left ++;
}
while (input[right] > mid) {
right --;
}
if (left <= right) {
int t = input[left];
input[left] = input[right];
input[right] = t;
left ++;
right --;
}
} while (left <= right);
if (start < right) {
sort(input, start, right);
}
if (left < end) {
sort(input, left, end);
}
}

Like this much more than bubble. Bubble is hard to write and not efficient, so I never use it.
This sort is more often used in programmings for olympics. (algorithm)