<?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: Akshat chaturvedi</title>
    <description>The latest articles on DEV Community by Akshat chaturvedi (@akshat3358).</description>
    <link>https://dev.to/akshat3358</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%2Fuser%2Fprofile_image%2F527644%2F6ff3609c-3d45-4d6a-b0e9-81dfbb79b9b7.jpg</url>
      <title>DEV Community: Akshat chaturvedi</title>
      <link>https://dev.to/akshat3358</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akshat3358"/>
    <language>en</language>
    <item>
      <title>Sorting Algorithm</title>
      <dc:creator>Akshat chaturvedi</dc:creator>
      <pubDate>Sat, 02 Oct 2021 08:04:02 +0000</pubDate>
      <link>https://dev.to/akshat3358/sorting-algorithm-3ml4</link>
      <guid>https://dev.to/akshat3358/sorting-algorithm-3ml4</guid>
      <description>&lt;h2&gt;
  
  
  Bubble Sorting Algorithm
&lt;/h2&gt;

&lt;p&gt;Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order i.e element at ith index is greater than element at jth index where i&amp;lt;j.&lt;/p&gt;

&lt;h4&gt;
  
  
  Bubble Sort Implementation
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void bubbleSort(int arr[], int n)
{
    int i, j;
    for (i = 0; i &amp;lt; n-1; i++){
        for (j = 0; j &amp;lt; n-i-1; j++){
            if (arr[j] &amp;gt; arr[j+1])
                swap(&amp;amp;arr[j], &amp;amp;arr[j+1]);
        }      
    }    
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Insertion Sorting Algorithm
&lt;/h2&gt;

&lt;p&gt;Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part.&lt;/p&gt;

&lt;h6&gt;
  
  
  Algorithm
&lt;/h6&gt;

&lt;p&gt;To sort an array of size n in ascending order: &lt;br&gt;
1: Iterate from arr[1] to arr[n] over the array. &lt;br&gt;
2: Compare the current element (key) to its predecessor. &lt;br&gt;
3: If the key element is smaller than its predecessor, compare it to the elements before. Move the greater elements one position up to make space for the swapped element.&lt;/p&gt;

&lt;h4&gt;
  
  
  Insertion Sorting Algorithm Implementation
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void insertionSort(int arr[], int n)
{
    int i, key, j;
    for (i = 1; i &amp;lt; n; i++)
    {
        key = arr[i];
        j = i - 1;
        while (j &amp;gt;= 0 &amp;amp;&amp;amp; arr[j] &amp;gt; key)
        {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  #Note :- Rest Of the Algorithms will understand in next blog.
&lt;/h6&gt;

</description>
    </item>
    <item>
      <title>All About Merge Sort Algorithm</title>
      <dc:creator>Akshat chaturvedi</dc:creator>
      <pubDate>Sat, 15 May 2021 20:09:18 +0000</pubDate>
      <link>https://dev.to/akshat3358/all-about-merge-sort-algorithm-4fk</link>
      <guid>https://dev.to/akshat3358/all-about-merge-sort-algorithm-4fk</guid>
      <description>&lt;h2&gt;
  
  
  Introduction to Merge Sort Algorithm
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; Merge Sort  works on the Priciple of &lt;strong&gt;Divide And Conquer&lt;/strong&gt; Algorithm to sort the group of element.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Question :- What do you understand by the term Divide And Conquer?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Answer&lt;/strong&gt; :- Divide And Conquer follows these three steps :-  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.Divide&lt;/strong&gt; refers to dividing the main Problem into Sub-Problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.Conquer&lt;/strong&gt; refers to solving the Sub-Problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.Combine&lt;/strong&gt; refers to combining the Solution of Sub-Problem to get the Solution of main Problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.&lt;/strong&gt; It is a &lt;strong&gt;Stable&lt;/strong&gt; Algorithm .&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Question&lt;/strong&gt; :-  What is the meaning of Stable ?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Answer&lt;/strong&gt; :- Suppose , We are Sorting the Array in which there are two element which are same , In this case , What  Merge sort Algorithm does that It maintains the original order if it finds two same element in an Array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; The Time complexity for Merge Sort Algorithm :-&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Worst&lt;/strong&gt; &lt;strong&gt;Case&lt;/strong&gt; :- O(n*logn)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Averge&lt;/strong&gt; &lt;strong&gt;Case&lt;/strong&gt; :- O(n*logn)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Best&lt;/strong&gt; &lt;strong&gt;Case&lt;/strong&gt; :- O(n*logn)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.&lt;/strong&gt; The Auxiliary Space for Merge Sort Algorithm is O(n).&lt;/p&gt;

&lt;h2&gt;
  
  
  Working of Merge Sort Algorithm
&lt;/h2&gt;

&lt;p&gt;*Here are the following steps that Merge Sort follows :-&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; Take two variable that hold the index of starting and last index of the array and take another variable that hold the value of middle index of the array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.&lt;/strong&gt; The index value of left half of array will be from low to mid and the index value of right half of array will be from mid+1 to high.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; Now , Divide the array into two halves (i.e Left and Right Half).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.&lt;/strong&gt; Repeat this untill you get single element in both (left and right half).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5.&lt;/strong&gt; After this , With the help of Merge function of Merge Sort Algorithm will compare , sort and Merge the array of element of both halves(i.e left and right half).&lt;/p&gt;

&lt;p&gt;Now ,let's look at the code of Merge Function of Merge Sort Algorithm :-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void merge(int array[] , int low , int mid , int high)
{
    int n1 = mid - low + 1 ;

    //n --&amp;gt; size of array of left half

    int n2 = high - mid ;

    //n2 --&amp;gt; size of array of right half

    int left[n1];

    //Array of left half

    int right[n2];

    //Array of right half

    for(int i=0;i&amp;lt;n1;i++){

        left[i] = array[low+i];

        //storing the left half of array into a temporary array

    }

    for(int i=0;i&amp;lt;n2;i++){

        right[i] = array[n1+i];

        //Storing the right half of array into a temporary array

    }

    //Till now , we have setup the Auxiliary space for our Array

    //Now , We will compare the element in both in left and right 

    //half And insert that element in the original which is 

    //smaller

    int i=0,j=0,k=0;

    while(i&amp;lt;n1 &amp;amp;&amp;amp; j&amp;lt;n2){// Here , we are traversing both the array

    //If we have traversed any of the array otherone is not fully 

    //traversed yet , then we will come out of this loop 

        if( left[i] &amp;lt;= right[j] ){

        //If the element in left half is smaller than the element

        //in right half , then insert and insert the index value 

        //of left half and original array 

            array[k] = left[i];

            i++;

            k++;

        }

        else{

        //Else Insert the element of right half and increase the

        //index value of right half and original array

            array[k] = right[j];

            j++;

            k++;

        }

    }

    //If the above loop ends , Before traversing the both Array

    //then we will traverse that array which is not traverse 

    //completly yet and insert the elements of that array in to

    //original Array inthe same order.

    while(i&amp;lt;n1){

        array[k] = left[i];

        i++;

        k++;

    }

    while(j&amp;lt;n2){

        array[k] = right[j];

        j++;

        k++;

    }

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

&lt;/div&gt;



&lt;p&gt;Now let's give a walkthrough of working of Merge Sort Algorithm using the example of this Array , arr[] = {14,7,3,12,9,11,6,12} :-&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%2Fpr1fa43ifd85blqillrf.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%2Fpr1fa43ifd85blqillrf.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, here in the above walkthrough ,we labelled the low index as p ,high index as r and mid index as q . So ,Now firstly look at the code of Merge Sort Algorithm then we will cover both the code and the walkthrough .&lt;/p&gt;

&lt;p&gt;Implementation of Merge Sort Algorithm :-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void mergesort( int array[] , int low ,int high ){
    if( high &amp;gt; low ){

        //we will recursively call this mergesort function    

        //for the left and right half of the array untill 

        //the high index is greater than the low index

        int mid_index = low + ( high - low )/2;

        mergesort(array,low,mid_index);

        //This recursive call is for left half of Array

        mergesort(array,mid_index+1,high);

        //This recursive is for right half of Array

        merge(array,low,mid_index,high);

        //The above call is to merge function compare and 
        //sort and merge the array 

    }

}

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

&lt;/div&gt;



&lt;p&gt;So ,When we apply the merge sort algorithm to above given array ,It does these steps :- &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; Find the middle index and divide the whole array into two halves ( left and right halves ).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.&lt;/strong&gt; BY call the merge sort function for the left and right half of the array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; When the high index is less than or equal to low index then we stop call the merge sort function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.&lt;/strong&gt; Now ,We call the merge fucntion to compare , sort and merge the array.&lt;/p&gt;

&lt;p&gt;And that's how we got the sorted Array...&lt;/p&gt;

&lt;h2&gt;
  
  
  Analysis of Merge sort Algorithm
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; The recursive calling part of merge sort fucntion takes O(1) time to break the array in two halves.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.&lt;/strong&gt; But the merge fucntion of merge sort algorithm take O(n) time to compare , sort and merge the array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; Now , Suppose there are x levels in your tree and at each level you are doing the  work in O(n) time then the total time taken by this algorithm is :---&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              T(n) = x * O(n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here, x is no. of level in the tree , So As we are dividing the array into two halves again and again untill we got single element ,So For this , The time taken by the algorithm to divide the array into two halves is O(log2n).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.&lt;/strong&gt; So , The Total time taken by the algorithm :--&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              T(n) = O(n*logn)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So with this , I will wrap-up this post , I hope you understand the whole Algorithm .&lt;/p&gt;

&lt;p&gt;Thankyou so much for Reading the post :-).&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>sorting</category>
      <category>mergesort</category>
      <category>problemsolving</category>
    </item>
  </channel>
</rss>
