0% found this document useful (0 votes)
28 views

Sorting and searching notes

The document provides notes on searching and sorting algorithms, specifically focusing on Bubble Sort and Linear Search techniques. It includes three Java programs for sorting numbers, names, and student marks, as well as three programs for searching elements in arrays, including student admission numbers and city names with their STD codes. Additionally, it presents homework questions related to searching and storing data using these techniques.
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)
28 views

Sorting and searching notes

The document provides notes on searching and sorting algorithms, specifically focusing on Bubble Sort and Linear Search techniques. It includes three Java programs for sorting numbers, names, and student marks, as well as three programs for searching elements in arrays, including student admission numbers and city names with their STD codes. Additionally, it presents homework questions related to searching and storing data using these techniques.
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
You are on page 1/ 7

Searching and Sorting notes

Sorting is a method of rearranging the elements in an array so that it occurs either


in ascending or descending order.

Bubble Sort: In this method of sorting, adjacent elements are checked with each
other, i.e., first element with the second element, the second element with the third
element and the process continues until all elements are exhausted. In case the
array element is greater than the adjacent element, the elements are interchanged.
The process continues until all the elements are exhausted.

Program 1

Write a program to accept any n numbers and sort them in Ascending order using
Bubble sort technique.

import java.util.*;
public class BubbleSortNumbers
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array");
int size=sc.nextInt();
int a[]=new int[size];
System.out.println("Enter"+" "+size+" "+"numbers");
for(int i=0; i<size; i++)
{
a[i]=sc.nextInt();
}
int i, j, temp;
for(i=0; i<size-1; i++)
{
for(j=0; j<size-1-i; j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

System.out.println("SORTED ARRAY IN ASCENDING ORDER");


for(i=0; i<size; i++)
{
System.out.println(a[i]);
}
}
}
Program 2: Write a program to sort names in ascending order.

import java.util.*;
public class BubbleSort
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String a[]=new String[5];

System.out.println("Enter any 5 names in the array");


for(int i=0; i<5; i++)
{
a[i]=sc.next();
}

System.out.println("UNSORTED ARRAY");
for(int i=0; i<5; i++)
{
System.out.print(a[i]+", ");
}

for(int i=0; i<5-1; i++)


{
for(int j=0; j<5-i-1; j++)
{
if(a[j].compareTo(a[j+1])>0)
{
String temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

System.out.println("SORTED ARRAY");
for(int i=0; i<5; i++)
{
System.out.print(a[i]+",");
}
}
}
Program 3: Write a program to enter marks and names of n students. Sort array in
descending order based on the marks obtained.

import java.util.*;
public class ComputerMarks
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the class");
int size=sc.nextInt();

String name[]=new String[size];


int m[]=new int[size];

System.out.println("Enter the names and marks of students");


for(int i=0; i<size; i++)
{
name[i]=sc.next();
m[i]=sc.nextInt();
}

for(int i=0; i<size-1; i++)


{
for(int j=0; j<size-i-1; j++)
{
if(m[j]<m[j+1])
{
int t=m[j];
m[j]=m[j+1];
m[j+1]=t;

String temp=name[j];
name[j]=name[j+1];
name[j+1]=temp;
}
}
}

System.out.println("The sorted array is");


for(int i=0; i<size; i++)
{
System.out.println("NAME\tMARKS");
System.out.println(name[i]+"\t"+m[i]);
}
}
}
Searching

Finding an element in an array and returning its position[index] is called searching.


Linear Search: In this type of search algorithm each and every element of the
array is compared one by one with the given item to be searched. This technique
traverses the array sequentially to locate the given item. This search technique is
called linear search or sequential search.

Program 1: Write a program to accept 10 numbers in an array. Now enter a number


and search whether it is present in the list or not.

import java.util.*;
public class LinearSearch
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
boolean found=false;
int pos=0;

System.out.println("Enter 10 numbers in the array");


for(int i=0; i<10; i++)
{
a[i]=sc.nextInt();
}

System.out.println("Enter the search element");


int search=sc.nextInt();

for(int i=0; i<10; i++)


{
if(a[i]==search)
{
found=true;
pos=i;
break;
}
}

if(found==true)
{
System.out.println("Search Successful");
System.out.println("The position of the search element is"+pos);
}
else
System.out.println("Search Unsuccessful");
}
}
Program 2
The school office keeps the records of all the students of a class by entering
Admission Number and the name of the students. Write a program to store all
names along with their corresponding admission numbers. Now, enter admission
number of a student and search whether the name is present or not. If the name is
present then display the name along with the admission number otherwise display
an appropriate message using Linear Search technique.

import java.util.*;
public class School
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array");
int n=sc.nextInt();
int an[]=new int[n];
String name[]=new String[n];
System.out.println("Enter the admission no and the corresponding name of
the student");
for(int i=0; i<n; i++)
{
an[i]=sc.nextInt();
name[i]=sc.next();
}
System.out.println("Enter the admission no whose name is to be searched");
int search=sc.nextInt();

int flag=0;
int pos=0;
for(int i=0; i<n; i++)
{
if(an[i]==search)
{
flag=1;
pos=i;
break;
}
}
if(flag==1)
{
System.out.println("Admission Number\tName of Student");
System.out.println(an[pos]+"\t"+name[pos]);
}
else
System.out.println("Search Unsuccessful. No such admission no found");
}
}
Program 3

Write a program to accept the names of 10 cities in a single dimensional String


array and their STD (Subscribers Trunk Dialing) codes in another single dimension
integer array. Search for the name of a city input by the user in the list. If found,
display “Search Successful” and print the name of the city along with its STD
code, or else display the message “Search Unsuccessful, no such city in the list.

import java.util.*;
public class City
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array");
int n=sc.nextInt();
int std[]=new int[n];
String city[]=new String[n];
System.out.println("Enter the std code and the name of the city");
for(int i=0; i<n; i++)
{
std[i]=sc.nextInt();
city[i]=sc.next();
}
System.out.println("Enter the name of the city whose STD code is to be
displayed");
String search=sc.next();
int flag=0;
int pos=0;
for(int i=0; i<n; i++)
{
if(city[i].equals(search))
{
flag=1;
pos=i;
break;
}
}

if(flag==1)
{
System.out.println("CITY NAME\tSTD CODE");
System.out.println(city[pos]+"\t"+std[pos]);
}
else
System.out.println("Search Unsuccessful. No such admission no found");
}
}
Home Work Questions

1) Write a program to store 20 different names along with corresponding


Telephone numbers. Enter a name from the console and search whether the
name is present of not. If the name is present then display the name along
with the phone number, otherwise display an appropriate message using
Linear Search technique.

2) Write a program to store names of 20 world-class batsmen with their


countries name and respective total number of centuries in different SDA.
The user wants to enlist the cricketers by entering the first letter of a
particular country viz ‘I’ for India, ‘A’ for Australia, ‘E’ for England and so
on. If found, then display name of the players with number of centuries
otherwise, “Search unsuccessful, Name not enlisted.

You might also like