Lab Manual
CPCS204
Data Structures 1
1432/1433H
Lab - 4
Laboratory 4:
Statement Purpose:
This lab will give you the description and implementation some basic Searching and Sorting Algorithms.
Activity Outcomes:
This lab teaches you the following topics:
Learn some well-known sorting algorithms with their implementations.
Programming practice on searching techniques in array elements.
Names I.D.
1. .………..………. ………
2. ..……….. ………
3. .………... ………
4. .……….. ..……….
CPCS204 –The Lab Note Lab-4 2
1) tage J (Journey)
Searching Algorithms:
A. Linear Search
linear search does O(n) comparisons on average.
B. Binary Search
Binary search does O(log n) comparisons at most which is much better
than linear search.
Linear Search Example:
In the given code, we have allowed the user to enter the numbers to be stored into the arrays. If the element is found we can return the index where the element is located in the array. If the element is not found we can return -1.
import java.util.*;
public class LinearSearch {
public int find(final int[] data, final int key) { for (int i = 0; i < data.length; ++i) {
if (data[i] > key) return -1;
else if (data[i] == key) return i;
}
return -1;
}
public static void main(String[] args) { final int arr[] = new int[10];
System.out.println("Enter 10 numbers");
Scanner input = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) { arr[i] = input.nextInt();
}
LinearSearch search = new LinearSearch();
System.out.print("Enter the element to search:
");
int num=input.nextInt();
int n = search.find(arr, num);
if ((n >= 0) && (n < arr.length)) {
System.out.println("Found at index: " + n);
} else {
System.out.println("Not Found");
} }
}
CPCS204 –The Lab Note Lab-4 4
Binary Searching ("divide and conquer“) Example:
Binary Searching involves searching for an element in an array, where the items are arranged in ascending or descending order. That means the entries in the array are sorted. If you want to learn how to write a
function that does this, keep reading.
Algorithm for Binary Search based on the following three cases:
Case 1: If the key is less than the middle element, recursively search the key in the first half of the array.
Case 2: If the key is greater than the middle element, recursively search the key in the second half of the array.
Case 3: If the key is equal to the middle element, the search ends with a match.
Carry on repeating the first two steps until the middle element is equal to the Key.
// Write Java code that will call the above BinarySearch() method to search any given Item.
Step1: Read Elements in the Array.
int BinarySearch(int a[], int value, int left, int right) {// See if the search has failed
if (left > right) return –1;
// See if the value is in the first half int middle = (left + right)/2;
if (value < a[middle])
return BinarySearch(a, value, left, middle – 1);
// See if the value is in the second half else if (value > a[middle])
return BinarySearch(a, value, middle + 1, right);
// The value has been found else
return middle;
}
Step 3: Call the Binary Search method and pass array and Item.
Step4: Print the desired result to the standard output.
import java.util.*;
public class Arr {
public static void main(String[] args) { int[] intArray = new int[10];
int searchValue = 0, index;
System.out.println("Enter 10 numbers");
Scanner input = new Scanner(System.in);
for (int i = 0; i < intArray.length; i++) { intArray[i] = input.nextInt();
}
System.out.print("Enter a number to search for: ");
searchValue = input.nextInt();
index = binarySearch(intArray, searchValue);
if (index != -1) {
System.out.println("Found at index: " + index);
} else {
System.out.println("Not Found");
} }
static int binarySearch(int[] search, int find) { int start, end, midPt;
start = 0;
end = search.length - 1;
while (start <= end) {
midPt = (start + end) / 2;
if (search[midPt] == find) { return midPt;
} else if (search[midPt] < find) { start = midPt + 1;
} else {
end = midPt - 1;
} }
return -1;}}
} }
CPCS204 –The Lab Note Lab-4 6