<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Scaler Topics</title>
    <description>The latest articles on DEV Community by Scaler Topics (@scalertopics).</description>
    <link>https://dev.to/scalertopics</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F5032%2Fd1ad675e-d460-4fff-b194-c145ed5673f1.png</url>
      <title>DEV Community: Scaler Topics</title>
      <link>https://dev.to/scalertopics</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/scalertopics"/>
    <language>en</language>
    <item>
      <title>What is Foreign Key in DBMS?</title>
      <dc:creator>Pranjali Nandan</dc:creator>
      <pubDate>Fri, 04 Mar 2022 10:15:22 +0000</pubDate>
      <link>https://dev.to/scalertopics/what-is-foreign-key-in-dbms-3gbe</link>
      <guid>https://dev.to/scalertopics/what-is-foreign-key-in-dbms-3gbe</guid>
      <description>&lt;p&gt;Before understanding the concept of foreign keys, let’s understand what a key is. So, as we know, in a database, we store our data in the form of tables, in the form of rows and columns in a table. What if we want to connect two or more tables with each other? For that, we need to understand the concept of a key. So, using the concept of primary key and foreign key Let's understand what the foreign key is in this article.&lt;/p&gt;

&lt;p&gt;A foreign key is a column or a group of columns in a database table which must match with some other column in another table. In simple words, we can say a column which is common in two tables can be considered a foreign key. Let’s understand more clearly what foreign keys are using an example given below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rjNaB6Fd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cn1zzxint5v1moe1gia3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rjNaB6Fd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cn1zzxint5v1moe1gia3.png" alt="" width="880" height="381"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a student table, in which we have roll no, name, class, and age as attributes. This type of storage of data is seen in our databases (in the form of tables). Let's explore another table named "Student Info Table."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_n6BytGy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3q8ajuk7r9mz7athfrer.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_n6BytGy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3q8ajuk7r9mz7athfrer.png" alt="" width="880" height="381"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see, it is a table containing information about a student, such as fees, status, contact information, and address. If you see, both tables (student and student info) have a common column, which is Roll No. So rolling no column in the student info table will work as a foreign key and rolling no column in the student table will work as a primary key. Using the concept of a foreign key and a primary key, we can connect both the tables.&lt;/p&gt;

&lt;h2&gt;
  
  
  Foreign Key Query in MySQL
&lt;/h2&gt;

&lt;p&gt;Let’s see how we can write code of foreign key in MySQL.&lt;br&gt;
 &lt;code&gt;CREATE TABLE Student info (&lt;br&gt;
    Roll no int NOT NULL,&lt;br&gt;
    Fees status varchar (30),&lt;br&gt;
    Contact no int NOT NULL,&lt;br&gt;
    Address varchar (50) NOT NULL,&lt;br&gt;
    PRIMARY KEY (Roll no),&lt;br&gt;
    FOREIGN KEY (Roll no) REFERENCES Student (Roll  no)&lt;br&gt;
);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let’s explore how we can add content to these tables.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;INSERT INTO Student VALUE (21,’Ramesh’, ‘10th’,15);&lt;br&gt;
INSERT INTO Student VALUE (22,’Atul’, ‘3rd’,8);&lt;br&gt;
INSERT INTO Student VALUE (23,’Rahul’, ‘8th’,13);&lt;br&gt;
INSERT INTO Student VALUE (24,’Piyush’, ‘12th’,17);&lt;br&gt;
INSERT INTO Student VALUE (25,’Arnav’, ‘7th’,13);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Remove the Foreign Key Constraint&lt;/p&gt;

&lt;p&gt;If we want to write a SQL command to drop a foreign key constraint, then we will write the following command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ALTER TABLE Student&lt;br&gt;
     DROP FOREIGN KEY&lt;/code&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Why do we need Foreign Key?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Using the concept of a foreign key, we can connect two or more tables as we can create a relationship between those tables with a common attribute.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Foreign keys also help us to maintain the referential integrity of the database.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How does Foreign Key maintain Referential Integrity?
&lt;/h2&gt;

&lt;p&gt;Let's understand this concept with the help of two tables? One is the student table, which contains the roll number, name, and address of a student, and the other table is the course table, which contains information about a student's course.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Fpsla5ZK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qz7t6uxub47p3el1cku0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Fpsla5ZK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qz7t6uxub47p3el1cku0.png" alt="" width="880" height="599"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, roll no in the course table (referencing table) will work as a foreign key and roll no in the student table will work as a primary key. Here, the student table is the reference table and the course table is the referencing table. So, the main thing we'll learn in this topic is what happens if we insert, update, or delete something in this table. So, let's explore that part also. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Insert – If we insert any entry into our base table, that is, the student table, then it will not create any issue. We can insert as many entries as we want, and it won’t create any issue for insertion. As a result, there is no violation in insertion.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Delete – If we delete any entry from our base table, like if any student leaves an organization, then we have to delete that data from our table. Let’s consider that roll no. 1 leaves the organization, and we delete that entry from the student table. But in the course table, we see that roll no. 1 is currently studying network subjects. So directly deleting will create an issue. Integrity will be lost in this case. But if we consider a case where we insert a new entry, let’s say roll no 4, and then we delete that roll no, then it will not create any issue. So, deletion may create a violation.  &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, what do you think will be the solution to this issue? So, we have a concept of a delete cascade where the row that we are deleting will be automatically deleted from other tables also. Another method is to delete set null where the row we are deleting is found, and if the same row is found in another table, then we set the foreign key value null on that set. Another method is to delete data with no action. As the name suggests, it will not do any action, so data will be deleted in this case.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Update – If we want to update any row, then it will create an issue, like if we have to update that data in other related tables, as we saw in the deletion. We can use the same method that we perform on deletion, that is, on update cascade, on update set null, on update no action. Updating may result in a violation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To learn more about the concepts of primary key and the distinction between primary key and foreign key on &lt;a href="https://www.scaler.com/topics/difference-between-primary-key-and-foreign-key/"&gt;Scaler Topics&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Author: Arnav Bhardwaj&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>dbms</category>
    </item>
    <item>
      <title>Difference Between &lt;br&gt; and &lt;br/&gt; Tag in HTML


