Download Binary Search

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
KINGABDULAZIZUNIVERSITY
Faculty of Computing & Information Technology
Department of Computer Science
Lab Manual
CPCS204
Data Structures 1
1432/1433H
Lab - 4
Term I
2013
Lab-4: Searching and Sorting Algorithms
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
1
Term I
2013
Lab-4: Searching and Sorting Algorithms
1) tageJ(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.
CPCS204 –The Lab Note
Lab-4
2
Term I
2013
Lab-4: Searching and Sorting Algorithms
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
3
Term I
2013
Lab-4: Searching and Sorting Algorithms
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.
Step2: Read the Item which you want to search in the Array.
Step 3: Call the Binary Search method and pass array and Item.
Step4: Print the desired result to the standard output.
CPCS204 –The Lab Note
Lab-4
4
Term I
2013
Lab-4: Searching and Sorting Algorithms
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
5
Term I
2013
Lab-4: Searching and Sorting Algorithms
Homework:
Write a java code that allow the user to enter 10 number then search about a
particular number by using binary search but by using Joption input class.
CPCS204 –The Lab Note
Lab-4
6