Computer Science, asked by oishikmuk735, 1 year ago

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

Answered by Anonymous
1

\huge{\textsf{\underline{\underline{\underline{\underline{\underline{CODE}}}}}}}}}



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 :-)

______________________________________________________________


Anonymous: welcome :-) but I don't think my code is correct it will show logical error don't know why
Anonymous: u cant get the correct output try once i will see it tomorrow again gud nyt:-)
oishikmuk735: Is it better to run this code by bufferedreader?
oishikmuk735: I find that easier!
oishikmuk735: Ok
Anonymous: ok do it i have habit of scanner class thats why
oishikmuk735: Nah..thats ok!
oishikmuk735: I have some problem with a pattern can u solve it tommorow?
Anonymous: sure inbox me
oishikmuk735: Ok
Answered by Anonymous
0

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

Similar questions