package com.pack.array;
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
int size=0,i;
System.out.println("Enter the size of Array");
Scanner sc=new Scanner(System.in);
size=sc.nextInt();
int arr[]=new int[size];
System.out.println("Enter "+size+" elements");
for(i=0;i<size;i++)
{
arr[i]=sc.nextInt();
}
//
System.out.println("Originall array");
for(i=0;i<size;i++)
{
System.out.print(arr[i]+"\t");
}
//sorting logic
for(i=0;i<size;i++)
{
for(int j=0;j<size-i-1;j++)
{
if(arr[j]>arr[j+1])
{
int t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
}
//printing sorted array
System.out.println("Sorted elements");
for(i=0;i<size;i++)
{
System.out.print(arr[i]+"\t");
}
}
}
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
int size=0,i;
System.out.println("Enter the size of Array");
Scanner sc=new Scanner(System.in);
size=sc.nextInt();
int arr[]=new int[size];
System.out.println("Enter "+size+" elements");
for(i=0;i<size;i++)
{
arr[i]=sc.nextInt();
}
//
System.out.println("Originall array");
for(i=0;i<size;i++)
{
System.out.print(arr[i]+"\t");
}
//sorting logic
for(i=0;i<size;i++)
{
for(int j=0;j<size-i-1;j++)
{
if(arr[j]>arr[j+1])
{
int t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
}
//printing sorted array
System.out.println("Sorted elements");
for(i=0;i<size;i++)
{
System.out.print(arr[i]+"\t");
}
}
}
0 comments:
Post a Comment