Last active
June 16, 2020 17:30
-
-
Save deathmarine/ebf9b9f93879bebfc4eb676f228d5a4f to your computer and use it in GitHub Desktop.
Bubble Sort, Linear Search, Binary Search
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.Arrays; | |
import java.util.Random; | |
public class Main { | |
public static void main(String[] args) { | |
Random generator = new Random(); | |
int size = 1_000_000; //Edit me to change the size of the array | |
/* | |
* 100,000 = ~16 Seconds | |
*/ | |
int[] a = new int[size]; //Instantiate array | |
int n = a.length; //Determine the size of the array | |
int i = 0, j = 0, location, x; //Instantiate variables | |
//Begin Randomizing | |
a = new int[size]; //Re-instantiate array (Future Proof) | |
n = a.length-1; //Re-Determine the size of the array (Future Proof) | |
for(i=0; i<size; i++) { | |
a[i] = generator.nextInt(size); | |
} | |
//End Randomizing | |
System.out.println(Arrays.toString(a)); //Uncomment to see the unsorted array | |
long time = System.currentTimeMillis(); | |
//Start - Bubble Sort Algorithm | |
for(i = 0; i < n; i++) { | |
for(j = 0; j < n-i; j++) { | |
if(a[j] > a[j+1]) { | |
int higher = a[j]; | |
int lower = a[j+1]; | |
a[j] = lower; | |
a[j+1] = higher; | |
} | |
} | |
} | |
//Stop - Bubble Sort Algorithm | |
System.out.println(Arrays.toString(a)); //Uncomment to see the sorted array | |
long time2 = System.currentTimeMillis() - time; | |
System.out.println("Time in millisec: "+time2); | |
time = System.nanoTime(); | |
//Now we can search within the array. | |
//Method: Linear Search (This will only find the very first instance of the match) | |
x = 42; //Assign the search integer | |
i = 0; //Start position | |
while(i <= n && x != a[i]) { //while i (position) is less than or equal to the length | |
//and our search integer does not equal the position in the array | |
i++; //Increment i each iteration. | |
} | |
if(i <= n) { //location check if the value has been found | |
location = i; //if true the position variable is copied to the location | |
}else { | |
location = 0; //if false the position is zero indicating it was not found | |
} | |
System.out.println("The answer to life and the universe is found at: "+location); | |
time2 = System.nanoTime() - time; | |
System.out.println("Time in nanosec: "+time2); | |
time = System.nanoTime(); | |
//Binary Search (This will only find the first instance of the match) | |
x = 42; //Assign the search integer | |
i = 0; //First position | |
j = a.length-1; //Last Position | |
while(i < j) { | |
int m = (i+j)/2; //Division of Integers in java will roll off remainder. | |
if(x > a[m]) { | |
i = m+1; | |
} else { | |
j = m; | |
} | |
} | |
if(x == a[i]) { | |
location = i; | |
}else { | |
location = 0; | |
} | |
System.out.println("The answer to life and the universe is found at: "+location); | |
time2 = System.nanoTime() - time; | |
System.out.println("Time in nanosec: "+time2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment