This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int searchInSortedArray(int[] array, int value) { | |
int index = -1; | |
for (int i = 0; i < array.length; i++) { | |
if (array[i] == value) { | |
index = i; | |
break; | |
} else if (array[i] > value) { | |
break; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int linearSearchArray(int[] array, int value) { | |
int index = -1; | |
for (int i = 0; i < array.length; i++) { | |
if (array[i] == value) { | |
index = i; | |
break; | |
} | |
} | |
return index; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int jumpSearch(int[] array, int target) { | |
int currentRight = 0; // right border of the current block | |
int prevRight = 0; // right border of the previous block | |
/* If array is empty, the element is not found */ | |
if (array.length == 0) { | |
return -1; | |
} | |
/* Check the first element */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int[] bubbleSort(int[] array) { | |
for (int i = 0; i < array.length - 1; i++) { | |
for (int j = 0; j < array.length - i - 1; j++) { | |
/* if a pair of adjacent elements has the wrong order it swaps them */ | |
if (array[j] > array[j + 1]) { | |
int temp = array[j]; | |
array[j] = array[j + 1]; | |
array[j + 1] = temp; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int binarySearch(int[] array, int elem, int left, int right) { | |
while (left <= right) { | |
int mid = left + (right - left) / 2; // the index of the middle element | |
if (elem == array[mid]) { | |
return mid; // the element is found, return its index | |
} else if (elem < array[mid]) { | |
right = mid - 1; // go to the left subarray | |
} else { | |
left = mid + 1; // go the the right subarray |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void quickSort(int[] array, int left, int right) { | |
if (left < right) { | |
int pivotIndex = partition(array, left, right); // the pivot is already on its place | |
quickSort(array, left, pivotIndex - 1); // sort the left subarray | |
quickSort(array, pivotIndex + 1, right); // sort the right subarray | |
} | |
} | |
private static int partition(int[] array, int left, int right) { | |
int pivot = array[right]; // choose the rightmost element as the pivot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.util.Scanner; | |
class FindEmptyDirectories { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
File file = new File(args[0]); | |
goRecursive(file); | |
sc.close(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.util.Scanner; | |
class Main { | |
static int maxLevel = 0; | |
static String deepestFileName = null; | |
static int currentLevel = 0; | |
static String deepestPath = null; | |
static Scanner sc = new Scanner(System.in); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.util.stream.Collectors; | |
class Monitor { | |
public static Map<String, Long> getUrlToNumberOfVisited(List<LogEntry> logs) { | |
return logs.stream() | |
.collect(Collectors.groupingBy(LogEntry::getUrl, Collectors.summingLong(a -> 1L))); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HibernateUtil { | |
private static StandardServiceRegistry standardServiceRegistry; | |
private static SessionFactory sessionFactory; | |
static { | |
StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder(); | |
Map<String, String> dbSettings = new HashMap<>(); | |
dbSettings.put(Environment.URL, "jdbc:postgresql://localhost/dbname"); |