Sorting and searching notes
Sorting and searching notes
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;
}
}
}
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("UNSORTED ARRAY");
for(int i=0; i<5; i++)
{
System.out.print(a[i]+", ");
}
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 temp=name[j];
name[j]=name[j+1];
name[j+1]=temp;
}
}
}
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;
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
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