Compare commits

...

5 Commits

Author SHA1 Message Date
NaiJi ✨ f5c973dfe7 1.13 2024-04-22 00:30:40 +04:00
NaiJi ✨ 2a1c977954 4 2024-04-22 00:14:07 +04:00
NaiJi ✨ a8e225cbbb 3 2024-04-21 23:05:50 +04:00
NaiJi ✨ 16ed3d024a 2 2024-04-21 23:05:40 +04:00
NaiJi ✨ 60fd331394 1 2024-04-21 22:28:31 +04:00
5 changed files with 134 additions and 0 deletions

View File

@ -0,0 +1,32 @@
#include <stdio.h>
main()
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; ++i)
ndigit[i] = 0;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else if (c == ' ' || c == '\t')
++nwhite;
else if (c == '\n')
{
++nwhite;
printf("digits = ");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n", nwhite, nother);
}
else
++nother;
printf("digits = ");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n", nwhite, nother);
}

View File

@ -0,0 +1,8 @@
Important concepts
Big O is a way to categorize your algorithms time or memory requirements based on input.
- Growth is with respect to the input
- Constants are dropped
- Worst case is usually the way we measure

View File

@ -0,0 +1,17 @@
array [0][]
a + width * offset
contigious memory
```
int a[3];
[ jakdf ] [ adsfa ] [ vdsfv ]
a[1] = 4;
[ jakdf ] [ 4 ] [ vdsfv ]
```
O(1) operations, do not grow by input, we know all the positions and variables, so we just multiply

View File

@ -0,0 +1,33 @@
#include <stdio.h>
#define SIZE 55
// Search complexity is O(n)
// bc growths in respect to input, if array is 999
// in the worst case the search will take 999 iterations over the loop
// if array is 4, the worst case will take 4 loops
int linear_search(int array[], int size, int value)
{
for (int i = 0; i < size; ++i)
if (array[i] == value)
return 1;
return 0;
}
int main()
{
int array[SIZE];
for (int i = 0; i < SIZE; ++i)
array[i] = 0;
array[3] = 45;
int value = 45;
int is_found = linear_search(array, SIZE, value);
printf("1 if found: %d\n", is_found);
is_found = linear_search(array, SIZE, 99);
printf("1 if found: %d\n", is_found);
}

View File

@ -0,0 +1,44 @@
#include "stdio.h"
#define SIZE 4096
int binary_search(int array[], int size, int offset, int value)
{
int pivot = size / 2;
int current_value = array[offset + pivot];
printf("At %d\n", offset + pivot);
if (current_value == value)
{
return 1;
}
if (pivot > 0)
{
if (current_value > value)
return binary_search(array, pivot, offset, value);
else
return binary_search(array, pivot, offset + pivot, value);
}
return 0;
}
int main()
{
int array[SIZE];
for (int i = 0; i < SIZE; ++i)
array[i] = i;
printf("1 if found: %d\n", binary_search(array, SIZE, 0, 5243));
printf("1 if found: %d\n", binary_search(array, SIZE, 0, 1));
printf("1 if found: %d\n", binary_search(array, SIZE, 0, 1044));
// for (int i = 0; i < SIZE; ++i)
// if (!binary_search(array, SIZE, 0, i))
// printf("YOU FUCKED UP!!! at index %d\n", i);
printf("1 if found: %d\n", binary_search(array, SIZE, 0, SIZE));
array[14] = 13;
printf("1 if found: %d\n", binary_search(array, SIZE, 0, 14));
}