Found 9291 Articles for Object Oriented Programming

Java Program to cube sum of first n natural numbers

AmitDiwan
Updated on 04-Jul-2020 09:55:23

399 Views

Following is the Java code to cube sum of first n natural numbers −Example Live Demoimport java.util.*; import java.lang.*; public class Demo{    public static int first_n_nat_no(int val){       int ini_sum = 0;       for (int x=1; x

Java Program for Common Divisors of Two Numbers

AmitDiwan
Updated on 04-Jul-2020 09:52:34

514 Views

Following is an example for Common Divisors of Two numbers in Java −Example Live Demopublic class Demo{    static int find_gcd(int val_1, int val_2){       if (val_1 == 0)       return val_2;       return find_gcd(val_2%val_1,val_1);    }    static int common_divisors(int val_1,int val_2){       int no = find_gcd(val_1, val_2);       int result = 0;       for (int i=1; i

Java Program for Bitonic Sort

AmitDiwan
Updated on 04-Jul-2020 09:50:29

148 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

Java Program for Binary Search (Recursive)

AmitDiwan
Updated on 04-Jul-2020 09:47:14

6K+ 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

Java Program for Anagram Substring Search

AmitDiwan
Updated on 04-Jul-2020 09:44:12

166 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

Java Program for Comb Sort

AmitDiwan
Updated on 04-Jul-2020 09:41:16

178 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

Java Program for Counting Sort

AmitDiwan
Updated on 04-Jul-2020 09:39:15

126 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

Java Program for Binary Insertion Sort

AmitDiwan
Updated on 02-Jul-2024 17:29:47

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

Java Program for Cocktail Sort

AmitDiwan
Updated on 04-Jul-2020 09:29:41

212 Views

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 the directions (left and right) in an alternating fashion.Following is the program for Cocktail Sort −Example Live Demopublic class Demo{    static int temp;    static void Cocktail(int a[], int n){       boolean swap = true;       int begin = 0, i;       int end = n - 1;       while (swap) {   ... Read More

Java Numeric Promotion in Conditional Expression

AmitDiwan
Updated on 04-Jul-2020 09:20:21

98 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

Advertisements