Answer to Question #259713 in Java | JSP | JSF for masedi moncho

Question #259713

Create a program for setup for water sort puzzle

1
Expert's answer
2021-11-01T18:50:28-0400
import java.util.Scanner;
public class Main
{
    static void sort(int arr[],int n){
    int temp;
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            if(arr[j] < arr[i])
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}
	public static void main(String[] args) {
    	int n;
    	Scanner in=new Scanner(System.in);
    	System.out.println("Enter number of elements in the array:");
        n=in.nextInt();
        System.out.println("Enter elements of the array:");
        int [] arr=new int [n];
        for(int i=0;i<n;i++){
            arr[i]=in.nextInt();
        }
        
        System.out.println("\nElements of the array before sorting:");
        for(int i=0;i<n;i++){
            System.out.print(arr[i]+"\t");
        }
        sort(arr,n);
        System.out.println("\nElements of the array after sorting: ");
        for(int i=0;i<n;i++){
            System.out.print(arr[i]+"\t");
        }
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS