Page 395 - CA_Blue( J )_Class10
P. 395

15         {    if(ar[j]>ar[j+1])
                  16            {    temp = ar[j];

                  17               ar[j]=ar[j+1];
                  18               ar[j+1]=temp;

                  19            }
                  20         }

                  21      }
                  22          for(i=0;i<10;i++)

                  23      {    System.out.print(ar[i]+ " ");
                  24      }

                  25        }
                  26  }

                 You will get the following output:
                 Enter a number : 9
                 Enter a number : 7
                 Enter a number : 5
                 Enter a number : 3
                 Enter a number : 1
                 Enter a number : 2
                 Enter a number : 4
                 Enter a number : 6
                 Enter a number : 8
                 Enter a number : 10
                 1 2 3 4 5 6 7 8 9 10

                                 To input 10 numbers in an array and sort the numbers in ascending order. (Use Selection sorting
                  Program 14
                                 technique)

                   1  import java.util.*;

                   2  class Selection_sort
                   3  {

                   4      public static void main()
                   5      {

                   6          Scanner sc= new Scanner(System.in);
                   7          int ar[]=new int[10];

                   8          int i,j,temp, min, minpos=-1;
                   9          for (i=0; i<10; i++)

                  10          {   System.out.print("Enter a number : ");



                                                                                                                       393
                                                                                                              Arrays   393
   390   391   392   393   394   395   396   397   398   399   400