Page 415 - CA_Blue( J )_Class10
P. 415
}
}
}
System.out.println("Names in sorted order :");
for(i = 0; i < 20; i++)
System.out.print(n[i] + " , ");
}
}
22. Write a program to accept the year of graduation from school as an integer value from the user. Using the binary search
technique on the sorted array of integers given below, output the message "Record exists" if the value input is located in the
array. If not, output the message "Record does not exist".
{1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010} [2014]
Ans. import java.util.*;
class Record{
public static void main() {
Scanner sc= new Scanner(System.in);
int yr,min=0,max,mid,i,pos=-1;
int a[] = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010};
System.out.print("Enter the year of graduation: ");
yr = sc.nextInt();
max=a.length-1;
while(min <= max) {
mid = (min + max) / 2;
if(yr == a[mid])
{ pos = mid;
break;
}
else if(yr < a[mid])
max = mid - 1;
else
min = mid + 1;
}
if(pos!=-1)
System.out.println("Record exists");
else
System.out.println("Record does not exists");
}
}
23. Write a program to input 10 integer elements in an array and sort them in descending order using bubble sort
technique. [2013]
Ans. import java.util.*;
class Bubblesort {
public static void main(){
Scanner sc = new Scanner(System.in);
int a[] = new int[10];
int i,j,temp;
System.out.println("Enter 10 integers:");
for(i = 0; i < a.length; i++)
a[i] = sc.nextInt();
for(i = 0; i < a.length-1; i++){
for(j = 0; j < a.length - 1 - i; j++){
if(a[j] < a[j + 1]){
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
413
Arrays 413

