Java Program to Insert Element in Array at Specified Position

Here you will get java program to insert element in array at specified position. How it works? First copy all the elements before the position where element is to be inserted in a new array. Now assign the element at given position in the new array. Finally copy all the elements after the position where element is to be inserted in a new array. We can partially copy elements of one array to another using System.arraycopy() method. Its syntax is

Read more

Java Program for Matrix Multiplication

Here you will get java program for matrix multiplication. Suppose we have matrix A with number of rows and columns as m and n. Let B be a matrix with number of rows and columns as p and q. Their multiplication is possible only if number of columns of matrix A is equal to number of rows of matrix B i.e. n should be equal to p. The resultant matrix is of order mxq. Also Read: Java Program for Matrix Addition

Read more

Java Program for Matrix Addition

Here you will get java program for matrix addition. If you have ever done addition of two matrices in your maths subject then you can easily do this in java also. It can be done in following way. Read two matrices in 2D array a[][] and b[][] respectively inside loop. Now add each element of a[][] with corresponding position element of b[][] and save the result in c[][]. Finally display the 2D array c[][]. Note: The addition is possible only when

Read more

Heap Sort in Java

Here you will get program for heap sort in java. Heap sort is a sorting algorithm that uses heap data structure. Its best, worst and average time complexity is O (n log n).   How heap sort algorithm works? First we make max heap from given set of elements. In max heap each parent node is greater than or equal to its left and right child. Now first node is swapped by last node and size of heap is reduced by 1.

Read more

Bubble Sort in Java

In this tutorial you will learn about bubble sort in Java. In bubble sort algorithm each element is compared with adjacent element and are swapped if not in correct order. It is one of the simplest but worst sorting algorithm. It is suitable for sorting small list of numbers. As bubbles come up on surface in water, in the same way the lighter or smaller elements come forward and heavier or bigger elements goes back. Below image shows how bubble

Read more

Difference between Array and ArrayList in Java

Here you will learn about difference between array and arraylist in java. Both array and arraylist are most important and frequently used data structure in java. Below I have discussed about various differences between them.   Difference between Array and ArrayList in Java Property Array ArrayList Size Array is fixed in size. Once declared, its size can’t be changed. ArrayList size can be changed after declaration. For example when we add or remove element in arraylist, its size increases or

Read more

Binary Search in Java

In this tutorial you will learn about binary search in Java. Binary search is a searching algorithm that uses divide and conquer technique. The array on which searching is to be done must be sorted in ascending order. The target element is compared with middle element. If it is less than middle element then left half is discarded and if it is greater than middle element then right half is discarded. Now a new middle element is calculated in the remaining

Read more

Linear Search in Java

Here you will learn about linear search in Java. It is one of the simplest and basic searching algorithm which is also known as sequential search. The targeted element is compared with each element of array until it is found. Its best and worst case time complexity is O (1) and O (n) respectively. Also Read: Binary Search in Java Below program shows that how to implement this algorithm in Java. Program for Linear Search in Java

  Output Enter

Read more