Skip to content

Instantly share code, notes, and snippets.

@alexleventer
Last active July 13, 2017 19:16
Show Gist options
  • Save alexleventer/f833e493d97c92ee0fe79e40dc0d57d6 to your computer and use it in GitHub Desktop.
Save alexleventer/f833e493d97c92ee0fe79e40dc0d57d6 to your computer and use it in GitHub Desktop.
public class BinarySearch
{
public static int binarySearch(int[] a, int target)
{
int left = 0, right = a.length -1;
while (left <= right)
{
int mid = (left + right) / 2;
if(a[mid] == target)
return mid;
else if (a[mid] < target)
left = mid + 1;
else
right = mid -1;
}
return -1;
}
public static void main(String[] args)
{
}
}
public class SelectionSort
{
public static int[] selectionSort(int[] a)
{
int temp, min;
for (int i = 0; i < a.length - 1; i++)
{
min = i;
for (int j = i + 1; j < a.length; i++)
{
if (a[j] < a[min])
min = j
}
if (min != i)
{
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
}
public static void main(String[] args)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment