Page 301 - Cs_withBlue_J_C11_Flipbook
P. 301

The whole array has been sorted in descending order.


                   Program 10    Write a program in Java to input “n” names and marks of “n” students in an array and sort
                                 the numbers in an ascending order. (Use Bubble sort technique)
                   1       import java.util.*;

                   2       class Bubble_sort
                   3       {

                   4           public static void main(int nos)
                   5           {

                   6               Scanner sc= new Scanner(System.in);
                   7               String n[ ]=new String[nos];

                   8               int m[ ]=new int[nos];
                   9               int i, j, temp_marks;

                   10              String temp_name;
                   11              for (i=0; i<nos; i++)

                   12              {
                   13                  System.out.print("Enter a name: ");

                   14                  n[i] =sc.next();
                   15                  System.out.print("Enter marks: ");

                   16                  m[i] =sc.nextInt();
                   17              }

                   18              for(i=0; i<nos-1; i++)
                   19              {

                   20                  for(j=0; j<nos-1-i; j++)
                   21                  {
                   22                      if(m[j]>m[j+1])

                   23                      {

                   24                          temp_marks = m[j];
                   25                          m[j]=m[j+1];
                   26                          m[j+1]=temp_marks;

                   27
                   28                          temp_name = n[j];

                   29                          n[j]=n[j+1];
                   30                          n[j+1]=temp_name;

                   31                      }




                                                                                                                       299
                                                                                                              Arrays   299
   296   297   298   299   300   301   302   303   304   305   306