Page 515 - Cs_withBlue_J_C11_Flipbook
P. 515

Program 11    Design a class Insertion_Sort that inputs an array from the user and sorts it in ascending
                                 order using Insertion Sort technique. A main class is created to give details of the constructor
                                 and the member methods.
                                 Data Members
                                 int ar[]                        :  Integer array to store numbers
                                 Member Methods
                                 Insertion_Sort()                :  A constructor to initialise data members to 0
                                 void input()                    :  To input the array from the user and store it in ar[]
                                 void sort()                     :  To sort ar[] in ascending order
                                 void display()                  :  To display the sorted array


                   1       import java.util.*;
                   2       class Insertion_sort

                   3       {
                   4           int ar[],n;
                   5           Scanner sc=new Scanner(System.in);

                   6           Insertion_sort()

                   7           {
                   8               n=0;
                   9           }

                   10
                   11          void accept()    //initializing variables

                   12          {
                   13              System.out.println("ENTER THE SIZE OF ARRAY");

                   14              n=sc.nextInt(); ar=new int[n];
                   15              System.out.println("ENTER THE ARRAY ELEMENTS");

                   16              for(int i=0;i<n;i++)
                   17              {

                   18                  ar[i]=sc.nextInt();
                   19              }

                   20          }
                   21

                   22          void display1()           //display the original array
                   23          {

                   24              System.out.println("ORIGINAL ARRAY IS :");
                   25              for(int i=0;i<n;i++)

                   26              {




                                                                                                                       513
                                                                                                   Internal Assessment  513
   510   511   512   513   514   515   516   517   518   519   520