Last active
December 16, 2015 03:19
-
-
Save andrewjkerr/5368640 to your computer and use it in GitHub Desktop.
A simple example of Selection Sort, Linear Search, and Binary Search.
---
Originally for my Programming Fundamentals for CIS Majors 2 class at the University of Florida. The discussion instructions can be found here: http://www.cise.ufl.edu/class/cop3503fa12/discussion/disc11/ (as long as link stays live!)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.Math; | |
import java.util.Arrays; | |
/** | |
* Discussion 11 COP 3503 Fall 2012 | |
* | |
*/ | |
public class SearchingAndSorting { | |
public static void main(String[] args) { | |
double[] values = {6.4, 194.5, 4.3, 4.6, 65.3, 34.666, 1.44, 0.98, 0.11, 23.2, 5.5, 100.1}; | |
selectionSort(values); | |
System.out.print("Sorted Array of length " + values.length + " :"); | |
for (double i : values) | |
System.out.print(" " + i); | |
// now search | |
double toSearch = 3.4; | |
int index = linearSearch(values, toSearch); | |
System.out.println("\n\nClosest value with linear search for " + toSearch + ": " + values[index]); | |
// Arrays.sort(values); | |
toSearch = 23.2; | |
index = binarySearch(values, toSearch); | |
System.out.println("\n\nClosest value with binary search for " + toSearch + ": " + values[index]); | |
} | |
// Swaps two values at indices i and j in an array | |
public static void swap(double[] a, int i, int j) { | |
double first = a[i]; | |
a[i] = a[j]; | |
a[j] = first; | |
} | |
// Returns the index of the minimum value | |
// Finds minimum values between indices start and end | |
public static int minimum(double[] a, int start, int end) { | |
int min = start; | |
for(int i = start + 1; i <= end; i++){ | |
if(a[i] < a[min]){ | |
i = min; | |
} | |
} | |
return min; | |
} | |
// Selection sort of the double array - uses the swap and min methods | |
public static void selectionSort(double[] a) { | |
for(int i = 0; i < a.length - 1; i++){ | |
int minIndex = i; | |
for(int j = i + 1; j < a.length; j++){ | |
if(a[j] < a[minIndex]){ | |
minIndex = j; | |
} | |
} | |
if(minIndex != i){ | |
swap(a, i, minIndex); | |
} | |
} | |
} | |
// Linear Search | |
// returns the index of the value closest to "toSearch" | |
public static int linearSearch(double[] values, double toSearch) { | |
int indexMatched = 0; | |
double difference = Math.abs(values[0] - toSearch); | |
for(int i = 1; i < values.length; i++){ | |
if(Math.abs(values[i] - toSearch) <= difference){ | |
indexMatched = i; | |
difference = Math.abs(values[i] - toSearch); | |
} | |
} | |
return indexMatched; | |
} | |
// Binary Search | |
// returns the index of the value closest to "toSearch" | |
public static int binarySearch(double[] values, double toSearch) { | |
int indexMatched = 0; | |
int left = 0; | |
int right = values.length; | |
double difference = Math.abs(values[0] - toSearch); | |
while(left <= right){ | |
int i = (left + right) / 2; | |
if(Math.abs(values[i] - toSearch) <= difference){ | |
indexMatched = i; | |
difference = Math.abs(values[i] - toSearch); | |
} | |
if(toSearch < values[i]){ | |
right = i - 1; | |
} | |
else if(toSearch > values[i]){ | |
left = i + 1; | |
} | |
else{ | |
indexMatched = i; | |
break; | |
} | |
} | |
return indexMatched; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment