A very small sort in C

Tiniest sort in CI came across this article on efforts to make the smallest sort function in C.  Being cynical I did wonder if it would compile and run. I mean 56 bytes is kind of short!

To get it to compile in VS 2019 I had to add a couple of changes, mainly ints and the void. This is it wrapped in a program. The screenshot shows it running and the output at the bottom. Very clever!

The sort function is that void s(… at the top.

 

#include <stdio.h>

void s(int *a, int n) { n-- > 1 ? s(a, n), s(a + 1, n), 
     n = *a, * a = a[1], a[n > * a] = n : 0; }

int arr[10] = { 9,1,2,6,8,11,45,-1,6,4 };
int main() {
	s(arr, 10);
	for (int i = 0; i < 10; i++) {
		printf("%d ", arr[i]);
	}
	printf("\n");
	return 0;
}

What I didn’t show you is the number of times Windows defender went ape and declared it a trojan, every time it compiled! My PC is kept pretty bug free and this is a false positive…

A trojan

Leave a Reply

Your email address will not be published. Required fields are marked *