Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class Searches {
- public static void sequentialSearch(int[] arr, int target){
- for (int i : arr){
- if (i == target)
- System.out.println("Found");
- }
- System.out.println("Not found");
- }
- public static void binarySearch(int[] arr, int target){
- int left = 0, right = arr.length - 1;
- while (left <= right){
- int middle = (left + right) / 2;
- if (arr[middle] < target)
- left = middle + 1;
- else if(arr[middle] > target)
- right = middle - 1;
- else //the case when target equals the middle value
- System.out.println("Found");
- }
- System.out.println("Not found");
- }
- public static void selectionSort(int[] array) {
- // Select
- for (int i = 0; i < array.length - 1; i++) {
- // We're searching the correct value at index i
- int indexOfMin = i;
- for (int j = i + 1; j < array.length; j++) {
- if (array[j] < array[indexOfMin]) {
- indexOfMin = j;
- }
- }
- // Swap
- int temp = array[indexOfMin];
- array[indexOfMin] = array[i];
- array[i] = temp;
- }
- }
- public static void main(String[] args){
- int[] x = {2, 3, 4, 1, 6, 5, 7};
- //sequentialSearch(x, 6);
- int[] y = {2, 3, 4, 5, 6};
- //binarySearch(y, 5);
- selectionSort(x);
- System.out.println(Arrays.toString(x));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement