Skip to content

Instantly share code, notes, and snippets.

@gboncoffee
Last active September 12, 2023 12:36
Show Gist options
  • Save gboncoffee/2ecfacb0bc7f03626e4c1d2e4d00253f to your computer and use it in GitHub Desktop.
Save gboncoffee/2ecfacb0bc7f03626e4c1d2e4d00253f to your computer and use it in GitHub Desktop.
Collection of stupid sorting algorithms.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#define printarr(v, n) { \
for (int i = 0; i < n - 1; i++) \
printf("%d ", v[i]); \
printf("%d\n", v[n - 1]); \
}
/*
* Here's a list of stupid sorting algorithms made for a blogpost
*
* https://gboncoffee.github.io/posts/stupid_sorting.html
*/
/*
* Copyright (C) 2023 Gabriel de Brito
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
int sorted(int *v, int n)
{
for (int i = 1; i < n; i++)
if (v[i] < v[i - 1])
return 0;
return 1;
}
/* Sorts the array by removing unsorted itens. Returns the final size. */
int kill_sort(int *v, int n)
{
int pad = 0;
for (int i = 1; i < n; i++) {
if (v[i] < v[i - 1 - pad])
pad++;
else
v[i - pad] = v[i];
}
return n - pad;
}
/* Sorts the array by removing a random half of it until it's sorted. Returns
* the final array pointer and sets n to it's size. */
int* thanos_sort(int *v, int *n)
{
srand(time(NULL));
while (*n > 1 && !sorted(v, *n)) {
if (random() % 2) {
v += *n / 2;
}
*n = *n / 2;
}
return v;
}
/* Sorts the array by randomizing a sequence and them checking if it's
* sorted. */
void random_sort(int *v, int n)
{
int x, y, tmp;
srand(time(NULL));
while (!sorted(v, n)) {
for (int i = 0; i < n; i++) {
x = random() % n;
y = random() % n;
tmp = v[x];
v[x] = v[y];
v[y] = tmp;
}
}
}
/* Sorts the array by the following principle:
* - There's n! chance of the array being sorted;
* - So it must have been sorted by an intelligent designer. */
void intelligent_design_sort(int *v, int n)
{
(void) v;
(void) n;
}
/* Sorts the array by waiting and hoping cosmic rays are going to flip the
* memory so the array get's sorted. */
void miracle_sort(int *v, int n)
{
while (!sorted(v, n))
sleep(5);
}
/* It's not sorted nor unsorted until you check it. */
int schrodinger_sort(int *v, int n)
{
return sorted(v, n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment