From c1614ed7d7786b27ad8b0e38b1b00f8144b0c290 Mon Sep 17 00:00:00 2001 From: ergz Date: Mon, 24 Jul 2023 22:12:34 -0700 Subject: [PATCH] add python stuff --- c/arraylist.c | 7 ++++++- c/bubble_sort.c | 35 +++++++++++++++++++++++++---------- python/bubble-sort.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 python/bubble-sort.py diff --git a/c/arraylist.c b/c/arraylist.c index 1e072b0..23430ad 100644 --- a/c/arraylist.c +++ b/c/arraylist.c @@ -32,6 +32,7 @@ void array_append(i32_ArrayList* s, int v) { } } +// takes the address of a pointer void resize_arraylist(i32_ArrayList** arr) { int new_size = (*arr)->capacity * 2; i32_ArrayList* new_arr = realloc((*arr), (sizeof(int) * new_size) + sizeof(i32_ArrayList)); @@ -49,10 +50,12 @@ void array_append2(i32_ArrayList* arr, int v) { if (arr->index == arr->capacity) { // lets just double the capacity resize_arraylist(&arr); - printf("size of arr: %d\n", arr->capacity); + printf("Resized: size of arr: %d\n", arr->capacity); } + printf("Before append: index = %d\n", arr->index); array_append(arr, v); + printf("After append: index = %d\n", arr->index); } // create an array list and fill in with values from array @@ -156,7 +159,9 @@ int main() { // lets implement v2 versions of these function that will grow // the array when required + printf("calling the version 2 of the append function\n"); array_append2(a, 5656); + printf("the size of the array is %d, and the cap is %d\n", a->index, a->capacity); print_array_list(a); // array_append(a, 14); diff --git a/c/bubble_sort.c b/c/bubble_sort.c index 16c7c69..75055e3 100644 --- a/c/bubble_sort.c +++ b/c/bubble_sort.c @@ -1,25 +1,40 @@ #include -// asume the array is sorted of course void bubble_sort(int arr[], int len) { + int total_iterations = 0; int temp; + int total_swaps = 1; for (int i = 0; i < len; i++) { + total_iterations++; + if (total_swaps == 0) { + break; + } + total_swaps = 0; for (int j = 0; j < len - 1; j++) { - if (arr[j] > arr[j+1]) { - temp = arr[j+1]; - arr[j+1] = arr[j]; + if (arr[j] > arr[j + 1]) { + total_swaps++; + temp = arr[j + 1]; + arr[j + 1] = arr[j]; arr[j] = temp; } } } + printf("total iterations: %d\n", total_iterations); +} + +void print_array(int* arr, int len) { + printf("[ "); + for (int i = 0; i < len; i++) { + printf("%d ", arr[i]); + } + printf("]\n"); } int main() { - int arr[5] = {1, 3, 2, 4, 5}; + int arr[5] = {2, 1, 3, 4, 5}; + printf("before the sort:\n"); + print_array(arr, 5); bubble_sort(arr, 5); - - for (int i = 0; i < 5; i++) { - printf("%d ", arr[i]); - } - printf("\n"); + printf("after the sort:\n"); + print_array(arr, 5); } \ No newline at end of file diff --git a/python/bubble-sort.py b/python/bubble-sort.py new file mode 100644 index 0000000..41f301b --- /dev/null +++ b/python/bubble-sort.py @@ -0,0 +1,35 @@ +from typing import List + + +def bubble_sort(arr: List[int]): + total_iterations = 0 + for i in range(len(arr)): + total_iterations += 1 + for j in range(len(arr) - 1): + if arr[j] > arr[j + 1]: + temp = arr[j] + arr[j] = arr[j + 1] + arr[j + 1] = temp + print(f"the total number of iterations: {total_iterations}") + + +def bubble_sort2(arr: List[int]): + total_swaps = 1 + total_iterations = 0 + for i in range(len(arr)): + if total_swaps == 0: + break + total_swaps = 0 + total_iterations += 1 + for j in range(len(arr) - 1): + if arr[j] > arr[j + 1]: + total_swaps += 1 + arr[j], arr[j + 1] = arr[j + 1], arr[j] + print(f"total iterations: {total_iterations}") + + +if __name__ == "__main__": + a = [2, 1, 3, 4, 5] + print(a) + bubble_sort2(a) + print(a)