Skip to content

Commit

Permalink
Add quadratic sorts
Browse files Browse the repository at this point in the history
  • Loading branch information
ishanpranav committed Feb 1, 2024
1 parent 79b9be0 commit 37eac0c
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 15 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ miller_rabin_primality_test$(O): \
lib/primality_tests/miller_rabin_primality_test.c
$(CC) $(CFLAGS) -c $< -o $@

bubble_sort$(O): lib/sorts/bubble_sort.c lib/sort.h
$(CC) $(CFLAGS) -c $< -o $@

insertion_sort$(O): lib/sorts/insertion_sort.c lib/sort.h
$(CC) $(CFLAGS) -c $< -o $@

selection_sort$(O): lib/sorts/selection_sort.c lib/sort.h
$(CC) $(CFLAGS) -c $< -o $@

Expand Down Expand Up @@ -154,8 +160,8 @@ id0020$(E): src/id0020.c euler$(O) string$(O) series$(O)
id0021$(E): src/id0021.c euler$(O) factor_iterator$(O) $(SIEVE_O)
$(CC) $(CFLAGS) $< -o $@ euler$(O) factor_iterator$(O) $(SIEVE_O) $(LM)

id0022$(E): src/id0022.c selection_sort$(O) euler$(O) string$(O) $(STRING_COLLECTION_O)
$(CC) $(CFLAGS) $< -o $@ selection_sort$(O) euler$(O) string$(O) $(STRING_COLLECTION_O)
id0022$(E): src/id0022.c insertion_sort$(O) selection_sort$(O) bubble_sort$(O) euler$(O) string$(O) $(STRING_COLLECTION_O)
$(CC) $(CFLAGS) $< -o $@ insertion_sort$(O) selection_sort$(O) bubble_sort$(O) euler$(O) string$(O) $(STRING_COLLECTION_O)

id0023$(E): src/id0023.c euler$(O) factor_iterator$(O) $(SIEVE_O)
$(CC) $(CFLAGS) $< -o $@ euler$(O) factor_iterator$(O) $(SIEVE_O) $(LM)
Expand Down
1 change: 1 addition & 0 deletions lib/_/dummy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Licensed under the MIT License.
5 changes: 5 additions & 0 deletions lib/euler.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ long gcd(long a, long b)

void swap(Object left, Object right, size_t size)
{
if (left == right)
{
return;
}

char* p = left;
char* q = right;
char swap;
Expand Down
26 changes: 26 additions & 0 deletions lib/sort.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ typedef void (*Sort)(
size_t itemSize,
Comparer itemComparer);

/**
*
* @param items
* @param count
* @param itemSize
* @param itemComparer
*/
void bubble_sort(
Array items,
size_t count,
size_t itemSize,
Comparer itemComparer);

/**
*
* @param items
Expand All @@ -23,3 +36,16 @@ void selection_sort(
size_t count,
size_t itemSize,
Comparer itemComparer);

/**
*
* @param items
* @param count
* @param itemSize
* @param itemComparer
*/
void insertion_sort(
Array items,
size_t count,
size_t itemSize,
Comparer itemComparer);
26 changes: 26 additions & 0 deletions lib/sorts/bubble_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed under the MIT License.

#include "../sort.h"
#include "../swap.h"

void bubble_sort(
Array items,
size_t count,
size_t itemSize,
Comparer itemComparer)
{
char* begin = items;

for (size_t i = 0; i < count; i++)
{
for (size_t j = 0; j < count - i - 1; j++)
{
char* q = begin + j * itemSize;

if (itemComparer(q, q + itemSize) > 0)
{
swap(q, q + itemSize, itemSize);
}
}
}
}
46 changes: 46 additions & 0 deletions lib/sorts/insertion_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Licensed under the MIT License.

#include <stdlib.h>
#include <string.h>
#include "../sort.h"

void insertion_sort(
Array items,
size_t count,
size_t itemSize,
Comparer itemComparer)
{
char* begin = items;
void* swap = malloc(itemSize);

if (!swap)
{
selection_sort(items, count, itemSize, itemComparer);

return;
}

for (size_t i = 1; i < count; i++)
{
char* p = begin + i * itemSize;
size_t j = i;

while (j-- > 0)
{
if (itemComparer(p, begin + j * itemSize) >= 0)
{
break;
}
}

j++;

char* q = begin + j * itemSize;

memcpy(swap, p, itemSize);
memmove(q + itemSize, q, (i - j) * itemSize);
memcpy(q, swap, itemSize);
}

free(swap);
}
11 changes: 4 additions & 7 deletions lib/sorts/selection_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include "../swap.h"

void selection_sort(
Array items,
size_t count,
size_t itemSize,
Array items,
size_t count,
size_t itemSize,
Comparer itemComparer)
{
char* begin = items;
Expand All @@ -23,9 +23,6 @@ void selection_sort(
}
}

if (min != i)
{
swap(begin + min * itemSize, begin + i * itemSize, itemSize);
}
swap(begin + min * itemSize, begin + i * itemSize, itemSize);
}
}
17 changes: 11 additions & 6 deletions src/id0022.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,37 @@
#include "../lib/string_collection.h"
#include "../lib/sort.h"

struct LongString {
char* value;
int extrabits;
};

int main(void)
{
long sum = 0;
struct List names;
clock_t start = clock();

euler_ok(list(&names, sizeof(String), 5000));
euler_ok(list(&names, sizeof(struct LongString), 5000));
euler_ok(string_collection_deserialize(&names, stdin));

selection_sort(names.items, names.count, names.itemSize, string_comparer);
// list_sort(&names, string_comparer);
bubble_sort(names.items, names.count, names.itemSize, string_comparer);
//list_sort(&names, string_comparer);

String* begin = names.items;
struct LongString* begin = names.items;

for (size_t i = 0; i < names.count; i++)
{
int rank = 0;

for (char* p = begin[i]; *p; p++)
for (char* p = begin[i].value; *p; p++)
{
rank += *p - 'A' + 1;
}

sum += rank * (i + 1);

free(begin[i]);
free(begin[i].value);
}

finalize_list(&names);
Expand Down

0 comments on commit 37eac0c

Please sign in to comment.