Compare commits
No commits in common. "ca7585931115b436f932432c57956c7c5ac48528" and "488a8596b5a6f4a6535d0d9ad3f49592c121bed7" have entirely different histories.
ca75859311
...
488a8596b5
|
@ -1,45 +0,0 @@
|
||||||
#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));
|
|
||||||
}
|
|
|
@ -1,46 +0,0 @@
|
||||||
#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");
|
|
||||||
}
|
|
Loading…
Reference in New Issue