Java Program
Java Program
Arrays;
class Sort
{
public static void main(String args[])
{
int data[] = { 4,-5,2,6,1 };
//i declared integer data holds the value 4,-5,2,6,1
Arrays.sort(data);
//since i use array we use arrays.sort, it will sort the value of data that I declared
for (int c: data)
//then after sorting the data to descending to ascending order, letter C hold the value of the data.
{
System.out.println(c);
//print the c to see the output
}
}
}
OUTPUT: it will print descending to ascending order
2.import java.util.Scanner;
public class bubsort1
{
public static void main(String[]args)
{
Scanner name = new Scanner(System.in);
String name1, name2, name3;
System.out.println("You will be asked to enter 3 name.");
//system asked you to input 3 names
System.out.println("Enter your first name:");
name1 = name.nextLine();
//first input(example:bike)
System.out.println("\nEnter your second name:");
name2 = name.nextLine();
//second input (example:ant)
System.out.println("\nEnter your third name:");
name3 = name.nextLine();
//third input (example:candy)
System.out.println("\nThe name will now be displayed in alphabetical order");
//first
if(name1.compareToIgnoreCase(name2)<0)
// first condition is to compare input 1 to input 2 (bike and ant) if false proceed in second
condition
{
if(name1.compareToIgnoreCase(name3)<0)
//second condition is to compare input1 to input 3(bike and candy)if true print
input1(bike) but if false proceed in third condition
{
System.out.println(name1);
}
if(name2.compareToIgnoreCase(name3)<0)
//third condition is to compare input 2( to input 3(ant and candy)
{
System.out.println(name2);
// if true the second input(ant) is more than to third input(candy) it will print
second input,if false proceed to second conditions
//(this condition is true because the hierarchy of ant is more than to candy.so it
will print ant)
System.out.println(name3);
}
else
{
System.out.println(name3);
//if true to the third input(candy) is more than to second input(ant) it will print
third input
//(and this condition is false because the hierarchy of candy is less than to ant)
System.out.println(name2);
}
}
//second
else if(name2.compareToIgnoreCase(name1)<0)
//first condition is to compare input 2 to input 1 (ant and bike) if false proceed in second
condition
{
if(name2.compareToIgnoreCase(name3)<0)
//second condition is to compare input 2 to input 3(ant and candy)if true print
input2(ant) but if false proceed in third condition
{
System.out.println(name2);
}
if(name1.compareToIgnoreCase (name3)<0)
//third condition is to compare input 1 to input 3(bike and candy)
{
System.out.println(name1);
//if true to the input 1(bike) is more than to third input(candy) it will print first
input.if false proceed to second conditions
//(this condition is true because the hierarchy of (bike) is more than to candy.so it
will print bike.
System.out.println(name3);
}
else
{
System.out.println(name3);
//if true to the input 3(candy) is more than to first input(bike) it will print first
input
//(and this condition is false because the hierarchy of candy is less than to the
bike)
System.out.println(name1);
}
}
//third
else if(name3.compareToIgnoreCase(name1)<0)
//first condition is to compare input 3 to input 1 (candy and bike) if false proceed in
second condition
{
if(name3.compareToIgnoreCase(name2)<0)
//second condition is to compare input 3 to input 2(candy and ant)if true print
input3(candy) but if false proceed in third condition
{
System.out.println(name3);
}
if(name1.compareToIgnoreCase(name2)<0)
// third condition is to compare input 1 to input 2(bike and ant)
{
System.out.println(name1);
//so this is the last decision so we wil find the lower hierarchy
//if true to the input 1(bike) is less than to second input(ant) it will print first
input.if false proceed to second conditions
//and this condition is false because the hierarchy of bike is more than to the ant)
System.out.println(name2);
}
else
{
System.out.println(name2);
//if true to the input 2(ant) is less than to first input(bike) it will print first input
//(and this condition is true because the hierarchy of ant is less than to the bike)
System.out.println(name1);
}
}
}
OUTPUT: i enter first name is bike,second is ant and third is candy then it will sort it
alphabetically order