</title>
      <dc:creator>Pranjali Nandan</dc:creator>
      <pubDate>Thu, 24 Feb 2022 11:09:14 +0000</pubDate>
      <link>https://dev.to/scalertopics/difference-between-and-tag-in-html-5350</link>
      <guid>https://dev.to/scalertopics/difference-between-and-tag-in-html-5350</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Suppose you want your webpage to show the below message with the same format, How will you write it in HTML?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdsu80kmujk2gafbz31wo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdsu80kmujk2gafbz31wo.png" alt="Line breaks"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are planning to use carriage return (ENTER key) to produce line breaks, then let me tell you that it won’t work because HTML will ignore any carriage return and extra spaces.&lt;/p&gt;

&lt;p&gt;You can use the br tag which is used to produce line breaks in HTML and here are the two possible ways of using br tag.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;div&amp;gt;&lt;br&gt;
    Hello &amp;lt;br&amp;gt;&lt;br&gt;
    Welcome to &amp;lt;br&amp;gt;&lt;br&gt;
    the blog&amp;lt;br&amp;gt;&lt;br&gt;
&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;div&amp;gt;&lt;br&gt;
    Hello &amp;lt;br/&amp;gt;&lt;br&gt;
    Welcome to &amp;lt;br/&amp;gt;&lt;br&gt;
    the blog &amp;lt;br/&amp;gt;&lt;br&gt;
&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Both will give the same output, but what is the difference between br and br/?&lt;/p&gt;

&lt;p&gt;But before that let’s look at some pointers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;br tag is used to produce line breaks in an HTML document and it is a self-closing tag.&lt;/li&gt;
&lt;li&gt;When a tag is used with nothing between it, then a self-closing tag (or empty tag) can be used. br is one such self closing tag, which means there is no need of closing tag ( /br) while using br.&lt;/li&gt;
&lt;li&gt;Generally, TAG/ (not to be confused with /TAG) is used as a condensed way of writing TAG.../TAG.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So from the above pointers, it is evident that  br and br/ will provide the same results when used in HTML.&lt;/p&gt;

