Skip to content

Commit bbc9f0d

Browse files
Merge pull request #11 from anthonyc1/java-algo
Implement Linear Search, and Quick Sort
2 parents d8f361b + c2c6c8b commit bbc9f0d

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

LinearSearch.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.util.Scanner;
2+
3+
public class LinearSearch{
4+
//Main method
5+
public static void main(String[] args){
6+
7+
Scanner input = new Scanner(System.in);
8+
int[] myArray;
9+
int size = 0;
10+
11+
//Prompt user to create array and its elements
12+
System.out.print("Enter the array size: ");
13+
size = input.nextInt();
14+
myArray = new int[size];
15+
for (int i = 0; i < size; i++){
16+
System.out.print("For index " + i + ", enter an integer: ");
17+
myArray[i] = input.nextInt();
18+
}
19+
20+
//Prompt user to search for particular element
21+
System.out.print("Enter integer to search for: ");
22+
int key = input.nextInt();
23+
24+
//Output array and index of target element, if found
25+
printarray(myArray);
26+
System.out.printf("The integer %d is found in index %d\n", key, linearsearch(myArray, key));
27+
28+
}
29+
30+
//Linear search method
31+
public static int linearsearch(int[] list, int key){
32+
33+
for (int i = 0; i< list.length; i++){
34+
if (list[i] == key){
35+
return i;
36+
}
37+
38+
} return -1;
39+
}
40+
//Helper method
41+
public static void printarray(int[] a){
42+
System.out.print("The array is: ");
43+
for( int d: a){
44+
System.out.print(d+" ");
45+
}
46+
System.out.println();
47+
}
48+
}

Quicksort.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import java.util.Scanner;
2+
3+
public class Quicksort{
4+
5+
public static void main(String[] args){
6+
Scanner input = new Scanner(System.in);
7+
int[] array;
8+
int size = 0;
9+
10+
//Prompt user to create array and its elements
11+
System.out.print("Enter the size of the array: ");
12+
size = input.nextInt();
13+
array = new int[size];
14+
for (int i = 0; i < size; i++){
15+
System.out.print("For index " + i + ", give an integer input: ");
16+
array[i] = input.nextInt();
17+
}
18+
19+
//Output inputted array
20+
System.out.println("The array is: ");
21+
printarray(array);
22+
System.out.println();
23+
24+
//Run quicksort, and output sorted array
25+
quicksort(array, 0, array.length - 1);
26+
System.out.println("The sorted array is: ");
27+
printarray(array);
28+
System.out.println();
29+
}
30+
31+
//Quicksort Method
32+
public static void quicksort(int[] ar, int start, int end){
33+
int[] array;
34+
35+
int i = start, j = end;
36+
if (end-start >= 1){
37+
int pivot = ar[end];
38+
while (i< j){
39+
while (ar[i]<pivot && i<end){
40+
i++;
41+
}
42+
while (ar[j]>=pivot && j>start){
43+
j--;
44+
}
45+
if (i<j){
46+
swap(ar, i, j);
47+
}
48+
} swap(ar, end, i);
49+
50+
quicksort(ar, start, i-1);
51+
quicksort(ar, i+1, end);
52+
} else{
53+
return;
54+
}
55+
}
56+
57+
//Helper methods
58+
public static void swap(int[] ar, int index1, int index2){
59+
60+
int temp = ar[index1];
61+
ar[index1] = ar[index2];
62+
ar[index2] = temp;
63+
}
64+
65+
public static void printarray(int[] array){
66+
67+
for (int data : array){
68+
System.out.print(data + " ");
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)