From 10c9e6adeccf515c367b9d52c0f7136ba35fc7b1 Mon Sep 17 00:00:00 2001 From: Anthony Chan Date: Wed, 15 Mar 2017 00:06:52 -0400 Subject: [PATCH 1/2] Add Linear Search --- LinearSearch.java | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 LinearSearch.java diff --git a/LinearSearch.java b/LinearSearch.java new file mode 100644 index 000000000..820785938 --- /dev/null +++ b/LinearSearch.java @@ -0,0 +1,48 @@ +import java.util.Scanner; + +public class LinearSearch{ + //Main method + public static void main(String[] args){ + + Scanner input = new Scanner(System.in); + int[] myArray; + int size = 0; + + //Prompt user to create array and its elements + System.out.print("Enter the array size: "); + size = input.nextInt(); + myArray = new int[size]; + for (int i = 0; i < size; i++){ + System.out.print("For index " + i + ", enter an integer: "); + myArray[i] = input.nextInt(); + } + + //Prompt user to search for particular element + System.out.print("Enter integer to search for: "); + int key = input.nextInt(); + + //Output array and index of target element, if found + printarray(myArray); + System.out.printf("The integer %d is found in index %d\n", key, linearsearch(myArray, key)); + + } + + //Linear search method + public static int linearsearch(int[] list, int key){ + + for (int i = 0; i< list.length; i++){ + if (list[i] == key){ + return i; + } + + } return -1; + } + //Helper method + public static void printarray(int[] a){ + System.out.print("The array is: "); + for( int d: a){ + System.out.print(d+" "); + } + System.out.println(); + } +} \ No newline at end of file From c2c6c8b66f22d980bd780a4d4031d5232d598b83 Mon Sep 17 00:00:00 2001 From: Anthony Chan Date: Wed, 15 Mar 2017 00:17:33 -0400 Subject: [PATCH 2/2] Implement quicksort --- Quicksort.java | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Quicksort.java diff --git a/Quicksort.java b/Quicksort.java new file mode 100644 index 000000000..f693c42b6 --- /dev/null +++ b/Quicksort.java @@ -0,0 +1,71 @@ +import java.util.Scanner; + +public class Quicksort{ + + public static void main(String[] args){ + Scanner input = new Scanner(System.in); + int[] array; + int size = 0; + + //Prompt user to create array and its elements + System.out.print("Enter the size of the array: "); + size = input.nextInt(); + array = new int[size]; + for (int i = 0; i < size; i++){ + System.out.print("For index " + i + ", give an integer input: "); + array[i] = input.nextInt(); + } + + //Output inputted array + System.out.println("The array is: "); + printarray(array); + System.out.println(); + + //Run quicksort, and output sorted array + quicksort(array, 0, array.length - 1); + System.out.println("The sorted array is: "); + printarray(array); + System.out.println(); + } + + //Quicksort Method + public static void quicksort(int[] ar, int start, int end){ + int[] array; + + int i = start, j = end; + if (end-start >= 1){ + int pivot = ar[end]; + while (i< j){ + while (ar[i]=pivot && j>start){ + j--; + } + if (i