Creat class Sortearch where the data members and methods are as follows:
Instance variables : int arr [ ] ,n
Methods:
Void accept (): accept n numbers from the user
Void sort ( ) : Sort the numbers in ascending order either using bubble sort logic
Void disp () : display the original and sorted array
Answers
import java.util.*;
class Sortearch
{
int n;
int arr[]=new int[n];
int b[]=new int[n];
public void main(String args[])
{
accept();
}
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number of elements");
n=sc.nextInt();
System.out.println("Enter the array elements");
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
b[i]=arr[i];
}
sort();
}
void sort()
{
for(int i=0;i<n-1;i++)
{
for(int j=0;j<(n-1-i);j++)
{
if(arr[j]>arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
disp();
}
void disp()
{
System.out.println("The elements in the array are arranged in acending:");
System.out.println("After that I printed the original");
for(int i=0;i<n;i++)
{
System.out.println(arr[i]);
System.out.println(b[i]);
}
}
}
===============================
Hope it helps :-)
______________________________________________________________
import java.util.*;
class selection_sort
{
public void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of elements in an array");
int n=sc.nextInt();
int a[]=new int[n];
System.out.println("Enter the array values randomly ! I will sort it ! I know magic");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
int min=0;
int temp=0;
for(int i=0;i<(n-1);i++)
{
min=i;
for(int j=i+1;j<n;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
System.out.println("Sorted array in ascending order gives all negative first then positive hehe");
for(int i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}
Hope helped