{"id":564,"date":"2021-07-07T14:52:26","date_gmt":"2021-07-07T14:52:26","guid":{"rendered":"https:\/\/xcodeph.net\/?page_id=564"},"modified":"2021-07-07T15:32:52","modified_gmt":"2021-07-07T15:32:52","slug":"sorting","status":"publish","type":"page","link":"https:\/\/xcodeph.net\/index.php\/sorting\/","title":{"rendered":"Sorting"},"content":{"rendered":"\n<h2>Bubble Sort<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Bubble Sort is the simplest of the sorting techniques.<\/p>\n\n\n\n<p>In the bubble sort technique, each of the elements in the list is compared to its adjacent element. Thus if there are n elements in list A, then A[0] is compared to A[1], A[1] is compared to A[2] and so on.<\/p>\n\n\n\n<p>After comparing if the first element is greater than the second, the two elements are swapped then.<\/p>\n\n\n\n<p>Bubble Sort Technique<\/p>\n\n\n\n<p>Using the bubble sort technique, sorting is done in passes or iteration. Thus at the end of each iteration, the heaviest element is placed at its proper place in the list. In other words, the largest element in the list bubbles up.<\/p>\n\n\n\n<p><strong>We have given a general algorithm of bubble sort technique below.<\/strong><\/p>\n\n\n\n<p>General Algorithm<\/p>\n\n\n\n<p><strong>Step 1<\/strong>: For i = 0 to N-1 repeat Step 2<\/p>\n\n\n\n<p><strong>Step 2<\/strong>: For J = i + 1 to N \u2013 I repeat<\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Step 3<\/strong>: if A[J] &gt; A[i]<\/p>\n\n\n\n<p>Swap A[J] and A[i]<\/p>\n\n\n\n<p>[End of Inner for loop]<\/p>\n\n\n\n<p>[End if Outer for loop]<\/p>\n\n\n\n<p><strong>Step 4<\/strong>: Exit<\/p>\n\n\n\n<p>Here is a pseudo-code for bubble sort algorithm, where we traverse the list using two iterative loops.<\/p>\n\n\n\n<p>In the first loop, we start from the 0th&nbsp;element and in the next loop, we start from an adjacent element. In the inner loop body, we compare each of the adjacent elements and swap them if they are not in order. At the end of each iteration of the outer loop, the heaviest element bubbles up at the end.<\/p>\n\n\n\n<p><strong>Pseudocode<\/strong><\/p>\n\n\n\n<p>Procedure bubble_sort (array , N)<br>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; array \u2013 list of items to be sorted<br>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; N \u2013 size of array<br>begin<br>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; swapped = false<br>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; repeat<br>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for I = 1 to N-1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if array[i-1] &gt; array[i] then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; swap array[i-1] and array[i]<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; swapped = true<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end if<br>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end for<br>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; until not swapped<br>end procedure<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"593\" height=\"575\" src=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-3.png\" alt=\"\" class=\"wp-image-568\" srcset=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-3.png 593w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-3-300x291.png 300w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-3-309x300.png 309w\" sizes=\"(max-width: 593px) 100vw, 593px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"593\" height=\"600\" src=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image.png\" alt=\"\" class=\"wp-image-565\" srcset=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image.png 593w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-297x300.png 297w\" sizes=\"(max-width: 593px) 100vw, 593px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"593\" height=\"601\" src=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-2.png\" alt=\"\" class=\"wp-image-567\" srcset=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-2.png 593w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-2-296x300.png 296w\" sizes=\"(max-width: 593px) 100vw, 593px\" \/><\/figure>\n\n\n\n<p>Array entirely sorted.<\/p>\n\n\n\n<p><strong>The above illustration can be summarized in a tabular form as shown below:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"877\" height=\"461\" src=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-1.png\" alt=\"\" class=\"wp-image-566\" srcset=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-1.png 877w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-1-300x158.png 300w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-1-768x404.png 768w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-1-400x210.png 400w\" sizes=\"(max-width: 877px) 100vw, 877px\" \/><\/figure>\n\n\n\n<p>As shown in the illustration, with every pass, the largest element bubbles up to the last thereby sorting the list with every pass. As mentioned in the introduction, each element is compared to its adjacent element and swapped with one another if they are not in order.<\/p>\n\n\n\n<p>Thus as shown in the illustration above, at the end of the first pass, if the array is to be sorted in ascending order, the largest element is placed at the end of the list. For the second pass, the second largest element is placed at the second last position in the list and so on.<\/p>\n\n\n\n<p>When we reach N-1 (where N is a total number of elements in the list) passes, we will have the entire list sorted.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2>SELECTION SORT<\/h2>\n\n\n\n<p>The selection sort technique first selects the smallest element in the array and swaps it with the first element in the array.<\/p>\n\n\n\n<p>Next, it swaps the second smallest element in the array with the second element and so on. Thus for every pass, the smallest element in the array is selected and put in its proper position until the entire array is sorted.<\/p>\n\n\n\n<p>Selection sort is quite a straightforward sorting technique as the technique only involves finding the smallest element in every pass and placing it in the correct position.<\/p>\n\n\n\n<p>Selection sort works efficiently when the list to be sorted is of small size but its performance is affected badly as the list to be sorted grows in size.<\/p>\n\n\n\n<p>Hence we can say that selection sort is not advisable for larger lists of data.<\/p>\n\n\n\n<p>General Algorithm<\/p>\n\n\n\n<p>The General Algorithm for selection sort is given below:<\/p>\n\n\n\n<p><strong>Selection Sort (A, N)<\/strong><\/p>\n\n\n\n<p><strong>Step 1<\/strong>: Repeat Steps 2 and 3 for K = 1 to N-1<\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Step 2<\/strong>: Call routine smallest(A, K, N,POS)<\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Step 3<\/strong>: Swap A[K] with A [POS]<\/p>\n\n\n\n<p>[End of loop]<\/p>\n\n\n\n<p><strong>Step 4<\/strong>: EXIT<\/p>\n\n\n\n<p><strong>Routine smallest (A, K, N, POS)<\/strong><\/p>\n\n\n\n<ul><li><strong>Step 1<\/strong>: [initialize] set smallestElem = A[K]<\/li><li><strong>Step 2<\/strong>: [initialize] set POS = K<\/li><li><strong>Step 3<\/strong>: for J = K+1 to N -1,repeat<br>if smallestElem &gt; A [J]<br>set smallestElem = A [J]<br>set POS = J<br>[if end]<br>[End of loop]<\/li><li><strong>Step 4<\/strong>: return POS<\/li><\/ul>\n\n\n\n<p>Pseudocode For Selection Sort<\/p>\n\n\n\n<p>Procedure selection_sort(array,N)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;array \u2013 array of items to be sorted<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;N \u2013 size of array<\/p>\n\n\n\n<p>begin<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>for <\/strong>I = 1 to N-1<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;begin<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set min&nbsp; = i<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>for <\/strong>j = i+1 to N<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>if <\/strong>array[j] &lt; array[min] then<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;min = j;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end <strong>if<\/strong><\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end <strong>for<\/strong><\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/swap the minimum element with current element<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>if <\/strong>minIndex != I then<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;swap array[min[] and array[i]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end <strong>if<\/strong><\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;end <strong>for<\/strong><\/p>\n\n\n\n<p>end procedure<\/p>\n\n\n\n<p>An example to illustrate this selection sort algorithm is shown below.<\/p>\n\n\n\n<p>Illustration<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"655\" height=\"549\" src=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-7.png\" alt=\"Selection Sort Illustration\" class=\"wp-image-573\" srcset=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-7.png 655w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-7-300x251.png 300w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-7-358x300.png 358w\" sizes=\"(max-width: 655px) 100vw, 655px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-6.png\" alt=\"Pass2\" class=\"wp-image-572\" width=\"653\" height=\"518\" srcset=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-6.png 628w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-6-300x238.png 300w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-6-378x300.png 378w\" sizes=\"(max-width: 653px) 100vw, 653px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-4.png\" alt=\"Pass3\" class=\"wp-image-570\" width=\"659\" height=\"349\" srcset=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-4.png 638w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-4-300x159.png 300w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-4-400x212.png 400w\" sizes=\"(max-width: 659px) 100vw, 659px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-5.png\" alt=\"Pass4\" class=\"wp-image-571\" width=\"659\" height=\"273\" srcset=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-5.png 555w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-5-300x124.png 300w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-5-400x166.png 400w\" sizes=\"(max-width: 659px) 100vw, 659px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-4.png\" alt=\"\" class=\"wp-image-569\" width=\"731\" height=\"387\" srcset=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-4.png 638w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-4-300x159.png 300w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-4-400x212.png 400w\" sizes=\"(max-width: 731px) 100vw, 731px\" \/><\/figure>\n\n\n\n<p>From the illustration, we see that with every pass the next smallest element is put in its correct position in the sorted array. From the above illustration, we see that in order to sort an array of 5 elements, four passes were required. This means in general, to sort an array of N elements, we need N-1 passes in total.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2><strong>&nbsp;Insertion Sort<\/strong><\/h2>\n\n\n\n<p>Insertion sort is a sorting technique which can be viewed in a way which we play cards at hand. The way we insert any card in a deck or remove it, insertion sorts works in a similar way.<\/p>\n\n\n\n<p>Insertion sort algorithm technique is more efficient than the Bubble sort and Selection sort techniques but is less efficient than the other techniques like Quicksort and Merge sort.<\/p>\n\n\n\n<p>In the insertion sort technique, we start from the second element and compare it with the first element and put it in a proper place. Then we perform this process for the subsequent elements.<\/p>\n\n\n\n<p>We compare each element with all its previous elements and put or insert the element in its proper position. Insertion sort technique is more feasible for arrays with a smaller number of elements. It is also useful for sorting linked lists.<\/p>\n\n\n\n<p>Linked lists have a pointer to the next element (in case of a singly linked list) and a pointer to the previous element as well (in case of a doubly linked list). Hence it becomes easier to implement insertion sort for a linked list.<\/p>\n\n\n\n<p><strong><em>Let us explore all about Insertion sort in this tutorial.<\/em><\/strong><\/p>\n\n\n\n<p>General Algorithm<\/p>\n\n\n\n<p><strong>Step 1<\/strong>: Repeat Steps 2 to 5 for K = 1 to N-1<\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Step 2<\/strong>: set temp = A[K]<\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Step 3<\/strong>: set J = K \u2013 1<\/p>\n\n\n\n<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Step 4<\/strong>: Repeat while temp &lt;=A[J]<\/p>\n\n\n\n<p>set A[J + 1] = A[J]<\/p>\n\n\n\n<p>set J = J \u2013 1<\/p>\n\n\n\n<p>[end of inner loop]<\/p>\n\n\n\n<p><strong>Step 5<\/strong>: set A[J + 1] = temp<\/p>\n\n\n\n<p>[end of loop]<\/p>\n\n\n\n<p><strong>Step 6<\/strong>: exit<\/p>\n\n\n\n<p>Thus, in the insertion sort technique, we start from the second element as we assume that the first element is always sorted. Then from the second element to the last element, we compare each element to all of its previous elements and the put that element in the proper position.<\/p>\n\n\n\n<p>Pseudocode<\/p>\n\n\n\n<p><strong>The pseudo code for insertion sort is given below.<\/strong><\/p>\n\n\n\n<p>procedure insertionSort(array,N )<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array \u2013 array to be sorted<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N- number of elements<\/p>\n\n\n\n<p>begin<\/p>\n\n\n\n<p><strong>int <\/strong>freePosition<\/p>\n\n\n\n<p><strong>int <\/strong>insert_val<\/p>\n\n\n\n<p><strong>for <\/strong>i = 1 to N -1 <strong>do<\/strong>:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insert_val = array[i]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;freePosition = i<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/locate free position to insert the element<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;whilefreePosition&gt; 0 and array[freePosition -1] &gt;insert_val&nbsp; <strong>do<\/strong>:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array [freePosition] = array [freePosition -1]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;freePosition = freePosition -1<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end <strong>while<\/strong><\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/insert the number at free position<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array [freePosition] = insert_val<\/p>\n\n\n\n<p>end <strong>for<\/strong><\/p>\n\n\n\n<p>end procedure<\/p>\n\n\n\n<p>Given above is the pseudo code for Insertion sort, next, we will illustrate this technique in the following example.<\/p>\n\n\n\n<p>Illustration<\/p>\n\n\n\n<p>The array to be sorted is as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"343\" height=\"70\" src=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-8.png\" alt=\"array to be sorted\" class=\"wp-image-575\" srcset=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-8.png 343w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-8-300x61.png 300w\" sizes=\"(max-width: 343px) 100vw, 343px\" \/><\/figure>\n\n\n\n<p>Now for each pass, we compare the current element to all its previous elements. So in the first pass, we start with the second element.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"700\" height=\"867\" src=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-10.png\" alt=\"Insertion Sort - Illustration\" class=\"wp-image-577\" srcset=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-10.png 700w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-10-242x300.png 242w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<p>Thus we require N number of passes to completely sort an array containing N number of elements.<\/p>\n\n\n\n<p><strong>The above illustration can be summarized in a tabular form:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"877\" height=\"305\" src=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-9.png\" alt=\"\" class=\"wp-image-576\" srcset=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-9.png 877w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-9-300x104.png 300w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-9-768x267.png 768w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-9-400x139.png 400w\" sizes=\"(max-width: 877px) 100vw, 877px\" \/><\/figure>\n\n\n\n<p>As shown in the above illustration, we begin with the 2nd&nbsp;element as we assume that the first element is always sorted. So we begin with comparing the second element with the first one and swap the position if the second element is less than the first.<\/p>\n\n\n\n<p>This comparison and swapping process positions two elements in their proper places. Next, we compare the third element to its previous (first and second) elements and perform the same procedure to place the third element in the proper place.<\/p>\n\n\n\n<p>In this manner, for each pass, we place one element in its place. For the first pass, we place the second element in its place. Thus in general, to place N elements in their proper place, we need N-1 passes.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2>Shell Sort<\/h2>\n\n\n\n<p>Shell sort is often termed as an improvement over insertion sort. In insertion sort, we take increments by 1 to compare elements and put them in their proper position.<\/p>\n\n\n\n<p>In shell sort, the list is sorted by breaking it down into a number of smaller sublists. It\u2019s not necessary that the lists need to be with contiguous elements. Instead, shell sort technique uses increment i, which is also called \u201cgap\u201d and uses it to create a list of elements that are \u201ci\u201d elements apart.<\/p>\n\n\n\n<p><strong>The general algorithm for shell sort is given below.<\/strong><\/p>\n\n\n\n<p>shell_sort (A, N)<\/p>\n\n\n\n<p>where A \u2013 list to be sorted; N \u2013 gap_size<\/p>\n\n\n\n<p>set gap_size = N, flag = 1<\/p>\n\n\n\n<p>while gap_size &gt; 1 or flag = 1, repeat<\/p>\n\n\n\n<p>begin<\/p>\n\n\n\n<p>set flag = 0<\/p>\n\n\n\n<p>set gap_size = (gap_size + 1)\/2<\/p>\n\n\n\n<p>end<\/p>\n\n\n\n<p>for i = 0 to i&lt; (N-gap_size) repeat<\/p>\n\n\n\n<p>begin<\/p>\n\n\n\n<p>if A[i + gap_size] &gt; A[i]<\/p>\n\n\n\n<p>swap A[i + gap_size], A[i]<\/p>\n\n\n\n<p>set flag = 0<\/p>\n\n\n\n<p>end<\/p>\n\n\n\n<p>end<\/p>\n\n\n\n<p>Thus in the above algorithm, we first set N which is the gap for sorting the array A using shell sort. In the next step, we divide the array into sub-arrays by using the gap. Then in the next step, we sort each of the sub-arrays so that at the end of the loop we will get a sorted array.<\/p>\n\n\n\n<p>Next, let us consider a detailed example to better understand the shell sort using a pictorial representation.<\/p>\n\n\n\n<p>Illustration<\/p>\n\n\n\n<p><strong>Let us illustrate the Shell sort with an Example.<\/strong><\/p>\n\n\n\n<p>Consider the following array of 10 elements.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"460\" height=\"71\" src=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-14.png\" alt=\"array of 10 elements\" class=\"wp-image-582\" srcset=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-14.png 460w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-14-300x46.png 300w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-14-400x62.png 400w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/figure>\n\n\n\n<p>If we provide a gap of 3, then we will have the following sub-lists with each element that is 3 elements apart. We then sort these three sublists.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"599\" height=\"224\" src=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-12.png\" alt=\"sub-lists\" class=\"wp-image-580\" srcset=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-12.png 599w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-12-300x112.png 300w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-12-400x150.png 400w\" sizes=\"(max-width: 599px) 100vw, 599px\" \/><\/figure>\n\n\n\n<p>The sorted sub-lists and the resultant list that we obtain after combining the three sorted sublists are shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"463\" height=\"307\" src=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-11.png\" alt=\"sorted sub-lists and the resultant list\" class=\"wp-image-579\" srcset=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-11.png 463w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-11-300x199.png 300w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-11-400x265.png 400w\" sizes=\"(max-width: 463px) 100vw, 463px\" \/><\/figure>\n\n\n\n<p>The above array that we have obtained after merging the sorted subarrays is nearly sorted. Now we can perform insertion sort on this list and sort the entire array. This final step is shown below for your reference.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"635\" height=\"300\" src=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-13.png\" alt=\"The final step\" class=\"wp-image-581\" srcset=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-13.png 635w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-13-300x142.png 300w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-13-400x189.png 400w\" sizes=\"(max-width: 635px) 100vw, 635px\" \/><\/figure>\n\n\n\n<p>As seen above, after performing shell sort and merging the sorted sublists, we only required three moves to completely sort the list. Thus we can see that we can significantly reduce the number of steps required to sort the array.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2>SHELL SORT<\/h2>\n\n\n\n<p>Shell sort is often termed as an improvement over insertion sort. In insertion sort, we take increments by 1 to compare elements and put them in their proper position.<\/p>\n\n\n\n<p>In shell sort, the list is sorted by breaking it down into a number of smaller sublists. It\u2019s not necessary that the lists need to be with contiguous elements. Instead, shell sort technique uses increment i, which is also called \u201cgap\u201d and uses it to create a list of elements that are \u201ci\u201d elements apart.<\/p>\n\n\n\n<p><strong>The general algorithm for shell sort is given below.<\/strong><\/p>\n\n\n\n<p>shell_sort (A, N)<\/p>\n\n\n\n<p>where A \u2013 list to be sorted; N \u2013 gap_size<\/p>\n\n\n\n<p>set gap_size = N, flag = 1<\/p>\n\n\n\n<p>while gap_size &gt; 1 or flag = 1, repeat<\/p>\n\n\n\n<p>begin<\/p>\n\n\n\n<p>set flag = 0<\/p>\n\n\n\n<p>set gap_size = (gap_size + 1)\/2<\/p>\n\n\n\n<p>end<\/p>\n\n\n\n<p>for i = 0 to i&lt; (N-gap_size) repeat<\/p>\n\n\n\n<p>begin<\/p>\n\n\n\n<p>if A[i + gap_size] &gt; A[i]<\/p>\n\n\n\n<p>swap A[i + gap_size], A[i]<\/p>\n\n\n\n<p>set flag = 0<\/p>\n\n\n\n<p>end<\/p>\n\n\n\n<p>End<\/p>\n\n\n\n<p>Thus in the above algorithm, we first set N which is the gap for sorting the array A using shell sort. In the next step, we divide the array into sub-arrays by using the gap. Then in the next step, we sort each of the sub-arrays so that at the end of the loop we will get a sorted array.<\/p>\n\n\n\n<p>Next, let us consider a detailed example to better understand the shell sort using a pictorial representation.<\/p>\n\n\n\n<p>Illustration<\/p>\n\n\n\n<p><strong>Let us illustrate the Shell sort with an Example.<\/strong><\/p>\n\n\n\n<p>Consider the following array of 10 elements.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"460\" height=\"71\" src=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-15.png\" alt=\"array of 10 elements\" class=\"wp-image-585\" srcset=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-15.png 460w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-15-300x46.png 300w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-15-400x62.png 400w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/figure>\n\n\n\n<p>If we provide a gap of 3, then we will have the following sub-lists with each element that is 3 elements apart. We then sort these three sublists.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"599\" height=\"224\" src=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-18.png\" alt=\"sub-lists\" class=\"wp-image-588\" srcset=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-18.png 599w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-18-300x112.png 300w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-18-400x150.png 400w\" sizes=\"(max-width: 599px) 100vw, 599px\" \/><\/figure>\n\n\n\n<p>The sorted sub-lists and the resultant list that we obtain after combining the three sorted sublists are shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"463\" height=\"307\" src=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-17.png\" alt=\"sorted sub-lists and the resultant list\" class=\"wp-image-587\" srcset=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-17.png 463w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-17-300x199.png 300w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-17-400x265.png 400w\" sizes=\"(max-width: 463px) 100vw, 463px\" \/><\/figure>\n\n\n\n<p>The above array that we have obtained after merging the sorted subarrays is nearly sorted. Now we can perform insertion sort on this list and sort the entire array. This final step is shown below for your reference.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"635\" height=\"300\" src=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-16.png\" alt=\"The final step\" class=\"wp-image-586\" srcset=\"https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-16.png 635w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-16-300x142.png 300w, https:\/\/xcodeph.net\/wp-content\/uploads\/2021\/07\/image-16-400x189.png 400w\" sizes=\"(max-width: 635px) 100vw, 635px\" \/><\/figure>\n\n\n\n<p>As seen above, after performing shell sort and merging the sorted sublists, we only required three moves to completely sort the list. Thus we can see that we can significantly reduce the number of steps required to sort the array.<\/p>\n\n\n\n<p>The choice of increment to create sub-lists is a unique feature of shell sort.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bubble Sort Bubble Sort is the simplest of the sorting techniques. In the bubble sort technique, each of the elements in the list is compared to its adjacent element. Thus if there are n elements in list A, then A[0] is compared to A[1], A[1] is compared to A[2] and so on. After comparing if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/xcodeph.net\/index.php\/wp-json\/wp\/v2\/pages\/564"}],"collection":[{"href":"https:\/\/xcodeph.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/xcodeph.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/xcodeph.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xcodeph.net\/index.php\/wp-json\/wp\/v2\/comments?post=564"}],"version-history":[{"count":4,"href":"https:\/\/xcodeph.net\/index.php\/wp-json\/wp\/v2\/pages\/564\/revisions"}],"predecessor-version":[{"id":589,"href":"https:\/\/xcodeph.net\/index.php\/wp-json\/wp\/v2\/pages\/564\/revisions\/589"}],"wp:attachment":[{"href":"https:\/\/xcodeph.net\/index.php\/wp-json\/wp\/v2\/media?parent=564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}