Skip to main content

Insertion sort and let us consider an array of size 10 for Data Structure in 'C'

Insertion sort: 

                 In this technique of sorting, the fact of inserting the pivotal element  at its proper position from n (size of the array) number of elements is used. The pivotal element to be inserted is picked from the array itself. The selected pivotal element is inserted at the position from the left part of the array that is treated as sorted.
                   Initially the first element of the array is treated as left part of the array and being one element that is treated as sorted part of the array. The first pivotal element that is selected for insertion is the second element of the array. the pivotal element is compared with the first element and if the first element is greater than the pivotal element, then it is moved to the second position. Now we remain with no elements compared to be compared in the left part, the position where the pivotal element is found as the first position. The pivotal element is inserted at the first position to over the pass. If the first element is not greater than the pivotal element then the position of the pivotal element is right and remains at the same place. After the first pass the left part of the array containing two elements is in sorted order.
                 In the further passes the element of the unsorted part of the array is selected as pivotal element and a position to insert it in the sorted part (left array) of the array is found by shifting the sorted elements to the right if required. So after every pass the pivotal element is inserted at its proper position in the array. As the insertion operation is being performed to insert the pivotal element, this technique of sorting the array is called as ‘’insertion sort’’.

 Let us consider an array of size 10 with the following elements:

                              91 18 22 43 34 10 88 11 33 77

We can observe the elements and see that the elements are not in any order. So to arrange the elements in ascending order we can apply the insertion sort.
In the first pass, we select the element 18 is pivotal element. The left part of the of the array before the pivotal element contains only one element and it is the sorted part of the array. 18 is compared with 91,91 is greater than 18. So, 91 is shifted to the right by one position. Now there are no elements to be compared in the sorted part of the array with the pivotal element. Now the pivotal element is inserted at first position of the array. After this pass the array looks as follows: [sorted elements-BOLD, pivotal element – underlined].
                                    18 91 22 43 34 10 88 11 33 77
In the second pass, 22 is selected as pivotal element. 22 is compared with 91, as 91 is greater then 22, 91 is shifted towards its right by one position. Again 22 is compared with 18,18 is not greater then the pivotal element 22. So, the comparison is stopped because the position where the pivotal element 22 is to be inserted is found. The pivotal element 22 is inserted at second position. After this pass the array looks as follows:
                                     18 22 91 43 34 10 88 11 33 77
In the third pass, 43 is selected as pivotal element from the unsorted part of the array. 43 is compared with 91 and  91 is shifted towards its right by one position. 43 is again compared with 22. 22 is not greater then 43 and comparison is stops. 43 is inserted at third element of array.  After this pass the array looks as follows:
                                     18 22 43 91 34 10 88 11 33 77
In the fourth pass, 34 is selected as pivotal element from the unsorted part of the array. 34 is compared with 91 and 91 is shifted towards its right by one position. 34 is again compared with 43. 43 is also right shifted by one position. 34 is now comparison with 22. 22 is not greater then 34 and comparison stops. 34 is inserted at third element of array.  After this pass the array looks as follows:
                                     18 22 34 43 91 10 88 11 33 77
In the fifth pass, 10 is selected as pivotal element from the unsorted part of the array. As 10 smaller then all the sorted elements of the left part of the array, each element is shifted to right by one position respectively. 10 is inserted as the first element of array.  After this pass the array looks as follows:
                                     10 18 22 34 43 91 88 11 33 77
In the sixth pass, 88 is selected as pivotal element from the unsorted part of the array. 88 is compared with 91 and  91 is shifted towards its right by one position because it is greater then 88. 88 is now compared with 43. and comparison is stops. 88 is inserted at sixth element of array.  After this pass the array looks as follows:
                                     10 18 22 34 43 88 91 11 33 77
In the seventh pass, 11 is selected as pivotal element from the unsorted part of the array. 11 is compared with all the element up to 18 and they are  shifted to right by one position respectively. The comparison is stops when 11 is compared with 10. So, 11 is inserted at second element of the array.  After this pass the array looks as follows:
                                     10 11 18 22 34 43 88 91 33 77
In the eighth pass, 33 is selected as pivotal element from the unsorted part of the array. 33 is compared with all the element up to 34 and they are  shifted to right by one position respectively. The comparison is stops when 33 is compared with 22. So, 33 is inserted at fifth element of the array.  After this pass the array looks as follows:
                                     10 11 18 22 33 34 43 88 91 77
In the ninth and final pass, 77 is selected as pivotal element from the unsorted part of the array. 77 is compared with 91 and 88 and they are shifted to right by one position respectively. The comparison is stops when 77 is compared with 43. So, 77 is inserted at eighth element of the array.  After this pass the array looks as follows:
                                     10 11 18 22 33 34 43 77 88 91 

Comments

Popular posts from this blog

difference between structure and union in C Language

In c language article we will see the difference between union and structure. Both are the user define datatype in c language. See the table which is mentioned below: ASP.NET Video Tutorial Series Structure Union1.The keywordstruct is used to define a structure 1. The keyword union is used to define a union. 2. When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum ofsizes of its members. The smaller members may end with unused slack bytes. 2. When a variable is associated with a union, thecompiler allocates thememory by considering the size of the largest memory. So, size of union is equal to the size of largest member. 3. Each member within a structure is assigned unique storage area of location. 3. Memory allocated is shared by individual members of union. 4. The address of each member will be in ascending order This indicates that memory for each member will start at different offset v…

Difference between Linear search and Binary Search in c language

SQL Video Channel : Download all SQL Video



Binary Search Linear Search Works only on sorted items. such as  1,2,3,4,5,6  etc
Works on sorted as well as unsorted items. 12,4,5,3,2,1 etc Very efficient if the items are sorted Very efficient if the items are less and present in the beginning of the list. such as Suppose your list items are : 12,3,4,5,1 and you want to search 12 number then you get beginning in the list. Works well with arrays and not on linked lists. Works with arrays and linked lists.
Number of comparisons are less More number of comparisons are required if the items are present in the later part of the array or its elements are more.

Memory representation of Linked List Data Structures in C Language

Memory representation of Linked List

             In memory the linked list is stored in scattered cells (locations).The memory for each node is allocated dynamically means as and when required. So the Linked List can increase as per the user wish and the size is not fixed, it can vary.

               Suppose first node of linked list is allocated with an address 1008. Its graphical representation looks like the figure shown below:


      Suppose next node is allocated at an address 506, so the list becomes,



  Suppose next node is allocated with an address with an address 10,s the list become,


The other way to represent the linked list is as shown below:




 In the above representation the data stored in the linked list is “INDIA”, the information part of each node contains one character. The external pointer root points to first node’s address 1005. The link part of the node containing information I contains 1007, the address of next node. The last node …