Advertisements
Advertisements
Question
Write a program to accept a list of 20 integers. Sort the first 10 numbers in ascending order and next the 10 numbers in descending order by using Bubble Sort technique. Finally, print the complete list of integers.
Hint: Class average is the average marks obtained by 40 students in a particular subject.
Answer in Brief
Solution
import java.util.*;
public class AQ7
{public static void main(String g[]){
int i, j, tmp;
int a[] = new int[20];
Scanner sc = new Scanner(System.in);
// INPUT
System.out.printIn("Enter the elements");
for (i = 0; i < a.length; i++)
{a[i] = sc.nextInt();
if (i == 9) System.out.printIn("Enter the next 10 elts");
}
// SORT FIRST TEN VALUES IN ASCENDING ORDER (bubble sort)
for (i = 0; i <= 9; i++)
{
for (j = 0; j < 9; j++)
{if (a[j] > a[j + 1])
{tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}}}
//SORT LAST TEN VALUES IN DESCENDING ORDER (bubble sort)
for (i = 10; i <= 19; i++)
{
for (j = 10; j < 19; j++)
{if (a[j] < a[j + 1])
{tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}}}
System.out.println("The first ten values in Ascending Order");
for (i = 0; i <= 19; i++)
{System.out.println(a[i]);
if (i==9) System.out.println("\n The Last Ten values In Descending Order:");
}
}}
shaalaa.com
Is there an error in this question or solution?