Assignment-6

Assignment-6 A class sort contains any array of 50 integers. Some of the member fns or data members are given below Class name-Sort Data member-arr[]- integers item- no. to be searched in the array Member methods- void inputdata()- to i/p 50 integers(no duplicate no.s are to be entered) void bubblesort()- to sort the array in ascending order using the bubble sort technique and to display the sorted list void binarysearch()- to i/p item and search for it using the binary search technique. If found print the item searched and its position in the sorted list. Otherwise print an appropriate message. Write a main function to perform the following operation details. Source Code: import java.io.*; class Sort { BufferedReader br1=new BufferedReader(new InputStreamReader(System.in)); int arr [] = new int [50]; int item,n; void inputdata()throws IOException { System.out.println("Enter number of data:"); n=Integer.parseInt(br1.readLine()); for(int i=0;iup) System.out.println("item="+item+" not found in the list"); }while(low<=up && item!=arr[mid]); } } ************MAIN FUNCTION************* class main { public static void main(String ars[])throws IOException { BufferedReader br2=new BufferedReader(new InputStreamReader(System.in)); int s1=0,s2=0; Sort ob=new Sort(); while(true) { if(s1==0) { System.out.println("\nEnter 1 to take input data"); } System.out.println("\nEnter 2 to sort the list of data using bubblesort"); System.out.println("\nEnter 3 for search an element using binary search"); System.out.println("\nEnter 4 to exit"); System.out.println("\nEnter our choice:"); int ch=Integer.parseInt(br2.readLine()); switch(ch) { case 1: ob.inputdata(); s1=1; break; case 2: if(s1==0) System.out.println("\nPlease first take input some data"); else { s2=1; ob.bubblesort(); } break; case 3: if(s1==0) System.out.println("\nPlease first take input some data"); else if(s2==0) System.out.println("\nPlease first sort the list then search"); else ob.binarysearch(); break; case 4: System.exit(0); } } } } OUTPUT: D:\java>javac assignment6.java D:\java>java main Enter 1 to take input data Enter 2 to sort the list of data using bubblesort Enter 3 for search an element using binary search Enter 4 to exit Enter our choice: 1 Enter number of data: 6 Enter data 1 : 23 Enter data 2 : 56 Enter data 3 : 43 Enter data 4 : 11 Enter data 5 : 98 Enter data 6 : 23 Enter 2 to sort the list of data using bubblesort Enter 3 for search an element using binary search Enter 4 to exit Enter our choice: 2 Sorted array is: 11 23 23 43 56 98 Enter 2 to sort the list of data using bubble sort Enter 3 for search an element using binary search Enter 4 to exit Enter our choice: 3 Enter your item: 23 item=23 found at position 3 Enter 2 to sort the list of data using bubble sort Enter 3 for search an element using binary search Enter 4 to exit Enter our choice: 3 Enter your item: 56 item=56 found at position 5 Enter 2 to sort the list of data using bubblesort Enter 3 for search an element using binary search Enter 4 to exit Enter our choice: 4

Share:

0 comments