Page 292 - Computer science 868 Class 12
P. 292

20                       j = j - 1;
                21                   }

                22                   arr[j + 1] = key;
                23               }
                24               System.out.println("After Sorting : ");

                25               for (i = 0; i < len; ++i)

                26                   System.out.print(arr[i] + " ");
                27               System.out.println();
                28           }

                29
                30           public static void main(String args[])

                31           {
                32            InsertionSort ob = new InsertionSort();

                33            ob.sort();
                34           }

                35       }

              The output of the preceding program is as follows:
              Before Sorting:

              1.2 4.6 7.8 4.5 8.9
              After Sorting:

              1.2 4.5 4.6 7.8 8.9

              Sorting and Searching

                                        Sorting                                       Searching
                    1.   Arranging  the array elements  in  ascending  or  1. Finding an element in an array is called searching.
                       descending order is called sorting.
                    2.  Two types of sorting are: Bubble sort and Selection  2.  Two types of searching are: Linear search and Binary
                      sort.                                          search.

              9.3.3 Insertion
              Sometimes, it is required to insert an element in an array in the desired position. To do this, the following steps are
              performed.
              1.  The number of elements should be at least one less than the size of the array.
              2.  The position to be inserted should be within the range of the array.
              3.  The elements of the array are to be shifted one column by right starting from the position to be inserted.
              4.  Store the element in the position.

                  import java.util.*;





                290290  Touchpad Computer Science-XII
   287   288   289   290   291   292   293   294   295   296   297