0% found this document useful (0 votes)
3 views6 pages

Arrays Sorting Programs

Uploaded by

hr.kumarshubham
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Arrays Sorting Programs

Uploaded by

hr.kumarshubham
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 8: Arrays

2. Write a program that accepts 10 numbers in an array and searches for a value
entered by the user using linear search. If the searched item is found, print "Search
successful", otherwise print "Search failed".
Ans.
import [Link].*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int arr[] = new int[10];
int flag = 0;
[Link]("Enter 10 numbers:");
for (int i = 0; i < 10; i++) arr[i] =
[Link](); [Link]("Enter number
to search"); int n = [Link]();
for (int i = 0; i < 10; i++)
{
if (arr[i] == n)
{
flag = 1;
break;
}
}
if (flag == 1)
[Link]("Search successful");
else
{
[Link]("Search failed");
}
}
}

3. Write a program that accepts 10 numbers in an array in an ascending order and


searches for a value entered by the user using the binary search technique. If the
searched item is found, print "Search successful", otherwise print "Search failed".
Ans.

import [Link].*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int arr[] = new int[10];
int flag = 0;
[Link]("Enter 10 numbers in ascending
order:");
for (int i = 0; i < 10; i++)
arr[i] = [Link](); [Link]("Enter number to
search"); int n = [Link]();
int beg = 0;
int end = [Link] - 1;
flag = binary_search(arr, n, beg, end);
if (flag == 1)
[Link]("Search successful");
else
{
[Link]("Search failed");
}
}
public static int binary_search(int arr[], int n, int b, int e)
{
int mid = (b + e) / 2;
while (b < e) {
if (arr[mid] == n)
return 1;
else if (arr[mid] < n)
b = mid + 1;
else {
e = mid - 1;
}
mid = (b + e) / 2;
}
return 0;
}
}

4. Write a program to accept the year of passing from school as an integer value from
the user. Find a particular year using the binary search technique on the sorted
array of integers. Display "Record exists", if the input value is located in the array. If
not, output the message "Record does not exist".
Sample array: [2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015]
Ans.
import [Link].*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int arr[] = new int[10];
int flag = 0;
[Link]("Enter year of passing:");
for (int i = 0; i < 10; i++) arr[i] =
[Link](); [Link]("Enter
year to search"); int n = [Link]();
int beg = 0;
int end = [Link] - 1;
flag = binary_search(arr, n, beg, end);
if (flag == 1)
[Link]("Record exists");
else
{
[Link]("Record does not exist");
}
}
public static int binary_search(int arr[], int n, int b, int e)
{
int mid = (b + e) / 2;
while (b < e)
{
if (arr[mid] == n)
return 1;
else if (arr[mid] < n)
b = mid + 1;
else
{
e = mid - 1;
}
mid = (b + e) / 2;
}
return 0;
}
}

5. Write a program that accepts 10 numbers in an array. Arrange it in ascending order


using selection sort. Print all the elements of the array.
Ans.
import [Link].*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int arr[] = new int[10];
[Link]("Enter 10 Number:"); for
(int i = 0; i < 10; i++)
arr[i] = [Link]();
for (int i = 0; i < 10 - 1; i++) {
int index = i;
for (int j = i + 1; j < 10; j++) {
if (arr[index] > arr[j])
index = j;
}
int temp = arr[i]; arr[i]
= arr[index]; arr[index]
= temp;
}
for (int i = 0; i < 10; i++)
[Link](arr[i] + " ");
}
}

6. Write a program that accepts the name and telephone number of 10 people.
Arrange them in alphabetical ascending order using bubble sort. Ans.
import [Link].*;
class bsort_2arr
{
public static void main(String args[])
{
Scanner input=new Scanner([Link]);
String ar[]=new String[10];
String mk[]=new String[10]; int
i,j,k,p;
String tmp,tmp2;

for(i=0;i<10;i++)
{
[Link]("Enter name");
ar[i]=[Link]();
[Link]("Enter telephone number");
mk[i]=[Link]();
}

for(j=0;j<9;j++)
{
for(k=0;k<9-j;k++)
if(ar[k].compareTo(ar[k+1])>0)
{ tmp=ar[k];
ar[k]=ar[k+1];
ar[k+1]=tmp;
tmp2=mk[k];
mk[k]=mk[k+1];
mk[k+1]=tmp2;
}
}
[Link]("SORTED LIST IS ....");
[Link]("NAME\t Telephone Number");
for(i=0;i<10;i++)
{
[Link](ar[i]+"\t"+mk[i]);
}

}
}

7. Write a program to input 100 integers into an array named ar[ ]. Arrange the first
50 integers in descending order using bubble sort and the next 50 integers in
ascending order using selection sort. Print the unsorted and sorted arrays.
Ans.
import [Link].*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]); int
arr[] = new int[100];
[Link]("Enter 100 numbers:");
for (int i = 0; i < 100; i++)
arr[i] = [Link]();
for (int i = 0; i < 50; i++) {
for (int j = 1; j < (50 - i); j++) {
if (arr[j - 1] < arr[j]) {
int temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
}
}
for (int i = 50; i < 100 - 1; i++) {
int min = i;
for (int j = i + 1; j < 100; j++) {
if (arr[j] < arr[i])
min = j;
}
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
[Link]("1st 50 elements are in
descending order then rest 50 elements are in
ascending order");
for (int i = 0; i < 100; i++)
[Link](arr[i] + " ");
}
}

8. Write a program to input age of 20 people and find the number of people in each
category given below:
1 to 20, 21 to 40, 41 to 60, 61 and above.
Ans.
import [Link].*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int arr[] = new int[20];
int teen = 0, mid_age = 0, adult = 0, senior_cit = 0;
[Link]("Enter age of 20 people:");
for (int i = 0; i < 20; i++)
arr[i] = [Link]();
for (int i = 0; i < 20; i++) {
if (arr[i] >= 1 && arr[i] <= 20)
teen++;
if (arr[i] >= 21 && arr[i] <= 40)
mid_age++;
if (arr[i] >= 41 && arr[i] <= 60)
adult++;
if (arr[i] >= 61)
senior_cit++;
}
[Link]("1 to 20 : " + teen + "\n21 to 40 : " +
mid_age + "\n41 to 60: " + adult + "\nabove 60: " +
senior_cit);
}
}

You might also like