commit d7cccb791a837bf9a21e404d9c435947c080edb2 Author: Piotr Kozak Date: Wed Apr 16 17:16:08 2025 +0200 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0c19cf9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +benchmarks/** diff --git a/benchmark.py b/benchmark.py new file mode 100644 index 0000000..4f6b9a7 --- /dev/null +++ b/benchmark.py @@ -0,0 +1,69 @@ +import subprocess +from random import randint +from time import time, sleep +import matplotlib.pyplot as plt + +def run_benchmark(program_name, array): + start = time() + process = subprocess.Popen(program_name, stdin=subprocess.PIPE) + process.communicate((f"{len(array)}\n" + "\n".join(map(str, array))).encode()) + end = time() + return end - start + +def generate_sequence(sequence_type, array_size): + if sequence_type == "random": + return [randint(0, 100) for i in range(array_size)] + elif sequence_type == "sorted": + return [i for i in range(array_size)] + elif sequence_type == "reversed": + return [i for i in range(array_size, 0, -1)] + elif sequence_type == "constant": + return [5 for i in range(array_size)] + elif sequence_type == "v_shaped": + return [i for i in range(array_size//2, 0, -1)]+[i for i in range(array_size//2)] + else: + raise ValueError("Invalid sequence type") + +def plot(program_name, sequences, array_size): + plt.figure(figsize=(10, 6)) + for sequence_type, timings in sequences.items(): + plt.plot(array_size, timings, label=sequence_type) + plt.xlabel("") + plt.ylabel("Czas (ms)") + plt.title(f"{program_name}") + plt.legend() + plt.savefig(f"benchmarks/{program_name}.png") + # plt.show() + +if __name__ == "__main__": + program_names = ["./insertion", "./selection"] + sequence_types = ["random", "sorted", "reversed", "constant", "v_shaped"] + array_sizes = [i for i in range(1000, 15001, 1000)] + + with open("results.txt", "w") as f: + for program_name in program_names: + sequenceTiming = { + "random": [], + "sorted": [], + "reversed": [], + "constant": [], + "v_shaped": [] + } + print(f"Running {program_name}") + for sequence_type in sequence_types: + print(f" - running {sequence_type}") + for array_size in array_sizes: + sequence = generate_sequence(sequence_type, array_size) + print(f" - running {sequence_type} with size {array_size}") + durations = [] + for i in range(10): + duration = run_benchmark(program_name, sequence) + duration *= 1000 + durations.append(duration) + sleep(0.1) + duration = sum(durations) / len(durations) + f.write(f"{program_name} {sequence_type} {array_size} {duration}\n") + sequenceTiming[sequence_type].append(duration) + sleep(0.5) + plot(program_name, sequenceTiming, array_sizes) + print("Benchmark finished") diff --git a/heapsort b/heapsort new file mode 100755 index 0000000..df90b1e Binary files /dev/null and b/heapsort differ diff --git a/heapsort.c b/heapsort.c new file mode 100644 index 0000000..16fd0c2 --- /dev/null +++ b/heapsort.c @@ -0,0 +1,76 @@ +#include +#include + +void Heapify(int A[], int i, int heapsize) { + int left = 2 * i +1; + int right = (2 * i) + 2; + int largest = i; + + if (left <= heapsize && A[left] > A[largest]) { + largest = left; + } else { + largest = i; + } + + if (right <= heapsize && A[right] > A[largest]) { + largest = right; + } + + if (largest != i) { + int temp = A[largest]; + A[largest] = A[i]; + A[i] = temp; + Heapify(A, largest, heapsize); + } +} + +int main() { + int size; + int res = scanf("%d", &size); + if (res != 1) { + fprintf(stderr, "Error reading size\n"); + return 1; + } + int *array = malloc(sizeof(int) * size); + for (int i = 0; i < size; i++) { + res = scanf("%d", &array[i]); + if (res != 1) { + fprintf(stderr, "Error reading array element\n"); + free(array); + return 1; + } + } + + printf("pre heap:"); + for (int i = 0; i < size; i++) { + printf("%d ", array[i]); + } + + // Build the heap + for (int i = size / 2 - 1; i >= 0; i--) { + Heapify(array, i, size); + } + + printf("\npost heap:"); + for (int i = 0; i < size; i++) { + printf("%d ", array[i]); + } + + int heapsize = size; + for (int i = size - 1; i >= 0; i--) { + int temp = array[i]; + array[i] = array[1]; + array[1] = temp; + heapsize--; + Heapify(array, i, heapsize); + } + + printf("\npost sort:"); + + for (int i = 0; i < size; i++) { + printf("%d ", array[i]); + } + + free(array); + return 0; +} diff --git a/insertion b/insertion new file mode 100755 index 0000000..6135dce Binary files /dev/null and b/insertion differ diff --git a/insertion.c b/insertion.c new file mode 100644 index 0000000..95c834a --- /dev/null +++ b/insertion.c @@ -0,0 +1,25 @@ +#include +#include + +// Capture the pipe of array contents, first number is the size +int main() { + int s; + scanf("%d", &s); + int *array = malloc(sizeof(int) * s); + for (int i = 0; i < s; i++) { + scanf("%d", &array[i]); + } + + for (int j = 1; j < s; j++) { + int key = array[j]; + int i = j - 1; + while (i >= 0 && array[i] > key) { + array[i + 1] = array[i]; + i -= 1; + } + array[i + 1] = key; + } + + free(array); + return 0; +} diff --git a/insertion_benchmark.png b/insertion_benchmark.png new file mode 100644 index 0000000..3d99f95 Binary files /dev/null and b/insertion_benchmark.png differ diff --git a/results.txt b/results.txt new file mode 100644 index 0000000..50d2643 --- /dev/null +++ b/results.txt @@ -0,0 +1,150 @@ +./insertion random 1000 1.0615110397338867 +./insertion random 2000 1.5358448028564453 +./insertion random 3000 2.274918556213379 +./insertion random 4000 3.3971309661865234 +./insertion random 5000 4.603886604309082 +./insertion random 6000 6.020760536193848 +./insertion random 7000 7.8598737716674805 +./insertion random 8000 9.824132919311523 +./insertion random 9000 12.162041664123535 +./insertion random 10000 14.610838890075684 +./insertion random 11000 17.46840476989746 +./insertion random 12000 20.206165313720703 +./insertion random 13000 23.66960048675537 +./insertion random 14000 27.488040924072266 +./insertion random 15000 30.75859546661377 +./insertion sorted 1000 0.8951187133789062 +./insertion sorted 2000 0.9664773941040039 +./insertion sorted 3000 1.1182308197021484 +./insertion sorted 4000 1.237630844116211 +./insertion sorted 5000 1.397252082824707 +./insertion sorted 6000 1.5844106674194336 +./insertion sorted 7000 1.7046451568603516 +./insertion sorted 8000 1.8734216690063477 +./insertion sorted 9000 2.349853515625 +./insertion sorted 10000 2.4387359619140625 +./insertion sorted 11000 2.462148666381836 +./insertion sorted 12000 2.5963783264160156 +./insertion sorted 13000 3.156876564025879 +./insertion sorted 14000 3.132033348083496 +./insertion sorted 15000 3.238677978515625 +./insertion reversed 1000 1.2768268585205078 +./insertion reversed 2000 2.137279510498047 +./insertion reversed 3000 3.491377830505371 +./insertion reversed 4000 5.584287643432617 +./insertion reversed 5000 7.988524436950684 +./insertion reversed 6000 10.77868938446045 +./insertion reversed 7000 14.134597778320312 +./insertion reversed 8000 18.278932571411133 +./insertion reversed 9000 22.71718978881836 +./insertion reversed 10000 27.739286422729492 +./insertion reversed 11000 33.2233190536499 +./insertion reversed 12000 39.423227310180664 +./insertion reversed 13000 45.474839210510254 +./insertion reversed 14000 52.727437019348145 +./insertion reversed 15000 60.09998321533203 +./insertion constant 1000 0.9403467178344727 +./insertion constant 2000 0.9927988052368164 +./insertion constant 3000 1.1275291442871094 +./insertion constant 4000 1.2822389602661133 +./insertion constant 5000 1.5305280685424805 +./insertion constant 6000 1.5430212020874023 +./insertion constant 7000 1.8325090408325195 +./insertion constant 8000 1.804804801940918 +./insertion constant 9000 2.2058486938476562 +./insertion constant 10000 2.1335840225219727 +./insertion constant 11000 2.322697639465332 +./insertion constant 12000 2.5031328201293945 +./insertion constant 13000 2.8443336486816406 +./insertion constant 14000 2.675914764404297 +./insertion constant 15000 2.9070377349853516 +./insertion v_shaped 1000 1.075124740600586 +./insertion v_shaped 2000 1.6106843948364258 +./insertion v_shaped 3000 2.419257164001465 +./insertion v_shaped 4000 3.438878059387207 +./insertion v_shaped 5000 4.725170135498047 +./insertion v_shaped 6000 6.513190269470215 +./insertion v_shaped 7000 7.992100715637207 +./insertion v_shaped 8000 10.184979438781738 +./insertion v_shaped 9000 12.364602088928223 +./insertion v_shaped 10000 15.029740333557129 +./insertion v_shaped 11000 18.083977699279785 +./insertion v_shaped 12000 21.661686897277832 +./insertion v_shaped 13000 24.31178092956543 +./insertion v_shaped 14000 27.93755531311035 +./insertion v_shaped 15000 32.60822296142578 +./selection random 1000 1.4104366302490234 +./selection random 2000 2.2107839584350586 +./selection random 3000 3.5886764526367188 +./selection random 4000 5.592441558837891 +./selection random 5000 7.922983169555664 +./selection random 6000 11.436033248901367 +./selection random 7000 14.230155944824219 +./selection random 8000 18.193602561950684 +./selection random 9000 22.896504402160645 +./selection random 10000 29.131484031677246 +./selection random 11000 33.37256908416748 +./selection random 12000 39.19968605041504 +./selection random 13000 47.031211853027344 +./selection random 14000 53.545570373535156 +./selection random 15000 61.37092113494873 +./selection sorted 1000 1.2252330780029297 +./selection sorted 2000 2.236461639404297 +./selection sorted 3000 3.660869598388672 +./selection sorted 4000 5.638456344604492 +./selection sorted 5000 8.042407035827637 +./selection sorted 6000 11.208224296569824 +./selection sorted 7000 14.447546005249023 +./selection sorted 8000 18.74523162841797 +./selection sorted 9000 22.982478141784668 +./selection sorted 10000 28.246402740478516 +./selection sorted 11000 34.0437650680542 +./selection sorted 12000 39.82250690460205 +./selection sorted 13000 46.31156921386719 +./selection sorted 14000 53.86967658996582 +./selection sorted 15000 61.89534664154053 +./selection reversed 1000 1.210784912109375 +./selection reversed 2000 2.1828174591064453 +./selection reversed 3000 3.7517547607421875 +./selection reversed 4000 5.427956581115723 +./selection reversed 5000 8.042573928833008 +./selection reversed 6000 10.962772369384766 +./selection reversed 7000 14.444756507873535 +./selection reversed 8000 18.53172779083252 +./selection reversed 9000 22.98440933227539 +./selection reversed 10000 28.051161766052246 +./selection reversed 11000 33.19251537322998 +./selection reversed 12000 39.997243881225586 +./selection reversed 13000 45.667243003845215 +./selection reversed 14000 53.35855484008789 +./selection reversed 15000 60.373663902282715 +./selection constant 1000 1.2602806091308594 +./selection constant 2000 2.1334409713745117 +./selection constant 3000 3.5085439682006836 +./selection constant 4000 5.34212589263916 +./selection constant 5000 7.747673988342285 +./selection constant 6000 10.555744171142578 +./selection constant 7000 14.265799522399902 +./selection constant 8000 18.23742389678955 +./selection constant 9000 22.236275672912598 +./selection constant 10000 27.255654335021973 +./selection constant 11000 32.57889747619629 +./selection constant 12000 38.65196704864502 +./selection constant 13000 45.708537101745605 +./selection constant 14000 51.95658206939697 +./selection constant 15000 59.54868793487549 +./selection v_shaped 1000 1.179647445678711 +./selection v_shaped 2000 2.0728111267089844 +./selection v_shaped 3000 3.5805463790893555 +./selection v_shaped 4000 5.700540542602539 +./selection v_shaped 5000 8.303594589233398 +./selection v_shaped 6000 10.987281799316406 +./selection v_shaped 7000 14.623737335205078 +./selection v_shaped 8000 18.853092193603516 +./selection v_shaped 9000 22.894835472106934 +./selection v_shaped 10000 27.98130512237549 +./selection v_shaped 11000 33.84420871734619 +./selection v_shaped 12000 39.37234878540039 +./selection v_shaped 13000 46.07586860656738 +./selection v_shaped 14000 53.040337562561035 +./selection v_shaped 15000 60.929179191589355 diff --git a/selection b/selection new file mode 100755 index 0000000..f3f1257 Binary files /dev/null and b/selection differ diff --git a/selection.c b/selection.c new file mode 100644 index 0000000..1499d9b --- /dev/null +++ b/selection.c @@ -0,0 +1,35 @@ +#include +#include + +int main() { + int s; + int res = scanf("%d", &s); + if (res != 1) { + fprintf(stderr, "Error reading size\n"); + return 1; + } + int *array = malloc(sizeof(int) * s); + for (int i = 0; i < s; i++) { + res = scanf("%d", &array[i]); + if (res != 1) { + fprintf(stderr, "Error reading array element\n"); + free(array); + return 1; + } + } + + for (int j = s; j > 1; j--) { + int max = j; + for (int i = j - 1; i >= 0; i--) { + if (array[i] > array[max]) { + max = i; + }; + }; + int temp = array[j]; + array[j] = array[max]; + array[max] = temp; + } + + free(array); + return 0; +} diff --git a/selection_benchmark.png b/selection_benchmark.png new file mode 100644 index 0000000..e297dc0 Binary files /dev/null and b/selection_benchmark.png differ