In: Computer Science
. Implement a method that meets the following requirements:
Computer Language:Java
(a) Try to write this method with as few lines of code as you
can
(b) Sorts a group of three integers, x,y and z, into increasing
order (they do not have to be in a sequence).
(c) Assume the value in x is less than the value in z. You can also
assume there are no duplicates among x, y and z (none of them
contain the same value)
(d) Prints a message each time the order of two elements are
changed.
(e) Prints the list before and after sorting
//Java code
public class Main { public static void main(String[] args) { // list of three integers int[]list ={45,67,23}; System.out.print("Before sort: \n"); display(list); sort(list); System.out.print("After Sort: \n"); display(list); } public static void sort(int[] list) { for (int i = 0; i <list.length ; i++) { for (int j = i+1; j <list.length ; j++) { if(list[i]>list[j]) { System.out.print("Swapping value\n"); int temp = list[i]; list[i] = list[j]; list[j] = temp; } } } } public static void display(int[] list) { for (int i = 0; i <list.length ; i++) { System.out.print(list[i]+" "); } System.out.print("\n"); } }
//Output
//If you need any help regarding this solution .....................please leave a comment ........ thanks