This is an array program where 20 values are stored in an array and the elements are sorted. There is a twist in the sorting process, 1st 10 values will be sorted using selection sort technique and the last 10 values will be sorted using bubble sort.
import java.util.*;
class A
{
int arr[]=new int[20];
int i,j,t,flag;
Scanner sc=new Scanner(System.in);
void show()
{
for(i=0;i<20;i++)
{
System.out.print("\nValue:");
arr[i]=sc.nextInt();
}
System.out.println("\nElements as follows:");
for(i=0;i<20;i++)
{
System.out.print(" "+arr[i]);
}
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{
if(arr[i]>arr[j])
{
t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
}
}
for(i=10;i<20;i++)
{
flag=0;
for(j=10;j<30-i-1;j++)
{
if(arr[j]>arr[j+1])
{
flag=1;
t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
if(flag==0)
break;
}
System.out.println("\nAfter sorting, values as follows:");
for(i=0;i<20;i++)
{
System.out.print(" "+arr[i]);
}
}
public static void main(String args[])
{
A obj=new A();
obj.show();
}
}
Sample Input Output
Value:33
Value:22
Value:11
Value:22
Value:33
Value:44
Value:77
Value:6
Value:5
Value:4
Value:55
Value:6
Value:55
Value:8
Value:77
Value:5
Value:467
Value:54
Value:66
Value:78
Elements as follows:
33 22 11 22 33 44 77 6 5 4 55 6 55 8 77 5 467 54 66 78
After sorting, values as follows:
4 5 6 11 22 22 33 33 44 77 5 6 8 54 55 55 66 77 78 467
No comments:
Post a Comment