0% found this document useful (0 votes)
28 views9 pages

Oops Lab

The document outlines the development of Java applications for searching and sorting algorithms, including sequential search, binary search, selection sort, and insertion sort. Each section provides the aim, algorithm steps, Java program code, output, and results confirming successful implementation. The applications demonstrate fundamental data structure operations in Java.

Uploaded by

tannam1071980
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 views9 pages

Oops Lab

The document outlines the development of Java applications for searching and sorting algorithms, including sequential search, binary search, selection sort, and insertion sort. Each section provides the aim, algorithm steps, Java program code, output, and results confirming successful implementation. The applications demonstrate fundamental data structure operations in Java.

Uploaded by

tannam1071980
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/ 9

EX.NO.

:1(a)
SOLVE PROBLEMS BY USING SEQUENTIAL SEARCH
DATE:

AIM:
To develop a Java application to search an element in an array using sequential search
algorithm.

ALGORITHM:

Step 1: Start the program


Step 2:Declare an array and search element as key.
Step 3:Traverse the array until the key is found.
Step 4:If the key is found, return the index position of the array element
Step 5:If the key element is not found, return -1.
Step 6: Stop the program.
PROGRAM:
LinearSearch.java
public class LinearSearch
{
static intsearch(intarr[], int n, int s)
{
for (inti = 0; i< n; i++)
{
if (arr[i] == s)
return i;
}
return -1;
}
public static void main(String[] args)
{
int[] arr = { 3, 4, 1, 7, 5 };
int n = arr.length;
int s = 4;
int index = search(arr, n, s);
if (index == -1)
System.out.println("Element is not present in the array");
else
System.out.println("Element found at index " + index);
}
}

1
OUTPUT:
D:\Java\CS3381>javac LinearSearch.java
D:\Java\CS3381>java LinearSearch
Element found at index 1

RESULT:
Thus, the Java application to perform sequential search was implemented and executed
successfully.

2
EX.NO.:1(b)
SOLVE PROBLEMS BY USING BINARY SEARCH
DATE:

AIM:
To develop a Java application to search an element in an array using binary search
algorithm.

ALGORITHM:

Step 1: Start the program.


Step 2:Declare an array and search element as key.
Step 3:Compare search key with the middle element.
Step 4:If key matches with the middle element,return the mid index.
Step 5:Otherwise, if key is greater than the mid element recur for the right half.
Step 6:Otherwise (s is smaller) recur for the left half.
Step 7: Stop the program.
PROGRAM:
BinarySearch.java

class BinarySearch
{
public static intbinarySearch(intarr[], int first, int last, int key)
{
if (last>=first)
{
int mid = (first +last)/2;
if (arr[mid] == key)
{
return mid;
}
else if (arr[mid] >key)
{
return binarySearch(arr, first, mid-1, key);
}
else
{
return binarySearch(arr, mid+1, last, key);
}
}

3
return -1;
}
public static void main(String args[])
{
intarr[] = {10,20,30,40,50};
int key = 30;
int last=arr.length-1;
int result = binarySearch(arr,0,last,key);
if (result == -1)
System.out.println("Element is not found!");
else
System.out.println("Element is found at index: "+result);
}
}

OUTPUT:

D:\Java\CS3381>javac BinarySearch.java
D:\Java\CS3381>java BinarySearch
Element is found at index: 2
D:\Java\CS3381>

RESULT:
Thus, the Java application to perform binary search was implemented and executed
successfully.

4
EX.NO.:1(c) SOLVE PROBLEMS BY USING QUADRATIC SORTING
DATE: ALGORITHMS- SELECTION SORT

AIM:
To develop a Java application to sort an array of elements in ascending order using selection
sort.

ALGORITHM:

Step 1: Start the program.


Step 2:Select the first unsorted element as the minimum.
Step 3:For each of the unsorted elements, if the element is <minimum, set element as new
minimum.
Step 4:Swap minimum with first unsorted position.
Step 5:Repeat steps 2-4 for (n-1) elements until the list is sorted.
Step 6: Print the sorted array.
Step 7: Stop the program.

PROGRAM:
SelectionSort.java

public class SelectionSort


{
public static void selectionsort(int[] arr)
{
int n=arr.length;
for(inti=0;i<n-1;i++)
{
intmin=i;
for(int j=i+1;j<n;j++)
{
if(arr[j]<arr[min])
{
min=j;
}
}

5
int temp=arr[i];
arr[i]=arr[min];
arr[min]=temp;
}
}
public static void main(String[] args)
{
int[] arr= {15,21,6,3,19,20};
System.out.println("Elements in the array before Sorting");
for(int i:arr)
System.out.print(i+" ");
selectionsort(arr);
System.out.println("\nElements in the array after Sorting");
for(int i:arr)
System.out.print(i+" ");
}
}

OUTPUT:

D:\Java\CS3381>javac SelectionSort.java
D:\Java\CS3381>java SelectionSort
Elements in the array before Sorting
15 21 6 3 19 20
Elements in the array after Sorting
3 6 15 19 20 21
D:\Java\CS3381>

RESULT:
Thus, the Java application to sort an array of N elements using selection sort was
implemented and executed successfully.

6
EX.NO.:1(d) SOLVE PROBLEMS BY USING QUADRATIC SORTING
DATE: ALGORITHMS-INSERTION SORT

AIM:
To develop a Java application to sort an array of elements in ascending order using insertion
sort.

ALGORITHM:

Step 1: Start the program.


Step 2:Define an array num to store N numbers for insertion sort.
Step 3:Run an outer loop i from 1 to N to repeat the process.
Step 4:Store the number num[i] to be inserted at proper place in variable x.
Step 5:Run a while loop j inside the body of the outer loop i from i-1 to 0.
Step 6:Check if the value of x is less than value of num[j] then shift the
number num[j] towards right else break the inner loop j.
Step 7:Outside the body of inner loop j insert the value of x at num[j+1] position.
Step 8:Print the sorted array.
Step 9: Stop the program.
PROGRAM:
InsertionSort.java

public class InsertionSort


{
public static void main(String args[])
{
intnum[]= {12,9,37,86,2,17,5};
inti,j,x;
System.out.println("Array before Insertion Sort");
for(i=0; i<num.length; i++)
{
System.out.print(num[i]+" ");
}
for(i=1; i<num.length; i++)
{
x=num[i];
j=i-1;
while(j>=0)
{
if(x<num[j])
{

7
num[j+1]=num[j];
}
else
{
break;
}
j=j-1;
}
num[j+1]=x;
}
System.out.print("\n\nArray after Insertion Sort\n");
for(i=0; i<num.length; i++)
{
System.out.print(num[i]+" ");
}
}
}

OUTPUT:

D:\Java\CS3381>javac InsertionSort.java
D:\Java\CS3381>java InsertionSort
Array before Insertion Sort
12 9 37 86 2 17 5
Array after Insertion Sort
2 5 9 12 17 37 86
D:\Java\CS3381>

RESULT:
Thus, the Java application to sort an array of N elements using insertion sort was
implemented and executed successfully.

You might also like