Compare commits

...

5 Commits

Author SHA1 Message Date
NaiJi ✨ ca75859311 6 i am retarded 2024-04-22 09:51:59 +04:00
NaiJi ✨ 734fea77a6 6 remove unneded variable 2024-04-22 09:50:15 +04:00
NaiJi ✨ fe9e21048f 5 comment 2024-04-22 09:47:03 +04:00
NaiJi ✨ fb847aa4bd 6 2024-04-22 09:42:40 +04:00
NaiJi ✨ ee8d34d954 5 2024-04-22 08:59:24 +04:00
2 changed files with 91 additions and 0 deletions

View File

@ -0,0 +1,45 @@
#include <math.h>
#include <stdio.h>
// complexity is O(sqrt(N))
// well we need ot find an exact moment,
// let's say "height" at which our crystal
// balls break, for that we only have 2 balls,
// so we can't do binary search, since the range
// is tooooo huge, therefore we run a linear
// growth bigger than N, but small enough to find where
// the first ball breaks, then we take the interval
// between: [the previous non-break] and [where it broken]
// and run a normal linear search
// therefore we run sqrt(n) + sqrt(n) times
// so it's O(sqrt(N)
#define SIZE 999
int two_crystal_balls_search(int array[], int size, int value)
{
int i = 0;
int step = sqrt((double)(size));
for (; i < size; i = i + step)
if (array[i] >= value)
break;
printf("Step is: %d\n", step);
printf("The first one broke at: %d\n", i);
i = i - step;
for (; i < i + step; ++i)
if (array[i] >= value)
return i;
return -1;
}
int main()
{
int array[SIZE];
for (int i = 0; i < SIZE; ++i)
array[i] = i;
printf("The balls break at: %d\n", two_crystal_balls_search(array, SIZE, 567));
}

View File

@ -0,0 +1,46 @@
#include <stdio.h>
#define SIZE 10
// complexity is O(n^2)
// how is that calculated?
// well we run two loops, even if the first one is N
// the second one is first N, then N - 1, then N - 2, ...
// all the way down to N - N + 1
// what's the sum of them?
// imagine sum of all form 1 to 100
// okay, 1 + 100 = 101
// okay, 2 + 99 = 101
// etc all the way down to 50 and 51,
// so we have 50 times 101 to calculate sum of 1 to 100
// which means:
// 101 * 50 or
// (N+1) * N/2 or
// N^2 + N which makes
// O(N^2 + N) then we drop the non-significant + N
// O(N^2)
void bubble_sort(int array[], int size)
{
for (int i = 0; i < size; ++i)
{
for (int j = 1; j < size - i; ++j)
{
if (array[j] < array[j-1])
{
int temp = array[j];
array[j] = array[j-1];
array[j-1] = temp;
}
}
}
}
int main()
{
int array[SIZE] = { 5, 7, 3, 2, 11, 16, 1, 4, 3, 2 };
bubble_sort(array, SIZE);
for (int i = 0; i < SIZE; ++i)
printf("%d ", array[i]);
printf("\n");
}