Skip to main content

Example of Selection sort for Data Structure in 'C'

Selection sort:

            In this technique of sorting, the fact of selecting the minimum element from n (size of the array) number of elements is used. The selected minimum element is interchanged with the first element of the array. Then leaving the first element, the minimum element is again picked from the remaining n-1 elements and interchanged with the second element. The same process of selecting minimum element and interchanging it from the respective element is repeated to obtain the sorted array. In total n-1 passes are applied to obtain the sorted array. As we select the minimum element in every pass this technique of sorting the elements is called as Selection sort. Let us understand this technique with the help of an analytical example before going into the algorithm and C function. 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 order. So to arrange the elements in ascending order we can apply the selection sort. 

In the first pass, we select the minimum element from the 10 elements as 10 with index 6. Interchange the element 10 with the element 91 (first element of the array). After this pass the array looks as follows:
                 10 18 22 43 34 91 88 11 33 77
In the second pass, we select the minimum element from 9 elements leaving the first element (as it is already sorted) as 11 with index 8. Interchange the element 11 with 18 (second element of the array). After this pass the array looks as follows:
                  10 11 22 43 34 91 88 18 33 77
In the third pass, we select the minimum element from 8 elements leaving the first two elements (as they are already sorted) as 18 with index 8. Interchange the element 18 with 22 (third element of the array). After this pass the array looks as follows:
                   10 11 18 43 34 91 88 22 33 77
In the fourth pass, we select the minimum element from 7 elements leaving the first three elements (as they are already sorted) as 22 with index 8. Interchange the element 22 with 43 (fourth element of the array). After this pass the array looks as follows:
                     10 11 18 22 34 91 88 43 33 77
 In the fifth pass, we select the minimum element from 6 elements leaving the first four elements (as they are already sorted) as 33 with index 9. Interchange the element 33 with 34 (fifth element of the array). After this pass the array looks as follows:
                      10 11 18 22 33 91 88 43 34 77
In the six pass, we select the minimum element from 5 elements leaving the first five elements (as they are already sorted) as 34 with index 9. Interchange the element 34 with 91 (sixth element of the array). After this pass the array looks as follows:
                       10 11 18 22 33 34 88 43 91 77
In the seventh pass, we select the minimum element from 4 elements leaving the first six elements (as they are already sorted) as 43 with index 8. Interchange the element 43 with 88 (seventh element of the array). After this pass the array looks as follows:
                       10 11 18 22 33 34 43 88 91 77
In the eighth pass, we select the minimum element from 3 elements leaving the first seven elements (as they are already sorted) as 77 with index 10. Interchange the element 77 with 88 (eighth element of the array). After this pass the array looks as follows:
                        10 11 18 22 33 34 43 77 91 88
In the ninth pass, we select the minimum element from 2 elements leaving the first three elements (as they are already sorted) as 88 with index 10. Interchange the element 77 with 91 (ninth element of the array). After this pass the array looks as follows:
                         10 11 18 22 33 34 43 77 88 91
The ninth pass is the last pass. We can observe from the last pass that after that pass we remain with the largest element of the array and is placed at its proper place. That is why we require only n-1 in this case 9 passes to sort the array.

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 …