Created
November 3, 2014 04:58
-
-
Save montycheese/bdd4da7fc93f0950c4a4 to your computer and use it in GitHub Desktop.
Sorting Algorithms
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.util.Scanner; | |
public class SortingAlgorithms { | |
public static void main(String[] args) { | |
int[] anArray = {2, 3, 1, 5, 6, 33, 10, 15, 16, 4, 7, 1000}; | |
int searchTerm; | |
System.out.println("Starting array"); | |
for (int x = 0; x < anArray.length; x ++) | |
System.out.printf("%d ", anArray[x]); | |
anArray = bubbleSort(anArray); | |
System.out.println("\nBubble Sort:"); | |
for (int x = 0; x < bubbleSort(anArray).length; x ++) | |
{ | |
System.out.printf("%d ", anArray[x]); | |
} | |
System.out.print("\nsearch for a number 1-7: "); | |
Scanner s = new Scanner(System.in); | |
searchTerm = s.nextInt(); | |
if (linearSearch(anArray, searchTerm) == -1) | |
System.out.println("not found"); | |
else | |
System.out.println("found at index " + linearSearch(anArray, searchTerm)); | |
int[] anArray2 = {2, 3, 1, 5, 6, 33, 10, 15, 16, 4, 7, 1000}; | |
System.out.println("insertionsort"); | |
anArray2 = insertionSort(anArray); | |
for (int x = 0; x < insertionSort(anArray2).length; x ++) | |
{ | |
System.out.printf("%d ", anArray2[x]); | |
} | |
System.out.print("\nsearch for a number 1-7: "); | |
searchTerm = s.nextInt(); | |
if (binarySearch(anArray, searchTerm) == -1) | |
System.out.println("not found"); | |
else | |
System.out.println("found at index " + binarySearch(anArray, searchTerm)); | |
} | |
public static int[] bubbleSort(int[] aArray){ | |
int temp; | |
for (int i = 0; i < aArray.length; i++) | |
{ | |
for (int x = 0; x < aArray.length -1; x++) | |
{ | |
if (aArray[x] > aArray[x+1]) | |
{ | |
temp = aArray[x]; | |
aArray[x] = aArray[x+1]; | |
aArray[x+1] = temp; | |
} | |
} | |
} | |
return aArray; | |
} | |
public static int linearSearch(int[] aArray, int searchTerm){ | |
int index = -1; | |
for (int x= 0; x < aArray.length; x++) | |
{ | |
if (aArray[x] == searchTerm) | |
{ | |
index = x; | |
return index; | |
} | |
} | |
return index; | |
} | |
public static int[] insertionSort(int[] aArray){ | |
int max = aArray[0], i,temp; | |
for (int x = 0; x < aArray.length; x++) | |
{ | |
i = x+1; | |
while (i < aArray.length) | |
{ | |
if (aArray[i] < aArray[i-1]) | |
{ | |
temp = aArray[i-1]; | |
aArray[i-1] = aArray[i]; | |
aArray[i] = temp; | |
} | |
i++; | |
} | |
} | |
return aArray; | |
} | |
public static int binarySearch(int[] aArray, int searchTerm){ | |
int start, end, mid; | |
start = 0; | |
end = aArray.length - 1; | |
while (start < end) | |
{ | |
mid = (start + end)/2; | |
if (mid < searchTerm) | |
{ | |
start = mid + 1; | |
} | |
else | |
end = mid; | |
} | |
if (aArray[start] == searchTerm) | |
return start; //index of found value | |
else return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment