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: |

ZDNET RECOMMENDS A Hong Kong protester with a petrol bomb. File photo: Dylan Hollingsworth/HKFP. Telegram is a leading cloud-based instant messages platform. It became popular in recent years for its privacy, speed, voice and video quality, and other unmatched features over its main competitor Whatsapp. The public channel had more than 109,000 subscribers, Judge Hui said. Ng had the power to remove or amend the messages in the channel, but he “allowed them to exist.” Developing social channels based on exchanging a single message isn’t exactly new, of course. Back in 2014, the “Yo” app was launched with the sole purpose of enabling users to send each other the greeting “Yo.”
from us


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