
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7442 Articles for Java

707 Views
In this article, we will learn to find the common divisors of two numbers using Java. The program will use a recursive method to calculate the greatest common divisor (GCD) of the two numbers, and then determine how many divisors are shared by both numbers. The output will display the total number of common divisors. Problem Statement Write a Java program to find and count the common divisors of two given numbers. Below is a demonstration of the same − Input val_1= 68 val_2= 34 Output The common divisors between the two numbers is4 Steps to find the common divisors ... Read More

274 Views
In Bitonic Sort, the comparision is in predefined sequence (Bitonic sequence), not dependent on the data to be sorted. Let us see an example for Bitonic Sort Java program −Example Live Demopublic class Demo{ void compare_swap(int my_arr[], int i, int j, int direction){ if ((my_arr[i] > my_arr[j] && direction == 1) || (my_arr[i] < my_arr[j] && direction == 0)){ int temp = my_arr[i]; my_arr[i] = my_arr[j]; my_arr[j] = temp; } } void merge_vals(int my_arr[], int low, int cnt, int direction){ ... Read More

7K+ Views
Following is the program for Recursive Binary Search in Java −Example Live Demopublic class Demo{ int rec_bin_search(int my_arr[], int left, int right, int x){ if (right >= left){ int mid = left + (right - left) / 2; if (my_arr[mid] == x) return mid; if (my_arr[mid] > x) return rec_bin_search(my_arr, left, mid - 1, x); return rec_bin_search(my_arr, mid + 1, right, x); } return -1; } ... Read More

234 Views
Following is an example for Anagram Substring Search in Java −Example Live Demopublic class Demo{ static final int max_val = 256; static boolean compare_vals(char my_arr_1[], char my_arr_2[]){ for (int i = 0; i < max_val; i++) if (my_arr_1[i] != my_arr_2[i]) return false; return true; } static void search_subs(String my_pattern, String my_text){ int pat_len = my_pattern.length(); int txt_len = my_text.length(); char[] count_pat = new char[max_val]; char[] count_txt = new char[max_val]; for ... Read More

261 Views
The Comb Sort in Java eliminates the smaller values situated to the end of the list and the inversions are removed oby by one. Let us see an example −Example Live Demoimport java.util.Arrays; public class Demo{ void comb_sort(int nums[]){ int len_gap = nums.length; float shrink_val = 1.3f; boolean swap = false; while (len_gap > 1 || swap) { if (len_gap > 1) { len_gap = (int)(len_gap / shrink_val); } swap ... Read More

208 Views
The Counting Sort counts the number of objects having distinct key values. Let us see an example −Note − The below code can be used with negative numbers as well.Example Live Demoimport java.util.*; public class Demo{ static void count_sort(int[] arr){ int max_val = Arrays.stream(arr).max().getAsInt(); int min_val = Arrays.stream(arr).min().getAsInt(); int range = max_val - min_val + 1; int count[] = new int[range]; int result[] = new int[arr.length]; for (int i = 0; i < arr.length; i++){ count[arr[i] - min_val]++; ... Read More

3K+ Views
Binary insertion sort uses the binary search to find the right position to insert an element at a specific index at every iteration. First, the location where the element needs to be inserted is found. Then, the elements are shifted to the next right location. Now, the specific element is placed in the position. Problem Statement Given an integer of array implement binary insertion sort using Java to sort it. The algorithm should use binary search to determine the correct position for each element and to maintain the sorted order it will shift elements accordingly. Steps for Binary Insertion ... Read More

361 Views
In this article, we will learn to perform cocktail sorting in Java. Cocktail Sort, also known as Bidirectional Bubble Sort, is a variation of the standard Bubble Sort algorithm. What is Cocktail Sort? Cocktail Sort works in contrast to bubble sort, wherein elements are iterated from left to right, and the largest element is first brought to its correct position, and so on. In cocktail sort, elements are iterated over in both directions (left and right) in an alternating fashion. Cocktail Sort Working Following are the steps to perform cocktail sort in Java − Traverse ... Read More

153 Views
The conditional operator (? :) leverages the output of one value (which is a bool) to decide which expression has to be evaluated next. Let us see an example −Example Live Demoimport java.io.*; public class Demo{ public static void main (String[] args){ Object my_obj = true ? new Integer(91) : new Float(89); System.out.println(my_obj); } }Output91.0A class named Demo contains the main function. Here, an object instance is defined and if it is true, an integer value is displayed otherwise a float value is displayed. Next, they are printed on the console.When promotional expression ... Read More

334 Views
In Java, multiplyExact() is an in-built static method of the Math class that accepts two arguments and returns their product as a result. This method can accept values of either integer or long type. One important point to note is that it may throw an exception if the produced result overflows the size of integer. After knowing the functionality of multiplyExact() in Java, one question that may pop into one's mind is that we can also use '*' operator to multiply two operands then why do we need multiplyExact() method. We are going to answer this question and also, explore ... Read More