BOOKJAVA Telegram 3890
📌Примеры базовых алгоритмов


➡️ 1. Сортировка пузырьком (Bubble Sort)


public class BubbleSort {
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// обмен
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}



➡️ 2. Бинарный поиск (Binary Search)


public class BinarySearch {
public static int binarySearch(int[] arr, int target) {
int left = 0, right = arr.length - 1;

while (left <= right) {
int mid = (left + right) / 2;

if (arr[mid] == target) return mid;
if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}

return -1; // элемент не найден
}
}


➡️ 3. Поиск максимального элемента в массиве


public class MaxInArray {
public static int findMax(int[] arr) {
int max = arr[0];
for (int num : arr) {
if (num > max) max = num;
}
return max;
}
}


➡️ 4. Факториал через рекурсию


public class Factorial {
public static long factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
}


➡️ 5. Проверка, является ли строка палиндромом


public class PalindromeCheck {
public static boolean isPalindrome(String str) {
int left = 0, right = str.length() - 1;

while (left < right) {
if (str.charAt(left++) != str.charAt(right--)) {
return false;
}
}

return true;
}
}


👉@BookJava
Please open Telegram to view this post
VIEW IN TELEGRAM
👍71



tgoop.com/BookJava/3890
Create:
Last Update:

📌Примеры базовых алгоритмов


➡️ 1. Сортировка пузырьком (Bubble Sort)


public class BubbleSort {
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// обмен
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}



➡️ 2. Бинарный поиск (Binary Search)


public class BinarySearch {
public static int binarySearch(int[] arr, int target) {
int left = 0, right = arr.length - 1;

while (left <= right) {
int mid = (left + right) / 2;

if (arr[mid] == target) return mid;
if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}

return -1; // элемент не найден
}
}


➡️ 3. Поиск максимального элемента в массиве


public class MaxInArray {
public static int findMax(int[] arr) {
int max = arr[0];
for (int num : arr) {
if (num > max) max = num;
}
return max;
}
}


➡️ 4. Факториал через рекурсию


public class Factorial {
public static long factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
}


➡️ 5. Проверка, является ли строка палиндромом


public class PalindromeCheck {
public static boolean isPalindrome(String str) {
int left = 0, right = str.length() - 1;

while (left < right) {
if (str.charAt(left++) != str.charAt(right--)) {
return false;
}
}

return true;
}
}


👉@BookJava

BY Библиотека Java разработчика


Share with your friend now:
tgoop.com/BookJava/3890

View MORE
Open in Telegram


Telegram News

Date: |

There have been several contributions to the group with members posting voice notes of screaming, yelling, groaning, and wailing in different rhythms and pitches. Calling out the “degenerate” community or the crypto obsessives that engage in high-risk trading, Co-founder of NFT renting protocol Rentable World emiliano.eth shared this group on his Twitter. He wrote: “hey degen, are you stressed? Just let it out all out. Voice only tg channel for screaming”. Choose quality over quantity. Remember that one high-quality post is better than five short publications of questionable value. The Standard Channel fire bomb molotov November 18 Dylan Hollingsworth yau ma tei With the “Bear Market Screaming Therapy Group,” we’ve now transcended language.
from us


Telegram Библиотека Java разработчика
FROM American