Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

EX NO 1 a) Implementation of sequential search

AIM
To develop a Java application to implement sequential search.

ALGORITHM

Step 1: First, read the array of n elements and the search element (Target element) in the
array.

Step 2: In the second step compare the search element with the first element in the array.

Step 3: If both are matched, return the index value of an element and display the message
“Target element is found".

Step 4: If both are not matched, compare the search element with the next element in the
array.

Step 5: In this step, repeat steps 3 and 4 until the search (Target) element is compared with
the last element of the array.

Step 6 - If the last element in the list does not match, the Linear Search Function will return
-1 and the message "Element is not found" will be displayed.

Program

SAMPLE INPUT & OUTPUT


Output:
G:\>javac linear_search.java
G:\>java linear_search
Enter the Size: 6
Enter the Array Elements
1 4 7 8 3 7
Enter the Element to Search: 2
Element is not present in the Array.
RESULT

Thus the java application to implement sequential search was executed and verified
successfully.

EX NO 1 b) Implementation of binary search


AIM
To develop a Java application to implement binary search.

ALGORITHM

Step 1: First, read the array of n elements and the search element (Target element) in the
array.

Step 2: Calculate the mid element of the collection.

Step 3: Compare the key item with the mid element.

Step 4: If key = middle element, then we return the mid index position for the key found.

Step 5: Else If key > mid element, then the key lies in the right half of the collection. Thus repeat
steps 1 to 3 on the lower (right) half of the collection.

Step 6: Else key < mid element, then the key is in the upper half of the collection. Hence you
need to repeat the binary search in the upper half.

Step 7: Repeat steps 2 to 6 until the value is found or not found in entire array.

Program

SAMPLE INPUT & OUTPUT


G:\java>javac binary.java
G:\java>java binary
Enter number of elements:
3
Enter 3 integers
10
20
30
Enter the search value:
50
50 is not found.
C:\java obs>java BinarySearch
Enter number of elements:
3
Enter 3 integers
10
20
30
Enter the search value:
20
20 found at location 2.

RESULT

Thus the java application to implement binary search was executed and verified
successfully.
EX NO 1 c) Implementation of Selection sort

AIM
To develop a Java application to implement selection sort.

ALGORITHM
Step 1. First, read the array of n elements
Step 2. Set MIN to location 0
Step 3. Search the minimum element in the list
Step 4. Swap with value at location MIN
Step 5. Increment MIN to point to next element
Step 6. Repeat until the array is sorted

Program

SAMPLE INPUT & OUTPUT

C:\java obs >javac SelectionSort.java


C:\java obs>java SelectionSort
Enter the Size of Array: 5
Enter 5 Elements for the Array:
50
10
40
20
30
The new sorted array is:
10 20 30 40 50

RESULT
Thus the java application to implement selection sort was executed and verified
successfully.

EX NO 1 d) Implementation of insertion sort

AIM
To develop a Java application to implement insertion sort.

ALGORITHM
Step 1. First, read the array of n elements
Step 2. If the element is the first element, assume that it is already sorted. Return 1.
Step 3. Pick the next element, and store it separately in a key.
Step 4. Now, compare the key with all elements in the sorted array
Step 5. If the element in the sorted array is smaller than the current element, then move to
the next element. Else, shift greater elements in the array towards the right.
Step 6. Insert the value
Step 7 - Repeat until the array is sorted

Program

SAMPLE INPUT & OUTPUT

C:\java obs >javac SelectionSort.java


C:\java obs>java SelectionSort
Enter the Size of Array: 5
Enter 5 Elements for the Array:
50
10
40
20
30
The new sorted array is:
10 20 30 40 50

RESULT

Thus the java application to implement insertion sort was executed and verified
successfully

You might also like