Page 572 - Computer science 868 Class 12
P. 572

Program 2      Design a class “Selection_Sort” that inputs an array from the User and sorts it in ascending
                               order using Selection Sort technique. A class is declared to give details of the constructor and
                               member methods.
                               Data Members
                               int ar[]                     :  Integer array to store numbers
                               int  n                       :  To store the length of the array
                               Member Functions
                               Selection_Sort()             :  A constructor to initialise n=0 and declare array size
                               void readlist()              :  To accept value of n and array elements from the user
                               int index_of_min(int startindex)  :  Return the index position of the lowest value

                               void sort()                  :   To sort the array in ascending order using Selection Sort
                                                               technique by calling method index_of_min(int startindex)
                               void display()               :  To display the array

                 1       import java.util.*;

                 2       class Selection_sort
                 3       {

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

                 6       Selection_sort()
                 7       {

                 8       n=0;
                 9       }

                10       void read_list()   // to accept data
                11       {

                12       System.out.println("ENTER SIZE OF THE 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       ar[i]=sc.nextInt();

                18       }
                19       int index_of_min(int startindex)    //finding index position of smallest element

                20       {
                21       int p=startindex;
                22       for(int j=startindex+1;j<n;j++)

                23       {




                570570  Touchpad Computer Science-XII
   567   568   569   570   571   572   573   574   575   576   577