Page 340 - ComputerScience_Class_11
P. 340
Swapping the numbers in the position, the array becomes:
Index 0 1 2 3 4
Ar 21 16 12 10 2
The whole array has been sorted in descending order.
Program 10 Write a Java program 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];
338 Touchpad Computer Science (Ver. 3.0)-XI

