add python stuff
This commit is contained in:
parent
52dd7164c5
commit
c1614ed7d7
|
@ -32,6 +32,7 @@ void array_append(i32_ArrayList* s, int v) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// takes the address of a pointer
|
||||||
void resize_arraylist(i32_ArrayList** arr) {
|
void resize_arraylist(i32_ArrayList** arr) {
|
||||||
int new_size = (*arr)->capacity * 2;
|
int new_size = (*arr)->capacity * 2;
|
||||||
i32_ArrayList* new_arr = realloc((*arr), (sizeof(int) * new_size) + sizeof(i32_ArrayList));
|
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) {
|
if (arr->index == arr->capacity) {
|
||||||
// lets just double the capacity
|
// lets just double the capacity
|
||||||
resize_arraylist(&arr);
|
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);
|
array_append(arr, v);
|
||||||
|
printf("After append: index = %d\n", arr->index);
|
||||||
}
|
}
|
||||||
|
|
||||||
// create an array list and fill in with values from array
|
// 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
|
// lets implement v2 versions of these function that will grow
|
||||||
// the array when required
|
// the array when required
|
||||||
|
printf("calling the version 2 of the append function\n");
|
||||||
array_append2(a, 5656);
|
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);
|
print_array_list(a);
|
||||||
|
|
||||||
// array_append(a, 14);
|
// array_append(a, 14);
|
||||||
|
|
|
@ -1,25 +1,40 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
// asume the array is sorted of course
|
|
||||||
void bubble_sort(int arr[], int len) {
|
void bubble_sort(int arr[], int len) {
|
||||||
|
int total_iterations = 0;
|
||||||
int temp;
|
int temp;
|
||||||
|
int total_swaps = 1;
|
||||||
for (int i = 0; i < len; i++) {
|
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++) {
|
for (int j = 0; j < len - 1; j++) {
|
||||||
if (arr[j] > arr[j + 1]) {
|
if (arr[j] > arr[j + 1]) {
|
||||||
|
total_swaps++;
|
||||||
temp = arr[j + 1];
|
temp = arr[j + 1];
|
||||||
arr[j + 1] = arr[j];
|
arr[j + 1] = arr[j];
|
||||||
arr[j] = temp;
|
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 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);
|
bubble_sort(arr, 5);
|
||||||
|
printf("after the sort:\n");
|
||||||
for (int i = 0; i < 5; i++) {
|
print_array(arr, 5);
|
||||||
printf("%d ", arr[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
}
|
|
@ -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)
|
Loading…
Reference in New Issue