Skip to content

Instantly share code, notes, and snippets.

View LeoPFreitas's full-sized avatar

Leonardo Freitas LeoPFreitas

  • CI&T
  • São Carlos, Brazil
View GitHub Profile
@LeoPFreitas
LeoPFreitas / searchInSortedArrays.java
Last active August 6, 2020 16:08
Simple linear search algorithm for searching a value in sorted arrays.
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;
}
}
@LeoPFreitas
LeoPFreitas / linearSearchArray.java
Last active August 6, 2020 16:08
Linear search in array of integers.
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;
}
@LeoPFreitas
LeoPFreitas / jumpSearch.java
Last active August 6, 2020 16:07
Jump search algorithm
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 */
@LeoPFreitas
LeoPFreitas / bubbleSort.java
Last active August 6, 2020 16:06
Simplest bubble sort algorithm implemented in Java.
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;
}
}
@LeoPFreitas
LeoPFreitas / binarySearch.java
Created August 6, 2020 16:05
Binary Search algorithm
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
@LeoPFreitas
LeoPFreitas / quickSort.java
Created August 6, 2020 20:30
Recursive quicksort algorithm
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
@LeoPFreitas
LeoPFreitas / FindEmptyDirectories.java
Created September 8, 2020 17:45
Recursively find empty directories -- need command line argument
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();
@LeoPFreitas
LeoPFreitas / FindDeepestFile.java
Created September 8, 2020 17:55
Find the deepest file in a system file tree.
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);
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)));
}
@LeoPFreitas
LeoPFreitas / HibernateUtil.java
Created December 22, 2020 14:28
Hibernate configuration file for Postgresql 9.4 or later
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");