Sem I (Program I) - Practice Flashcards
1
Q
Schreib ein Beispiel für Fakultätsberechnung(Factorial) mit Methode nutzung iterativ
A
class HelloWorld{ public static int fakultaet(int eingabe) { int f = 1; for (int i = 2; i <= eingabe; i++) f = f * i; return f; } public static void main(String[] args) { int erg = fakultaet(5); System.out.println(erg); } }
2
Q
Schreib ein Beispiel für Fakultätsberechnung(Factorial) ohne Methode nutzung
A
class HelloWorld{ public static void main(String[] args) { int eingabe = 3; int f = 1; for (int i = 2; i <= eingabe; i++) f = f * i; System.out.print(f); } }
3
Q
Schreib ein Beispiel für Fakultätsberechnung(Factorial) mit Methode nutzung und rekursion
A
class HelloWorld{ public static int fakultaet(int eingabe) { if (eingabe <= 1) return 1; else return eingabe * fakultaet(eingabe - 1); } public static void main(String[] args) { int erg = fakultaet(5); System.out.println(erg); } }
4
Q
Напиши пример алгоритма Bubble Sort
A
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]) { // Меняем arr[j] и arr[j+1] местами int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } }
5
Q
Напиши пример алгоритма Selection Sort
A
public class SelectionSort { public static void selectionSort(int[] arr) { int n = arr.length; for (int i = 0; i < n-1; i++) { int min_idx = i; for (int j = i+1; j < n; j++) { if (arr[j] < arr[min_idx]) { min_idx = j; } } int temp = arr[min_idx]; arr[min_idx] = arr[i]; arr[i] = temp; } }
6
Q
Напиши пример алгоритма Insertion Sort
A
public class InsertionSort { public static void insertionSort(int[] arr) { int n = arr.length; for (int i = 1; i < n; ++i) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; } }
7
Q
Напиши пример алгоритма Merge Sort
A
public class MergeSort { public static void mergeSort(int[] arr, int l, int r) { if (l < r) { int m = l + (r - l) / 2; mergeSort(arr, l, m); mergeSort(arr, m + 1, r); merge(arr, l, m, r); } } public static void merge(int[] arr, int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int[] L = new int[n1]; int[] R = new int[n2]; System.arraycopy(arr, l, L, 0, n1); System.arraycopy(arr, m + 1, R, 0, n2); int i = 0, j = 0, k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } }
8
Q
Напиши пример алгоритма Quick Sort
A
public class QuickSort { public static void quickSort(int[] arr, int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } public static int partition(int[] arr, int low, int high) { int pivot = arr[high]; int i = (low - 1); for (int j = low; j < high; j++) { if (arr[j] < pivot) { i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return i + 1; }
9
Q
Напиши пример алгоритма Heap Sort
A
public class HeapSort { public static void heapSort(int[] arr) { int n = arr.length; for (int i = n / 2 - 1; i >= 0; i--) { heapify(arr, n, i); } for (int i = n - 1; i > 0; i--) { int temp = arr[0]; arr[0] = arr[i]; arr[i] = temp; heapify(arr, i, 0); } } public static void heapify(int[] arr, int n, int i) { int largest = i; int left = 2 * i + 1; int right = 2 * i + 2; if (left < n && arr[left] > arr[largest]) { largest = left; } if (right < n && arr[right] > arr[largest]) { largest = right; } if (largest != i) { int swap = arr[i]; arr[i] = arr[largest]; arr[largest] = swap; heapify(arr, n, largest); } }
10
Q
Напиши пример алгоритма LinearSearch
A
public class LinearSearch { public static int linearSearch(int[] array, int target) { for (int i = 0; i < array.length; i++) { if (array[i] == target) { return i; } } return -1; // Целевой элемент не найден }
11
Q
Напиши пример алгоритма BinarySearch
A
public class BinarySearch { public static int binarySearch(int[] array, int target) { int left = 0; int right = array.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (array[mid] == target) { return mid; } if (array[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; // Целевой элемент не найден }