Page 574 - Computer science 868 Class 12
P. 574

4
              1
              0
              99
              The Unsorted Array:
              2 | 4 | 1 | 0 | 99 |
              The Sorted Array:
              0 | 1 | 2 | 4 | 99 |



                Program 3      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  the details  of the
                               constructor and its member methods.
                               Data Members
                               int ar[]                     :  Integer array to store numbers
                               int l                        :  to store array length
                               Member Functions
                               Insertion_Sort()             :  A constructor to initialise data members to 0
                               void input()                 :  to enter the array from the user and store it in ar[]
                               void sort()                  :  to sort ar[] in ascending order
                               void display()               :  to display the 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       void accept()

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

                13       n=sc.nextInt();
                14       ar=new int[n];

                15       System.out.println("ENTER ARRAY ELEMENTS");
                16       for(int i=0;i<n;i++)

                17       {
                18       ar[i]=sc.nextInt();



                572572  Touchpad Computer Science-XII
   569   570   571   572   573   574   575   576   577   578   579