You are on page 1of 3

Write a program to sort/arrange an array of 10 integers

in ascending order using bubble sort technique

import java.util.*;
class bubble_sort_no
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int i,j,t;
int m[]=new int[10];
System.out.println("Enter 10 numbers");
for(i=0;i<10;i++)
{
m[i]=sc.nextInt();
}

for(i=0;i<9;i++)
{
for(j=0;j<9-i;j++)
{
if(m[j]>(m[j+1]))
{
t=m[j];
m[j]=m[j+1];
m[j+1]=t;
}
}
}
System.out.println("The numbers in ascending order");
for(i=0;i<10;i++)
System.out.println(m[i]);
}
}
Write a program to input and store roll number and total marks of 20
students. Using Bubble Sort technique generate a merit list. Print the merit list
in two columns containing roll number and total marks in the below format:
Roll No. Total Marks
... ...
... ...
... ...
Answer
*/
import java.util.*;
class MeritList
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n = 20;
int rollNum[] = new int[n];
int marks[] = new int[n];
for (int i = 0; i < n; i++)
{
System.out.print("Enter Roll Number: ");
rollNum[i] = sc.nextInt();
System.out.print("Enter marks: ");
marks[i] = sc.nextInt();
}
int t,p;
//Bubble Sort
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (marks[j] < marks[j + 1])
{
t = marks[j];
marks[j] = marks[j+1];
marks[j+1] = t;

p = rollNum[j];
rollNum[j] = rollNum[j+1];
rollNum[j+1] = p;
}
}
}

System.out.println("Roll No."+"\t\t"+"Total Marks");


for (int i = 0; i < n; i++)
{
System.out.println(rollNum[i] + "\t\t" + marks[i]);
}
}
}

You might also like