0% found this document useful (0 votes)
7 views10 pages

Java Programs for Sorting and Searching

Uploaded by

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

Java Programs for Sorting and Searching

Uploaded by

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

Input/Output:

Sam
Judy
Suzy
Christ
Sisu
Ash
John
Jasmine
Daisy
Sankar
Jayden
Pushpak
Lohith
Raphael
Lithish
Adarsh
Lakshmi
Sonny
Divya
Priya
Enter the Alphabet: S
Sam
Suzy
Sisu
Sankar
Sonny

Variable description:
[Link] Variable Name Data Type Purpose
.
1 sc Scanner To take input from the user.
2 names[] String[] Array to store 20 names.
3 ch char Alphabet entered by the user.
4 i int Loop counter.
SELECTION SORT
Program –
Date:

Question: Write a program to store the names and total marks of 20 students in two
different 1 D arrays. Accept any name from the user and print the corresponding total
marks using any Search technique. Sort the name array in ascending order using
selection sort Technique.

Program Listing:
import [Link];
class Question22 {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
String names[] = new String[20];
int marks[] = new int[20];

// Input names and marks


[Link]("Enter 20 names and their
marks:");
for(int i=0; i<20; i++) {
names[i] = [Link]();
marks[i] = [Link]();
}

// Input name to search


[Link]("Enter a name to search: ");
String key = [Link]();

boolean found = false;


for(int i=0; i<20; i++) {
if(names[i].equalsIgnoreCase(key)) {
[Link]("Marks of " + names[i] +
" = " + marks[i]);
found = true;
break;
}
}
if(!found) [Link]("Name not found");

// Selection sort on names


for(int i=0; i<19; i++) {
int min = i;
for(int j=i+1; j<20; j++) {
if(names[j].compareToIgnoreCase(names[min]) < 0)
{
min = j;
}
}
// Swap
String temp = names[i];
names[i] = names[min];
names[min] = temp;

int t = marks[i];
marks[i] = marks[min];
marks[min] = t;
}

[Link]("Sorted names with marks:");


for(int i=0; i<20; i++) {
[Link](names[i]); }
}
}

Input/Output:
Input:
John 85
Sam 90
Judy 70
Sisu 100
Ash 100
John 91
Jasmine 82
Daisy 64
Sankar 86
Jayden 89
Pushpak 100
Lohith 98
Raphael 99
Lithish 100
Adarsh 96
Lakshmi 84
Sonny 78
Divya 80
Priya 100

Enter a name to search: Sam


Marks of Sam = 90
Sorted list:
Adarsh 96
Ash 100
Daisy 64
Divya 80
Jasmine 82
Jayden 89
John 85
Johny 91
Judy 70
Lakshmi 84
Lithish 100
Lohith 98
Priya 100
Pushpak 100
Raphael 99
Sam 90
Sankar 86
Sisu 100
Sonny 78

Variable description:
[Link] Variable Name Data Type Purpose
.
1 sc Scanner To take input from the user.
2 names[] String[] Array to store names of students.
3 marks[] int[] Array to store total marks of students.
4 key String Name to be searched.
5 found boolean Checks if the searched name is
found.
6 i, j int Loop counters for traversal and
sorting.
BUBBLE SORT
Program –
Date:

Question: Define a class and store the given city names in a single dimensional array.
Sort these names in alphabetical order using the Bubble Sort technique only.
INPUT: Delhi, Bangalore, Agra, Mumbai, Calcutta
OUTPUT: Agra, Bangalore, Calcutta, Delhi, Mumbai

Program Listing:
class Question23 {
public static void main(String args[]) {
String cities[] = {"Delhi", "Bangalore", "Agra",
"Mumbai", "Calcutta"};

// Bubble Sort
for(int i=0; i<[Link]-1; i++) {
for(int j=0; j<[Link]-1-i; j++) {

if(cities[j].compareToIgnoreCase(cities[j+1]
) > 0) {
String temp = cities[j];
cities[j] = cities[j+1];
cities[j+1] = temp;
}
}
}

// Display sorted cities


[Link]("Sorted Cities:");
for(int i=0; i<[Link]; i++) {
[Link](cities[i]);
}
}
}
Output:
Agra
Bangalore
Calcutta
Delhi
Mumbai

Variable description:
[Link] Variable Name Data Type Purpose
.
1 cities[] String[] Array to store city names.
2 i, j int Loop counters for bubble sort.
3 temp String Temporary variable for swapping
cities.
[Link] Variable Name Data Type Purpose
.
1 cities[] String[] Array to store city names.
2 i, j int Loop counters for bubble sort.
Input/Output:
Delhi 11
Mumbai 22
Kolkata 33 ... (10 cities)
Enter city name to search: Mumbai
Search Successful: Mumbai - 22

Variable description:
[Link] Variable Name Data Type Purpose
.
1 sc Scanner To take input from the user.
2 cities[] String[] Array to store names of cities.
3 codes[] int[] Array to store STD codes of cities.
4 key String City name to be searched.
5 found boolean Checks if the searched city exists.
6 i int Loop counter.
2D ARRAY
Program –
Date:

Question: Define a class to accept values into a 3×3 array and check if it is a special
array. An array
is a special array if the sum of the even elements = sum of the odd elements.
Example:
A[][]={{ 4 ,5, 6}, { 5 ,3, 2}, { 4, 2, 5}};
Sum of even elements = 4+6+2+4+2 =18
Sum of odd elements= 5+5+3+5=18

Program Listing:
import [Link];
class Question25 {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
int arr[][] = new int[3][3];

// Input 3x3 matrix


[Link]("Enter 9 numbers for 3x3
matrix:");
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
arr[i][j] = [Link]();
}
}

int evenSum = 0, oddSum = 0;


for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if(arr[i][j] % 2 == 0) evenSum += arr[i][j];
else oddSum += arr[i][j];
}
}

[Link]("Sum of even elements = " +


evenSum);
[Link]("Sum of odd elements = " +
oddSum);
if(evenSum == oddSum)
[Link]("It is a Special Array");
else
[Link]("It is Not a Special Array");
}
}

Input/Output:
456
532
425

Sum of even elements = 18


Sum of odd elements = 18
It is a Special Array

Variable description:
[Link] Variable Name Data Type Purpose
.
1 sc Scanner To take input from the user.
2 arr[][] int[][] 3x3 matrix to store numbers.
3 i, j int Loop counters for matrix traversal.
4 evenSum int Stores sum of even elements.
5 oddSum int Stores sum of odd elements.
[Link] Variable Name Data Type Purpose
.

You might also like