Skip to main content

Posts

Insertion and Deletion of a Node in Linked List | Java | MyCodingNetwork

  Insertion of a new node & Deletion of an existing node After an exhilarating commencement of the Linked List series, in this post, we'll be exploring the artistry behind insertion and deletion operations in linked list. So let's now move on to the second problem statement of the series, i.e., write two functions for insertion and deletion of a node respectively, from linked list . Algorithm for inserting a node at pos position: Create a function insert () which takes head of the node, pos and the new node as the parameter. Take a base case, if pos==1, means the new node is to be made the head of the linked list. If the above case is not true, traverse the linked list and reach to position, pos-1 . Assign the new node with the address of the node at pos. Finally, assign the node at pos-1 with address of new node. Algorithm for deleting a node at pos position: Create a function deletion () which takes pos and the head of the LinkedList as parameter. Base case, if pos ...

LinkedList Creation and Traversal | Java | MyCodingNetwork

How to Create and Traverse a Linked List without using Java collection frameworks? Linked lists are an essential data structure in computer science, providing an efficient way to store and manage data. In Java, the LinkedList class in the Java Collections framework is widely used for this purpose. However, in this blog post, we will explore how to create a LinkedList from scratch using a custom class, without relying on the built-in Java Collections.   Algorithm: 1.             Define a Node class that represents individual nodes in the list. Each node contains data and a reference to the next node in the sequence. 2.             To add elements to the LinkedList, we will create an object of the Node class and assign a value of the node to its constructor. Its syntax is: Node newNode = new Node(value); 3.          ...

Features of Java | Code Trivia | MyCodingNetwork

Features of Java Java is platform  independent. Java is  portable. Java is object-oriented and it distributed. Java is  multithreaded. Java is write once run anywhere. Hope you liked this explanation, for any doubt or feedback you can write down in the comment section. #java #features #coding #programming

Scanner Class | Java | Code Trivia | MyCodingNetwork

Scanner class in Java is used to accept values from the user. The values can be used in the program. It is part of the java.util package. Users can use it to provide values of primitive data types like boolean, int, float, double, String etc.  One of the major advantages of Scanner class is that users can test the program for various inputs instead of the constant values assigned to the variables of the program. In other words, it increases the useability of the program. Syntax:  1. To import the class import java.util.Scanner; OR import java.util.*; 2. Creating object sc of the Scanner Scanner sc=new Scanner (System.in); Program 1: import java.util.Scanner; class Test{ public static void main(String args[ ]) { Scanner sc=new Scanner (System.in); //nextInt() is function to receive value of int //data type  int a=sc.nextInt(); int b=sc.nextInt(); int sum=a+b; System.out.println(sum); }} Output can change according to the values entered by the user. Now compare it with ...

Bubble Sort | Algorithm | MyCodingNetwork

Bubble Sort Bubble sort is a sorting algorithm which is used to sort the elements of an array and its time complexity O(N 2 ).  This algorithm’s time complexity is not as efficient as Merge Sort but still it is one of the simplest sorting algorithms. Source Code: Output: The highlighted part is the sorted array (ascending order). Working: In the program shown above, we have used bubble sort technique to sort the array in ascending order, given by the user as the input ( in the main( ) function ). The entire bubble sorting algorithm ( in the program ) takes place in the function bubblesort(), the function takes two parameters.  When an array undergoes bubble sort, continuous swapping takes place (if the array is not in desired order). The program given above is to arrange the array in ascending order, it can be changed to descending order, simply by changing “greater than” symbol to “less than” symbol on line 33. Swapping takes place from line 36( swapping takes place only if t...

Merge Sort | Algorithm | MyCodingNetwork

Merge Sort Hi there, I am back with another article on one of the most important algorithms in the world of DSA. In this article we would be covering Merge Sort and talk about its various aspects. So, without wasting much time let's jump into the topic right away. Merge sort is another type of sorting algorithm whose time complexity is O(N*logN) . The worst, average and best time complexity of merge sort always remains same , that is, O(N*logN) . You must be thinking why the time complexity is same for all cases. Well, the answer to that we will get as we move further in our discussion. It works on divide and conquer technique, where the given array is divided into smaller somebodies of two halves and then sorting is done. Source Code: conquer()  function: divide( ) function: main( ) function: Output: #1  In the above example, user first enters the length of the array, and then the array elements. The highlighted part are the elements of array after sorting. #2 Let's take ano...

Binary Search | The-Algorithm-Drive | MyCodingNetwork

Binary Search The next algorithm, we are going to study is Binary Search . As the name suggests, it is a searching algorithm. It is one of the very famous searching techniques. Unlike its counterpart linear search, it is way more advanced.  Time complexity of linear search is O(N) whereas that of binary search is O (log N). Source Code: Output Number Found at index =5 Working Let’s come on to the working of the algorithm, as you can see in the above program, we need a sorted array to perform binary search algorithm. 1.    Here, bin function in the above program it accepts 4 parameters, that is, the array arr , lower index low , higher index high and the searching number sn . 2.    Next, we have a do-while loop to check if high is greater than or equal to low (high>=low). If at any point of iteration this condition is not satisfied, it means the searching number sn does not exist in the array. Following is the algorithm for binary search: while ...