Create a program for setup for water sort puzzle
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");
}
}
}
Comments
Leave a comment