From 7214990070ba769f8af2cbb2360a4c05e8e994e8 Mon Sep 17 00:00:00 2001 From: Sergey Iryupin Date: Sun, 25 Jun 2023 13:29:23 +0200 Subject: [PATCH] algorithms lesson #2 + hw init --- .../Algorithms/src/homework2/Homework2.java | 59 +++++++++++++++++++ .../Algorithms/src/lesson2/Lesson2.java | 32 ++++++++++ 2 files changed, 91 insertions(+) create mode 100644 BerlinTelran/Algorithms/src/homework2/Homework2.java create mode 100644 BerlinTelran/Algorithms/src/lesson2/Lesson2.java diff --git a/BerlinTelran/Algorithms/src/homework2/Homework2.java b/BerlinTelran/Algorithms/src/homework2/Homework2.java new file mode 100644 index 00000000..74e3859e --- /dev/null +++ b/BerlinTelran/Algorithms/src/homework2/Homework2.java @@ -0,0 +1,59 @@ +package homework2; + +/** + * Algorithms. Homework #2 + * + * @author Sergey Iryupin + * @version 25 Jun 2023 + */ + +/** + * 2. Task #1 + * START + * READ number n + * IF n == 1 THEN return + * FOR i = 1, i <= n, i++ + * FOR j = 1; j <= n, j++ + * print "*" + * BREAK + * END + */ + +/** + * 2. Task #2 + * START + * READ number n + * numbers i = 0, j = 0, a = 0 + * FOR i = n/2, i <= n; i++ + * FOR j = 2, j <= n, j * 2 + * a = a + n / 2 + * END + */ + +/** + * 2. Task #3 + * START + * READ number n + * number a = 0 + * FOR i = 0, i < n, i++ + * FOR j = n, j > i, j-- + * a = a + i + j + * END + */ + +/** + * 2. Task #4 + * START + * READ number n + * numbers a = 0, i = n + * WHILE i > 0 + * a = a + i + * i = i / 2 + * END + */ + +public class Homework2 { + public static void main(String[] args) { + + } +} diff --git a/BerlinTelran/Algorithms/src/lesson2/Lesson2.java b/BerlinTelran/Algorithms/src/lesson2/Lesson2.java new file mode 100644 index 00000000..2262bcdf --- /dev/null +++ b/BerlinTelran/Algorithms/src/lesson2/Lesson2.java @@ -0,0 +1,32 @@ +package lesson2; + +import java.util.Arrays; + +public class Lesson2 { + public static void main(String[] args) { + int[] array = {1, 4, 5, 6, 9}; + + // сложность алгоритма O(1) + int a = array[1]; + + // сложность алгоритма O(N) + int sum = 0; + for (int item : array) { + sum += item; + } + System.out.println(Arrays.toString(array)); + System.out.println(sum); + + // сложность алгоритма O(log N) + int idx = Arrays.binarySearch(array, 4); + + // сложность алгоритма O(N log N) + // сортировка слиянием и быстрая сортировка + + // сложность алгоритма O(N ^ 2) + // пузырьковая сортировка + + // сложность алгоритма O(N!) + // задача коммивояжёра + } +}