&lt;p&gt;But, whenever we are using XHTML (which is more strict than HTML), it doesn’t allow leaving tags open (like you cannot write just br, you have to close it also but /br doesn't make sense since it is a self-closing tag ), hence br/ will be used in that case.&lt;/p&gt;

&lt;p&gt;Also using br is less effective when it comes to code neatness and readability than br/. Hence br/ is generally preferred over br &lt;/p&gt;

&lt;p&gt;To sum up, the key differences between br and br/ in HTML can be understood with the help of the following table: &lt;/p&gt;

&lt;h2&gt;
  
  
  Difference between br and br/ Tag
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl4hbdtz42j37qre6bxoz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl4hbdtz42j37qre6bxoz.png" alt="Difference between br and br/ Parameter"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Browser Compatibility
&lt;/h2&gt;

&lt;p&gt;Let’s look at its browser compatibility now&lt;/p&gt;

&lt;h2&gt;
  
  
  Desktop Browsers
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdpfc89yaj0fi3zhezfbv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdpfc89yaj0fi3zhezfbv.png" alt="Desktop Browsers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frqtg4grvbtr60hpcxzt9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frqtg4grvbtr60hpcxzt9.png" alt="mobile Browsers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Both br and br/ produces the same result i.e line break. But br/ is preferred because it can be used with strict rules as well(like while writing XHTML document) and the latter looks cleaner and readable.&lt;/p&gt;

&lt;p&gt;Read more about other self-closing tags on &lt;a href="https://www.scaler.com/topics/br-tag-in-html/" rel="noopener noreferrer"&gt;Scaler topics&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Author: Rdiddhi Suteri&lt;/em&gt;&lt;/p&gt;

</description>
      <category>html</category>
      <category>tag</category>
      <category>programming</category>
    </item>
    <item>
      <title>Advantages of Doubly Linked List in Data Structure</title>
      <dc:creator>Bikash Daga</dc:creator>
      <pubDate>Mon, 21 Feb 2022 10:06:10 +0000</pubDate>
      <link>https://dev.to/scalertopics/advantages-of-doubly-linked-list-in-data-structure-3h6b</link>
      <guid>https://dev.to/scalertopics/advantages-of-doubly-linked-list-in-data-structure-3h6b</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A linked list contains nodes in which each node has a data part and the reference to the next node. Each node in a linked list is connected via links. The operations that can be performed on a linked list are search, insert, delete, traverse, etc. There are various kinds of linked lists like singly link list, doubly link list and circular link list. &lt;/p&gt;

&lt;p&gt;In this article, we will see the most used doubly link list and its advantages.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is a Doubly Linked List?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Doubly linked list is a variation of linked list in which we can move forward and backward. We can define a doubly linked list using the following terms - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Link:&lt;/strong&gt; Each link of a linked list can store a data called an element.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Next:&lt;/strong&gt; Each link of a linked list contains a link to the next link called Next.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prev:&lt;/strong&gt; Each link of a linked list contains a link to the previous link called Prev.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LinkedList:&lt;/strong&gt; A Linked List contains the connection link to the first link called First and to the last link called Last.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An example of a doubly linked list is the tabs in the browser, you can move to any tab forward or backward. &lt;/p&gt;

&lt;p&gt;So if a Linked List ⇒ A → B → C&lt;br&gt;
Then a Doubly Linked List ⇒ A ⇆ B ⇆ C&lt;/p&gt;
&lt;h2&gt;
  
  
  Representation of a &lt;a href="https://www.scaler.com/topics/data-structures/doubly-linked-list/"&gt;Doubly Linked List in Data Structure&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;A class node is created that contains a data variable to hold the value of a node. A next pointer to store the address of the next linked node in the list. &lt;/p&gt;

&lt;p&gt;A prev pointer to store the address of previously linked nodes in the list. &lt;/p&gt;
&lt;h2&gt;
  
  
  C++ Code
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Node
{
    public:
    int data; // value of a node in linked list 
    Node* next; // Pointer to next node in DLL
    Node* prev; // Pointer to previous node in DLL
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  C Code
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct Node // create a structure for C ll 
{
    int data; // value of a node in linked list 
    struct Node* next; // Pointer to next node in DLL
    struct Node* prev; // Pointer to previous node in DLL
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Operations on a Doubly Linked List
&lt;/h2&gt;

&lt;p&gt;The following are some various operations that can be performed in a doubly linked list -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Add a Node in the Front&lt;/strong&gt;
Steps involved -&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Allocate node&lt;/li&gt;
&lt;li&gt;Put in the data&lt;/li&gt;
&lt;li&gt;Make next of new node as head and previous as NULL&lt;/li&gt;
&lt;li&gt;Change prev of head node to new node&lt;/li&gt;
&lt;li&gt;Move the head to point to the new node&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Add a Node After a Given Node&lt;/strong&gt; 
Steps involved - &lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Check if the given previous node is NULL &lt;/li&gt;
&lt;li&gt;Allocate new node&lt;/li&gt;
&lt;li&gt;Put in the data &lt;/li&gt;
&lt;li&gt;Make next of new node as next of previous node&lt;/li&gt;
&lt;li&gt;Make the next of previous node as new_node&lt;/li&gt;
&lt;li&gt;Make previous node as previous of new_node&lt;/li&gt;
&lt;li&gt;Change previous of new_node's next node&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Add a Node in the End&lt;/strong&gt;
Steps involved -&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Allocate node and put in the data&lt;/li&gt;
&lt;li&gt;This new node is going to be the last node, so If the Linked List is empty, then make the new&lt;/li&gt;
&lt;li&gt;Else traverse till the last node&lt;/li&gt;
&lt;li&gt;Change the next of last node&lt;/li&gt;
&lt;li&gt;Make last node as previous of new node&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Add a Node in the End&lt;/strong&gt;
Steps involved - &lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Check if the next_node is NULL or not. If it’s NULL, return from the function because any new node can not be added before a NULL&lt;/li&gt;
&lt;li&gt;Allocate memory for the new node, let it be called new_node&lt;/li&gt;
&lt;li&gt;Set the value of data to the new node &lt;/li&gt;
&lt;li&gt;Set the previous pointer of this new_node as the previous node of the next_node, new_node-&amp;gt;prev = next_node-&amp;gt;prev&lt;/li&gt;
&lt;li&gt;Set the previous pointer of the next_node as the new_node.&lt;/li&gt;
&lt;li&gt;Set the next pointer of this new_node as the next_node.&lt;/li&gt;
&lt;li&gt;If the previous node of the new_node is not NULL, then set the next pointer of this previous node as new_node.&lt;/li&gt;
&lt;li&gt;Else, if the prev of new_node is NULL, it will be the new head node. &lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Code to Demonstrate the Insertion Operations
&lt;/h2&gt;
&lt;h2&gt;
  
  
  CPP
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;bits/stdc++.h&amp;gt;
using namespace std;
class Node // Create a doubly linked list 
{
    public:
    int data; // value of a node 
    Node* next;// next pointer 
    Node* prev;// previous pointer of node 
};


void push(Node** head_ref, int new_data) // insert a node in front of list 
{
    /* 1. allocate node
    Node* new_node = new Node();

    /* 2. put in the data */
    new_node-&amp;gt;data = new_data;

    /* 3. Make next of new node as head
    and previous as NULL */
    new_node-&amp;gt;next = (*head_ref);
    new_node-&amp;gt;prev = NULL;

    /* 4. change prev of head node to new node */
    if ((*head_ref) != NULL)
        (*head_ref)-&amp;gt;prev = new_node;

    /* 5. move the head to point to the new node */
    (*head_ref) = new_node;
}


void insertAfter(Node* prev_node, int new_data) // function to insert after a node 
{
    /*1. check if the given prev_node is NULL */
    if (prev_node == NULL)
    {
        cout&amp;lt;&amp;lt;"the given previous node cannot be NULL";
        return;
    }

    /* 2. allocate new node */
    Node* new_node = new Node();

    /* 3. put in the data */
    new_node-&amp;gt;data = new_data;

    /* 4. Make next of new node as next of prev_node */
    new_node-&amp;gt;next = prev_node-&amp;gt;next;

    /* 5. Make the next of prev_node as new_node */
    prev_node-&amp;gt;next = new_node;

    /* 6. Make prev_node as previous of new_node */
    new_node-&amp;gt;prev = prev_node;

    /* 7. Change previous of new_node's next node */
    if (new_node-&amp;gt;next != NULL)
        new_node-&amp;gt;next-&amp;gt;prev = new_node;
}


void append(Node** head_ref, int new_data) // function to add a new node at the end 
{
    /* 1. allocate node */
    Node* new_node = new Node();

    Node* last = *head_ref; /* used in step 5*/

    /* 2. put in the data */
    new_node-&amp;gt;data = new_data;

    /* 3. This new node is going to be the last node, so
        make next of it as NULL*/
    new_node-&amp;gt;next = NULL;

    /* 4. If the Linked List is empty, then make the new
        node as head */
    if (*head_ref == NULL)
    {
        new_node-&amp;gt;prev = NULL;
        *head_ref = new_node;
        return;
    }

    /* 5. Else traverse till the last node */
    while (last-&amp;gt;next != NULL)
        last = last-&amp;gt;next;

    /* 6. Change the next of last node */
    last-&amp;gt;next = new_node;

    /* 7. Make last node as previous of new node */
    new_node-&amp;gt;prev = last;

    return;
}


void printList(Node* node)// function to print the doubly linked list 
{
    Node* last;
    cout&amp;lt;&amp;lt;"\nTraversal in forward direction \n";
    while (node != NULL)
    {
        cout&amp;lt;&amp;lt;" "&amp;lt;&amp;lt;node-&amp;gt;data&amp;lt;&amp;lt;" ";
        last = node;
        node = node-&amp;gt;next;
    }

    cout&amp;lt;&amp;lt;"\nTraversal in reverse direction \n";
    while (last != NULL) // traverse the list in reverse direction 
    {
        cout&amp;lt;&amp;lt;" "&amp;lt;&amp;lt;last-&amp;gt;data&amp;lt;&amp;lt;" ";
        last = last-&amp;gt;prev;
    }
}

int main() // Main function
{

    Node* head = NULL; // currently the list is empty 
    append(&amp;amp;head, 6); // insert 6 in the beginning 
    push(&amp;amp;head, 7); // insert 7 before 6 
    push(&amp;amp;head, 1); 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Created Doubly linked list is: 
Traversal in forward direction 
 1  7  8  6  4 
Traversal in reverse direction 
 4  6  8  7  1 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Deletion in Doubly Linked List&lt;/strong&gt;
The deletion of nodes can be divided into three categories - head, middle and last node deletion.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Steps to be performed for deletion&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Let the node be Deleted be D.&lt;/li&gt;
&lt;li&gt;If the node to be Deleted is the head node, then change the head pointer to the next current head.&lt;/li&gt;
&lt;li&gt;Set next to previous to D, if previous to D exists.&lt;/li&gt;
&lt;li&gt;Set prev of next to D, if next to D exists.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  CPP Code to Perform Deletion Operation
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;bits/stdc++.h&amp;gt;
using namespace std;
class Node // create a doubly linked list 
{
    public:
    int data; // value of the node 
    Node* next; // next pointer of the node 
    Node* prev; // previous pointer to the node 
};

void deleteNode(Node** head_ref, Node* del) // function to delete a node in DLL
{
    /* base case */
    if (*head_ref == NULL || del == NULL)
        return;

        if (*head_ref == del)
        *head_ref = del-&amp;gt;next;

    if (del-&amp;gt;next != NULL)
        del-&amp;gt;next-&amp;gt;prev = del-&amp;gt;prev;

    if (del-&amp;gt;prev != NULL)
        del-&amp;gt;prev-&amp;gt;next = del-&amp;gt;next;

    /* Finally, free the memory occupied by del*/
    free(del);
    return;
}

void push(Node** head_ref, int new_data) // function to insert data into the DLL
{

    Node* new_node = new Node();

    /* put in the data */
    new_node-&amp;gt;data = new_data;

    new_node-&amp;gt;prev = NULL;
    new_node-&amp;gt;next = (*head_ref);

    if ((*head_ref) != NULL)
        (*head_ref)-&amp;gt;prev = new_node;

    (*head_ref) = new_node;
}

void printList(Node* node) // function to print the content of the DLL
{
    while (node != NULL) // iterate till the end of DLL 
    {
        cout &amp;lt;&amp;lt; node-&amp;gt;data &amp;lt;&amp;lt; " "; // print node data 
        node = node-&amp;gt;next;
    }
}

int main() // main function 
{
    Node* head = NULL; // currently head is NULL 

   // DLL created =  10&amp;lt;-&amp;gt;8&amp;lt;-&amp;gt;4&amp;lt;-&amp;gt;2 
    push(&amp;amp;head, 2);
    push(&amp;amp;head, 4);
    push(&amp;amp;head, 8);
    push(&amp;amp;head, 10);

    cout &amp;lt;&amp;lt; "Original Doubly Linked list ";
    printList(head); // print original DLL

    /* delete nodes from the doubly linked list */
    deleteNode(&amp;amp;head, head); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Original Doubly Linked list 10 8 4 2 
Modified Doubly Linked list 8 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Advantages of a Doubly Linked List
&lt;/h2&gt;

&lt;p&gt;As we have seen the operations of a DLL, below are the advantages - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It becomes easier to iterate in both directions.&lt;/li&gt;
&lt;li&gt;Deletion of a particular node is easy as we have access to the previous node.&lt;/li&gt;
&lt;li&gt;Performing a reverse operation is easy.&lt;/li&gt;
&lt;li&gt;A DLL can grow and shrink dynamically. &lt;/li&gt;
&lt;li&gt;It is beneficial in implementing other data structures like binary trees, hash tables, stacks, etc.&lt;/li&gt;
&lt;li&gt;It provides the flexibility to perform undo/redo operations.&lt;/li&gt;
&lt;li&gt;It can also be used in gaming like playing deck of cards.&lt;/li&gt;
&lt;li&gt;Traversing a DLL in a bidirectional manner is easy as compared to singly linked lists.&lt;/li&gt;
&lt;li&gt;In the operating system, a thread scheduler manages a doubly linked list of all the processes. &lt;/li&gt;
&lt;li&gt;The concept of DLL can be applied in forward and backward navigation areas. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Disadvantages of a Doubly Linked List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It stores an extra pointer to store the address of previously linked nodes. Hence, it consumes more memory when compared to a singly linked list. &lt;/li&gt;
&lt;li&gt;Due to extra pointers more time is required in handling the overhead. &lt;/li&gt;
&lt;li&gt;We can't randomly access elements in a doubly linked list. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;A doubly linked list has three parts: the data, next and a previous pointer. We can perform various operations on a doubly linked list like add a node in front, add a node in last, delete a node in front, delete a node in the lost, insert a node after a given node, insert a node before a given node, etc. &lt;/p&gt;

&lt;p&gt;The main benefit of a doubly linked list is its iteration in backward and forward direction. Also a DLL can shrink dynamically. Doubly linked list examples are - a music playlist in which songs can be changed by moving backward and forward, the undo and redo functionality in a word file, etc.&lt;/p&gt;

&lt;p&gt;Reference: &lt;a href="https://www.scaler.com/topics/data-structures/"&gt;Scaler Topics&lt;/a&gt; &lt;/p&gt;

</description>
      <category>programming</category>
      <category>computerscience</category>
      <category>algorithms</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What is RDBMS ? </title>
      <dc:creator>Pranjali Nandan</dc:creator>
      <pubDate>Thu, 17 Feb 2022 10:42:13 +0000</pubDate>
      <link>https://dev.to/scalertopics/what-is-rdbms--2lfg</link>
      <guid>https://dev.to/scalertopics/what-is-rdbms--2lfg</guid>
      <description>&lt;p&gt;RDBMS stands for Relational Database Management System. Also called RDB (Relational Database), it is a database that stores data in tables (rows and columns) so that it can be used to form relations with other tables. The only difference between relational databases and simple databases is that we can easily create relationships with other tables using RDB. Most databases used these days are relational databases.&lt;br&gt;
We can perform any type of operation on RDB, such as updating, deleting, viewing, and so on. Most relational databases use SQL as a language to access the database. SQL (Structured Query Language) is a programming language that is used to communicate with databases. It helps us create, update, or delete data in relational databases. The syntax of SQL is also very simple, making it easy to learn.&lt;/p&gt;

&lt;p&gt;Let’s make RDB clear with the help of a simple example. Let's assume we have two tables; one is a student table in which we have student name and student roll number, and the other table is an info table, which has roll number and total marks. So, we can make a relationship between these two tables and find out the total marks of individual students. Note that we have a common column (student roll number) in these two tables. Some of the examples of relational database management systems are mentioned below:- &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MySQL&lt;/li&gt;
&lt;li&gt;Oracle DB&lt;/li&gt;
&lt;li&gt;PostgreSQL&lt;/li&gt;
&lt;li&gt;SQLite&lt;/li&gt;
&lt;li&gt;SQL Server&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  RDBMS Operation
&lt;/h2&gt;

&lt;p&gt;A relational database management system works in tables; it stores data in the form of tables. There can be any number of tables that contain rows and tables. Every table in database has unique primary key. As we know, tables have rows and columns, so let’s understand the role of these rows and columns. So, a row records the data of an individual entity, and a column holds the data for a specific field. Using SQL, we can run a query to show some specific results. SQL has a term called SQL Constraints. They are used for some rules for data before it enters the table. Let's explore some constraints given below:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NOT NULL - It ensures that a column doesn’t have any empty values or NULL values.&lt;/li&gt;
&lt;li&gt;UNIQUE - It ensures that no two values in a given column are the same.&lt;/li&gt;
&lt;li&gt;PRIMARY KEY - A primary key is a group of columns in a table that uniquely identify the row.&lt;/li&gt;
&lt;li&gt;CHECK – This function ensures that columns adhere to any given condition.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why RDBMS?
&lt;/h2&gt;

&lt;p&gt;The relational database management system is very safe. If for any reason, our programme crashes, our data will remain safe. There are many security layers, so the data will remain safe.&lt;br&gt;
In relational databases, accessing the data is easy and if we want to perform any action like deleting, updating, etc., such a type of action is easy to implement.&lt;br&gt;
A relational database can manage large amounts of data and this data is stored in the form of rows and columns in a table.&lt;br&gt;
Using a foreign key, we can link two or more tables and then work on them according to our requirements.&lt;br&gt;
We can also give permission to multiple users so that they can work individually according to their needs.&lt;br&gt;
Management of relational databases is also very clean and effective as data is stored in the form of rows and columns.&lt;/p&gt;

&lt;h2&gt;
  
  
  RDBMS Advantages
&lt;/h2&gt;

&lt;p&gt;Below are some of the advantages of relational databases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Relational databases are very flexible in nature. Let's understand this by taking an example. Assume that we have an employee table, and if we need to update any information about an employee, rather than updating it in each file, we simply update it in the main file, and RDBMS automatically updates this information in every file of the database. By doing this, we can save a lot of time.&lt;/li&gt;
&lt;li&gt;Relational databases can be accessed at any time, and they are also very easy to use. Relational databases allow database admins to control their events, like maintaining data, updating data, etc. Backing up data is also simple with the help of RDBMS and some automation tools.&lt;/li&gt;
&lt;li&gt;The database admin of a relational database has control over the database and can give specific access to the required user. It cannot give all access to all users. Access is given according to the user’s need.&lt;/li&gt;
&lt;li&gt;As we know, relational databases use rows and columns to store their data, so tables are very comfortable for users to understand. Also, writing queries for RDBMS is easy.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  RDBMS Disadvantages
&lt;/h2&gt;

&lt;p&gt;A relational database has many advantages, but it also comes with some disadvantages. For example, to implement a relational database, we need some special software, so some special software needs to be purchased. This results in an extra cost. Also, to setup a relational database, we have to write millions of lines of code into RDBMS tables. For that, we need some extra programmers. Also, while doing this, we must take care that our data doesn’t fall into the wrong hands. Also, sometimes combining multiple tables can become more complicated in some cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Indexing in RDBMS
&lt;/h2&gt;

&lt;p&gt;To get faster access to data, indexes are used. Suppose you have an employee table with 10 employees. If we sort the data in increasing order, we can easily find out the nth salary using indexes.&lt;/p&gt;

&lt;h2&gt;
  
  
  DBMS vs. RDBMS
&lt;/h2&gt;

&lt;p&gt;Now let's explore the key difference between DBMS and Relational DBMS as given below:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In DBMS, only one user is accepted at a time, but in the case of relational databases, multiple users are allowed at a time.&lt;/li&gt;
&lt;li&gt;Relational databases necessitate greater investment in software and hardware than DBMS.&lt;/li&gt;
&lt;li&gt;As we know, relational databases make relational between multiple tables, so they can manage large amounts of data easily, but DBMS only manage a small amount of data as compared to RDBMS.&lt;/li&gt;
&lt;li&gt;A relational database stores its data in the form of tables (using rows and columns), while a DBMS stores its data in hierarchical form.&lt;/li&gt;
&lt;li&gt;Relational databases support distributed databases (it is a type of database that is present on multiple sites, i.e., on multiple computers or over a network of computers; it is not limited to only one user), while DBMS doesn’t support distributed databases.&lt;/li&gt;
&lt;li&gt;Relational databases can be normalized, but in the case of DBMS, it is difficult to normalize them.&lt;/li&gt;
&lt;li&gt;Relational databases follow ACID properties (Atomicity, Consistency, Isolation, Durability) while storing the data, while the DBMS doesn’t follow ACID properties.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For a better understanding of difference between DBMS and RDBMS, refer to &lt;a href="https://www.scaler.com/topics/difference-between-dbms-and-rdbms/"&gt;Scaler Topics&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Author - Arnav Bhardwaj&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>rdbms</category>
      <category>dbms</category>
    </item>
    <item>
      <title>How to Prevent Deadlock in DBMS?</title>
      <dc:creator>Pranjali Nandan</dc:creator>
      <pubDate>Thu, 10 Feb 2022 11:39:48 +0000</pubDate>
      <link>https://dev.to/scalertopics/how-to-prevent-deadlock-in-dbms-27f9</link>
      <guid>https://dev.to/scalertopics/how-to-prevent-deadlock-in-dbms-27f9</guid>
      <description>&lt;p&gt;Before proceeding directly to the prevention of deadlock in DBMS, let's understand what deadlock is. Deadlock is a condition where one or more processes are waiting for a resource indefinitely, where this resource is acquired by another process.&lt;br&gt;
For greater clarity, consider the following scenario: we have two resources, say R1 and R2, and we can have two processes, say P1 and P2. P1 has R2 resource, and P2 has R1 resource; therefore, P1 requires both R1 and R2 resources to complete its process, and P2 requires both R1 and R2 resources to complete its process. P1 process now holds R2 resource and waits indefinitely for R1 resource, similar to P2 process. So, these conditions are deadlock conditions where some processes are blocked because they hold some resources and are waiting for other processes that were held by other processes, so this waiting will never end until resolved.&lt;/p&gt;

&lt;p&gt;So, we need to understand the conditions that can lead to a deadlock. So, according to Coffman, there are four conditions where deadlock can occur, which are mentioned below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mutual exclusion&lt;/li&gt;
&lt;li&gt;Circular waiting situation&lt;/li&gt;
&lt;li&gt;Condition of hold and wait&lt;/li&gt;
&lt;li&gt;There is no preemption condition.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Prevention of Deadlock
&lt;/h2&gt;

&lt;p&gt;When any transaction starts to execute, the database management system will inspect all its operations so that a deadlock condition will not occur. If it finds in any condition that a deadlock condition can occur, then that transaction is not allowed to be executed.&lt;br&gt;
We can avoid deadlock by avoiding one or more of the four Coffman's conditions.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Removing mutual exclusion, i.e., all the resources must be shareable.&lt;/li&gt;
&lt;li&gt;Removing the hold and wait condition&lt;/li&gt;
&lt;li&gt;Preemption of resources&lt;/li&gt;
&lt;li&gt;Avoid the condition of circular waiting.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Methods for Preventing Deadlocks
&lt;/h2&gt;

&lt;p&gt;Deadlock prevention is very suitable for large databases. If all the resources were arranged in such a manner that the process did not need to wait for resources, then the deadlock could be prevented. The database management system analyses the situation of operations of a transaction and if there is any chance of deadlock occurring, then that transaction will never be executed. Let’s look at some methods of prevention:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Wait-Die&lt;/li&gt;
&lt;li&gt;Wound-Wait&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Wait-Die&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The Wait-Die scheme is a non-preemptive technique to use for preventing deadlock. Suppose we have two transactions, T1 and T2.&lt;br&gt;
When T1 requests data currently held by transaction T2, in this case, T1 is allowed to wait if and only if its timestamp is smaller than the timestamp of T2.&lt;br&gt;
(In simple words, it means T1 is older than T2). If not, then T1 is going to die or be killed.&lt;br&gt;
That is why this scheme is known as the "WAIT-DIE SCHEME."&lt;/p&gt;

&lt;p&gt;In this technique, two possibilities may occur:&lt;/p&gt;

&lt;p&gt;a) T1 timestamp &amp;lt; T2 timestamp = It means if T1 is older than T2, then T1 is allowed to wait until the resource is not provided to T1.&lt;br&gt;
b) T1 timestamp &amp;gt; T2 timestamp = It means if T1 is younger than T2, then T1 is going to die. It is not allowed to wait.&lt;/p&gt;

&lt;p&gt;So more precisely, we can say that if the transaction is older than the other transaction, then it has the right to wait, otherwise it is going to die.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
Let's consider this example. It will clear up all your doubts and the above theory in a simple manner.&lt;/p&gt;

&lt;p&gt;Suppose we have a transaction T1, T2, T3 having a timestamp of 2, 4, 6, respectively.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If T1 requests a resource that is held by T2, then T1 has the right to wait. What's the deal? because the T1 timestamp is earlier than the T2 timestamp&lt;/li&gt;
&lt;li&gt;If T3 requests a resource that is held by T2, then T3 is going to die. What's the deal? because the T3 timestamp is more recent than the T2 timestamp&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope it is clear to all of you now.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Wound-Wait&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The Wound-Wait scheme is a preemtive technique used in the prevention of deadlock in DBMS. It is the total opposite of the wait-die scheme.&lt;br&gt;
Suppose we have two transactions, T1 and T2. If T1 requests data or a resource which is held by T2, then T1 is allowed to wait if and only if T1's timestamp is greater than T2. Otherwise, in this case, T2 is going to die instead of T1.&lt;br&gt;
That means T2 is wounded by T1. That is why this scheme is known as the "Wound-wait" scheme in DBMS.&lt;/p&gt;

&lt;p&gt;In this technique, 2 possibilities may occur, like in the wait-die scheme:&lt;br&gt;
a) T1 timestamp &amp;lt; T2 timestamp = In this case, T2 is going to be killed by T1 and it will restart at a random delay but with the same timestamp.&lt;br&gt;
b) T1 timestamp &amp;gt; T2 timestamp =It means if T1 is older than T2, then T1 is allowed to wait until the resource is not provided to T1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
Let's consider this example. It will clear up all your doubts and the above theory.&lt;br&gt;
Suppose we have a transaction T1, T2, T3 having a timestamp of 2, 4, 6, respectively.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If T1 requests a resource that is held by T2, then T1 is going to force T2 and it will wound T2.&lt;/li&gt;
&lt;li&gt;If T3 requests a resource that is held by T2, then T3 has the right to wait.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a better understanding of deadlocks, Read more about it on &lt;a href="https://www.scaler.com/topics/dbms/deadlock-in-dbms/"&gt;Scaler Topics&lt;/a&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>programming</category>
    </item>
    <item>
      <title>Virtual base class in c++</title>
      <dc:creator>Amod Nazirkar</dc:creator>
      <pubDate>Fri, 04 Feb 2022 06:07:55 +0000</pubDate>
      <link>https://dev.to/scalertopics/virtual-base-class-in-c-50o6</link>
      <guid>https://dev.to/scalertopics/virtual-base-class-in-c-50o6</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Let’s start with an example of the hybrid inheritance, in which there is a base class, let's say A, and it has two derived classes, let's say class B and class C. Now as there is hybrid inheritance so there may be another class, let’s say D which inherits properties of both class B and class C.&lt;/p&gt;

&lt;p&gt;As a child class can access the member functions or properties of the parent class or even the properties of the parent class’s parents which implies D can access the member functions of its parents B and C, also there parent A. But the problem is A is the parent of both B and C so there are two ways for objects of class D to access the properties of A either via class B or via class C. When a function call is made from object of class D to the function of class A then the compiler will get confused about the path to approach class A this leads to an error. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let’s see the code of above defined scenario&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;bits/stdc++.h&amp;gt;
&lt;/span&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// defining class A&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;hello_A&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;cout&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s"&gt;"This is class A"&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// defining class B&lt;/span&gt;
&lt;span class="c1"&gt;// class B inherits from class A&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;hello_B&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;cout&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s"&gt;"This is class B"&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// defining class C&lt;/span&gt;
&lt;span class="c1"&gt;// class C inherits from class A&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;C&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;hello_C&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;cout&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s"&gt;"This is class C"&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// defining class D&lt;/span&gt;
&lt;span class="c1"&gt;// class D inherits from both class B and C&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;D&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;hello_D&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;cout&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s"&gt;"This is class D"&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// creating the object of class D&lt;/span&gt;
    &lt;span class="n"&gt;D&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

    &lt;span class="c1"&gt;// calling the function of the class A using object of class D&lt;/span&gt;
    &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hello_A&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Error&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;HelloWorld&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpp&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;In&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
&lt;span class="n"&gt;HelloWorld&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpp&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;52&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;member&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;hello_A&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;ambiguous&lt;/span&gt;
     &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hello_A&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
         &lt;span class="o"&gt;^~~~~~~&lt;/span&gt;
&lt;span class="n"&gt;HelloWorld&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpp&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;note&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;candidates&lt;/span&gt; &lt;span class="n"&gt;are&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;hello_A&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
     &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;hello_A&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
          &lt;span class="o"&gt;^~~~~~~&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We get an ambiguous error here as the compiler has no idea which path it has to choose to reach class A. This problem is known as diamond problem, when there is diamond-like shape in our hybrid inheritance then there will be ambiguous error and to remove this error we define the parent class as the virtual class with the help of virtual keyword which means virtual base class is a way of defining virtual inheritance to prevent the multiple instances of a class.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to declare virtual base class in C++?
&lt;/h2&gt;

&lt;p&gt;Virtual base class is declared with the help of the virtual keyword.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;Syntax&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Adding&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;keyword&lt;/span&gt; &lt;span class="n"&gt;before&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;keyword&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;child_class&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;parent_class&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="n"&gt;Syntax&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Adding&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;keyword&lt;/span&gt; &lt;span class="n"&gt;after&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;keyword&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;chid_class&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;parent_class&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above syntax we defined a child class which inherits from the parent class public members and to make it virtual base class we just added virtual keyword by two ways either before public keyword or before access specifier or we can define it after access specifier as we have done in second syntax. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let’s implement our above code by declaring class A as virtual parent class&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;bits/stdc++.h&amp;gt;
&lt;/span&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// defining class A&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;hello_A&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;cout&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s"&gt;"This is class A"&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// defining class B&lt;/span&gt;
&lt;span class="c1"&gt;// class B inherits from class A&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;hello_B&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;cout&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s"&gt;"This is class B"&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// defining class C&lt;/span&gt;
&lt;span class="c1"&gt;// class C inherits from class A&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;C&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt;  &lt;span class="n"&gt;A&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;hello_C&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;cout&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s"&gt;"This is class C"&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// defining class D&lt;/span&gt;
&lt;span class="c1"&gt;// class D inherits from both class B and C&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;D&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;hello_D&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;cout&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s"&gt;"This is class D"&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// creating the object of class D&lt;/span&gt;
    &lt;span class="n"&gt;D&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

    &lt;span class="c1"&gt;// calling the function of the class A using object of class D&lt;/span&gt;
    &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hello_A&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;This&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We rewrite the code explained in the above section, only change is we define the base class (class A) as a virtual class using the virtual keyword while inheriting it in class B and class C that makes our code work properly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Virtual base class is a way of defining virtual inheritance to prevent the multiple instances of a class.&lt;/li&gt;
&lt;li&gt;When there is diamond-like shape in hybrid-inheritance, then there is a chance to get errors which can be prevented by declaring base class as virtual.&lt;/li&gt;
&lt;li&gt;To declare a virtual class, a virtual keyword is used while inheriting it in child class.&lt;/li&gt;
&lt;li&gt;To discover the virtual-base-class topic covered in detail please refer to this &lt;a href="https://www.scaler.com/topics/virtual-base-class-in-cpp/"&gt;Scaler&lt;/a&gt; link.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>cpp</category>
      <category>programming</category>
      <category>computerscience</category>
      <category>learncpp</category>
    </item>
    <item>
      <title>Program to Implement Insertion Sort in Data Structure</title>
      <dc:creator>Bikash Daga</dc:creator>
      <pubDate>Wed, 05 Jan 2022 13:33:12 +0000</pubDate>
      <link>https://dev.to/scalertopics/program-to-implement-insertion-sort-in-data-structure-a0i</link>
      <guid>https://dev.to/scalertopics/program-to-implement-insertion-sort-in-data-structure-a0i</guid>
      <description>&lt;p&gt;Sorting is a technique to arrange the data in ascending, descending order or lexicographical order. Using an efficient sorting algorithm can improve the time complexity of the code. &lt;/p&gt;

&lt;p&gt;The insertion sort is a famous and easy problem that is asked in various product based companies and semester exams. In this article, you will learn about insertion sort with real life examples with code demonstration. &lt;/p&gt;

&lt;h2&gt;
  
  
  Definition of insertion sort
&lt;/h2&gt;

&lt;p&gt;Insertion sort is defined as a sorting algorithm that takes the ith element and places it in the correct position in the array. The insertion sort algorithm performs the same operation on the whole array by picking the element one by one and storing it in the right place. &lt;/p&gt;

&lt;p&gt;One real world example of insertion sort is derived from playing cards in school. When a pack of cards is taken in hand, we take one card and place it in the correct position behind the large numbers. &lt;/p&gt;

&lt;p&gt;Another example of insertion sort algorithm is placing suits at a garment store. &lt;/p&gt;

&lt;p&gt;Another example of insertion sort algorithm is placing suits at a garment store. &lt;/p&gt;

&lt;p&gt;In the cupboard, the suits are placed in sorted order and the lower sized suits are placed below all the higher ones. &lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages of insertion sort algorithm
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Simple -&lt;/strong&gt; The insertion sort code is easier to understand and implement.    &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Efficient for smaller data set -&lt;/strong&gt; The algorithm sounds perfect for small sized arrays as we have not to search the exact place for the element. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Adaptive -&lt;/strong&gt; The algorithm works appropriate for the sorted data sets.&lt;/p&gt;

&lt;h2&gt;
  
  
  Insertion sort example
&lt;/h2&gt;

&lt;p&gt;Let us try to understand this algorithm working by taking the following array as an example - &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arr[] = [9, 6, 7, 3, 4]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Operating the element arr[0]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We mark the section blue as sorted and the orange as unsorted, So we operare 9 and since this is the first element in the left array it remains sorted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arr[] = [9, 6, 7, 3, 4]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Operating the element arr[1]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now we take 6 and mark it as blue. So the left array has 2 elements 9 and 6. Since 6 is less than 9 we interchange to sort the left array. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arr[] = [6, 9, 7, 3, 4]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Operating the element arr[2]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now we take 7 and mark it as blue. So the left array has 3 elements 9, 6, 7. Since 7 is less than 9 and greater than 6 we interchange to sort the left array. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arr[] = [ 6,7, 9, 3, 4]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Operating the element arr[3]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now we take 3 and mark it as blue. So the left array has 4 elements 9, 6, 7, 3. Since 3 is the smallest we place it in the beginning. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arr[] = [3, 6, 7, 9, 4]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Operating the element arr[4]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now we take the last element. 4 comes after 3 so we place it in second position.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arr[] = [3, 4, 6, 7, 9]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For a better understanding in a picture mode you can visit &lt;/p&gt;

&lt;h2&gt;
  
  
  Pseudocode for insertion sort algorithm
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;insertionSortAlgorithm&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;mark&lt;/span&gt; &lt;span class="nc"&gt;First&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="n"&gt;as&lt;/span&gt; &lt;span class="n"&gt;sorted&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="n"&gt;comes&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="n"&gt;unsorted&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="no"&gt;A&lt;/span&gt;
        &lt;span class="nc"&gt;Store&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="no"&gt;A&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;lastSortedIndex&lt;/span&gt; &lt;span class="n"&gt;down&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;A&lt;/span&gt;
                &lt;span class="n"&gt;move&lt;/span&gt; &lt;span class="n"&gt;sorted&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt; &lt;span class="n"&gt;loop&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="n"&gt;insert&lt;/span&gt; &lt;span class="no"&gt;A&lt;/span&gt; &lt;span class="n"&gt;here&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="n"&gt;insertionSortAlgorithm&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Line 2: The first element is single so it is already in sorted format. &lt;br&gt;
Line 3: Run a loop from start to end to operate each element in the array. &lt;br&gt;
Line 4: Extract the element at position i i.e. array[i]. Let it be called A.&lt;br&gt;
Line 5: To compare A with its right elements by running the loop j from i-1 to 0&lt;br&gt;
Line 6, 7: Compare A with the left element, if A is lesser, then move arr[j] to right by 1.&lt;br&gt;
Line 8: After finding the appropriate index for A insert into that position. &lt;/p&gt;

&lt;h2&gt;
  
  
  C code of insertion sort algorithm
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="n"&gt;include&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;stdio&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;h&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// Function for insertion sort&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Insertion_Sort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[],&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt;
    &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
        &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// Do swapping&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
            &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;--;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Function to print elements of array&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Print_Array&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[],&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d\t"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]);&lt;/span&gt;

    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"\n"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Driver Function&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;scanf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;scanf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;Insertion_Sort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;Print_Array&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt;&lt;br&gt;
num = 6&lt;br&gt;
array = [1, 6, 3, 3, 5, 2]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
[1, 2, 3, 3, 5, 6]&lt;/p&gt;

&lt;h2&gt;
  
  
  CPP Code for Insertion Sort Algorithm
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="n"&gt;include&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;iostream&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Function for insertion sort&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Insertion_Sort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[],&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt;
    &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
        &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// Do swapping&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
            &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;--;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Function to print elements of array&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Print_Array&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[],&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt;
        &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Driver Function&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;cin&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;cin&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;Insertion_Sort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;Print_Array&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt;&lt;br&gt;
num = 6&lt;br&gt;
array = [1, 6, 3, 3, 5, 2]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
[1, 2, 3, 3, 5, 6]&lt;/p&gt;

&lt;h2&gt;
  
  
  Java Code for Insertion Sort Algorithm
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Scanner&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Insertion_Sort_algo&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// function for insertion sort&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;InsertionSort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt;
        &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
            &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

            &lt;span class="c1"&gt;// Do swapping&lt;/span&gt;
            &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
                &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;--;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;

            &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// function to print array&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Print_Array&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="nc"&gt;InsertionSort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;Print_Array&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt;&lt;br&gt;
num = 6&lt;br&gt;
array = [1, 6, 3, 3, 5, 2]&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
[1, 2, 3, 3, 5, 6]&lt;/p&gt;

&lt;h2&gt;
  
  
  Time Complexity of Insertion Sort Algorithm
&lt;/h2&gt;

&lt;p&gt;From the code we have seen that insertion sort performs two operations - iterating in the right array and interchanging with the left one. This combined operation contributes to the time complexity of the algorithm. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Best case&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In best case the time complexity of insertion sort algorithm is O(N). This is the case when the whole array is already sorted. It becomes N case time complexity as the inner loop never gets executed. The outer loop will check and scan for each element which makes the time complexity to O(N). &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Worst case&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The worst case time complexity is O(N*N). This is the case when the whole array is unsorted. In this case the inner loop will be executed till the end of the right array in order to place the arr[i] in the correct position. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Average case&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;The average case time complexity is O(N*N). In this case, elements are mixed up neither fully sorted nor fully unsorted. &lt;/p&gt;

&lt;h2&gt;
  
  
  Space Complexity of Insertion Sort Algorithm
&lt;/h2&gt;

&lt;p&gt;The space complexity of insertion sort algorithm is constant as it takes only one or two variables in the entire array. So, the space complexity of insertion sort algorithm is O(1). &lt;/p&gt;

&lt;p&gt;Read more about Insertion Sort on &lt;a href="https://www.scaler.com/topics/insertion-sort/"&gt;Scaler Topics&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy Learning!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>computerscience</category>
      <category>algorithms</category>
      <category>softwareengineering</category>
    </item>
  </channel>
</rss>
