Programming Articles - Page 1873 of 3366

Memory leaks in Java

AmitDiwan
Updated on 07-Jul-2020 08:19:01

519 Views

In Java, garbage collection (the work of destructor) is done automatically using garbage collection. But what if there are objects that have references to them in the code? It can’t be de-allocated, i.e their memory can’t be cleared. If such a situation occurs again and again, and the created or referred objects are not used at all, they become useless. This is what is known as a memory leak.If the memory limit is exceeded, the program gets terminated by throwing an error, i.e ‘OutOfMemoryError’. This is the reason why it is always suggested to remove all references to an object ... Read More

Max Heap in Java

AmitDiwan
Updated on 07-Jul-2020 08:17:19

3K+ Views

Max heap is a complete binary tree, wherein the value of a root node at every step is greater than or equal to value at the child node.Below is an implementation of Max Heap using library functions.Example Live Demoimport java.util.*; public class Demo{    public static void main(String args[]){       PriorityQueue my_p_queue = new PriorityQueue(Collections.reverseOrder());       my_p_queue.add(43);       my_p_queue.add(56);       my_p_queue.add(99);       System.out.println("The elements in the priority queue are : ");       Iterator my_iter = my_p_queue.iterator();       while (my_iter.hasNext())       System.out.println(my_iter.next());       my_p_queue.poll(); ... Read More

Java Virtual Machine (JVM) Stack Area

AmitDiwan
Updated on 07-Jul-2020 08:15:24

1K+ Views

Following are some key points to undertstand JVM Stack Area −During the creation of a thread, the Java Virtual Machine creates a separate stack.The JVM performs only two operations upon this stack. The operations are push (i.e insert) and pop (i.e delete).When a thread is currently in execution, the stack associated with it is known as runtime stack.Every method call done by the thread, intermediate calculations, assignment of local variables, calling parameters etc, are stored as an operation in the runtime stack.Once the thread stops or completes executing, the respective part from the stack is deleted.Once all the calls by ... Read More

Java program for iterative quick sort

AmitDiwan
Updated on 18-Nov-2024 22:32:01

897 Views

In this program, we will perform an iterative quick sort in Java to sort an array of integers. Instead of using recursion, the program uses an iterative approach with an auxiliary stack to manage the sorting process. The output will display the sorted array after applying the quick sort algorithm. Problem Statement Write a Java program to sort an array of integers using the iterative quick sort method. Below is the demonstration of the same − Input {34, 76, 41, 32, 11, 0, 91, 102, -11} Output After iteratively performing quick sort, the array is -11 0 11 32 34 ... Read More

Java Program for Iterative Merge Sort

Alshifa Hasnain
Updated on 25-Feb-2025 19:21:22

570 Views

In this article, we will learn about iterative Merge sort in Java. Unlike the traditional iterative Merge Sort, the following Java program implements a recursive merge step while dividing the array and an iterative approach for merging. What is merge sort? Merge Sort is a popular sorting algorithm that follows the Divide and Conquer rule. It recursively divides the array into two halves, sorts each half, and then merges them back in a sorted manner.The following are three main steps to perform Merge sort in Java − Divide: The array is split into two halves ... Read More

Java Program for Gnome Sort

AmitDiwan
Updated on 04-Dec-2024 22:05:31

418 Views

Gnome Sort, also known as Stupid Sort, is a simple sorting algorithm that works by iterating through a list, comparing adjacent elements, and swapping them if they are in the wrong order. If a swap occurs, the algorithm moves one step backward to recheck the order, otherwise, it moves forward. This process continues until the array is sorted. In this article, we will explain the working of Gnome Sort using Java. Gnome Sort Approach Start from the first element. Compare the current element with the previous one: ... Read More

Java Program for Reversal algorithm for array rotation

AmitDiwan
Updated on 07-Jul-2020 08:04:24

349 Views

Following is the Java program to implement Reversal algorithm for array rotation −Example Live Demoimport java.io.*; public class Demo{    static void rotate_left(int my_arr[], int no_of_rotation){       int n = my_arr.length;       array_reversal(my_arr, 0, no_of_rotation - 1);       array_reversal(my_arr, no_of_rotation, n - 1);       array_reversal(my_arr, 0, n - 1);    }    static void array_reversal(int my_arr[], int start, int end){       int temp;       while (start < end) {          temp = my_arr[start];          my_arr[start] = my_arr[end];          my_arr[end] = ... Read More

Java Program for Recursive Insertion Sort

AmitDiwan
Updated on 07-Jul-2020 07:57:49

965 Views

Following is the Java Program for Recursive Insertion Sort −Example Live Demoimport java.util.Arrays; public class Demo{    static void recursive_ins_sort(int my_arr[], int arr_len){       if (arr_len = 0 && my_arr[j] > last){          my_arr[j+1] = my_arr[j];          j--;       }       my_arr[j+1] = last;    }    public static void main(String[] args){       int my_arr[] = {11, 23, 67, 83, 42, 11, 0};       recursive_ins_sort(my_arr, my_arr.length);       System.out.println("The array elements after implementing insertion sort is ");       System.out.println(Arrays.toString(my_arr));    } }OutputThe ... Read More

Java program for recursive Bubble Sort

AmitDiwan
Updated on 27-Aug-2024 18:48:46

2K+ Views

In this article, we will learn to implement Bubble Sort using recursion in Java. Our goal is to understand how recursion can be applied to the Bubble Sort algorithm to achieve the same sorting effect as its iterative counterpart. Problem Statement Write a Java program to sort an array of integers using the recursive approach of Bubble Sort. Input my_arr[] = {45, 67, 89, 31, 63, 0, 21, 12} Output The array after implementing bubble sort is[0, 12, 21, 31, 45, 63, 67, 89] Bubble Sort Algorithm Bubble sort algorithm is a simple sorting algorithm to sort elements. ... Read More

Java program to calculate area of a tetrahedron

Alshifa Hasnain
Updated on 26-Feb-2025 19:36:26

482 Views

In this article, we will learn to calculate the area of a Tetrahedron using Java. A tetrahedron is a type of polyhedron that consists of four triangular faces. What is a tetrahedron? A tetrahedron is a three-dimensional polyhedron with four triangular faces, six edges, and four vertices. If all its faces are equilateral triangles, it is called a regular tetrahedron. The surface area of a regular tetrahedron can be calculated using the formula − 𝐴 = sqrt{3} . s^2 Where A is the surface area, where s is the length of a side of the tetrahedron. Calculating the Area of ... Read More

Advertisements