this is the program to sort the given numbers in a accenting or decanting order
import java.util.*;
public class sorting
{
int table[]=new int[5];
public static void main(String arg[])
{
sorting m=new sorting();
m.readArray();
m.bubbleSort();
m.print();
}
void bubbleSort()
{
for(int i=1;i<=4;i++)
{
for(int j=0;j<=(4-i);j++)
{
if(table[j]>table[j+1])
swap(j,j+1);
}
}
}
void print()
{
System.out.print("sorted list of your numbers:");
for(int i=0;i<=4;i++)
{
System.out.print(" "+table[i]);
}
}
void readArray()
{
for(int i=0;i<=4;i++)
{
Scanner n=new Scanner(System.in);
System.out.println("enter your number "+(i+1));
table[i]=n.nextInt();
}
}
void swap(int x,int y)
{
int m=table[x];
table[x]=table[y];
table[y]= m;
}
}
import java.util.*;
public class sorting
{
int table[]=new int[5];
public static void main(String arg[])
{
sorting m=new sorting();
m.readArray();
m.bubbleSort();
m.print();
}
void bubbleSort()
{
for(int i=1;i<=4;i++)
{
for(int j=0;j<=(4-i);j++)
{
if(table[j]>table[j+1])
swap(j,j+1);
}
}
}
void print()
{
System.out.print("sorted list of your numbers:");
for(int i=0;i<=4;i++)
{
System.out.print(" "+table[i]);
}
}
void readArray()
{
for(int i=0;i<=4;i++)
{
Scanner n=new Scanner(System.in);
System.out.println("enter your number "+(i+1));
table[i]=n.nextInt();
}
}
void swap(int x,int y)
{
int m=table[x];
table[x]=table[y];
table[y]= m;
}
}
No comments:
Post a Comment