Skip to content

Implement Linear Search, and Quick Sort #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions LinearSearch.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
71 changes: 71 additions & 0 deletions Quicksort.java
Original file line number Diff line number Diff line change
@@ -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 && i<end){
i++;
}
while (ar[j]>=pivot && j>start){
j--;
}
if (i<j){
swap(ar, i, j);
}
} swap(ar, end, i);

quicksort(ar, start, i-1);
quicksort(ar, i+1, end);
} else{
return;
}
}

//Helper methods
public static void swap(int[] ar, int index1, int index2){

int temp = ar[index1];
ar[index1] = ar[index2];
ar[index2] = temp;
}

public static void printarray(int[] array){

for (int data : array){
System.out.print(data + " ");
}
}
}