Skip to content

Instantly share code, notes, and snippets.

@claudhu
Created May 2, 2017 04:48
Show Gist options
  • Save claudhu/cd20568dcd78091887087732405cea53 to your computer and use it in GitHub Desktop.
Save claudhu/cd20568dcd78091887087732405cea53 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
class BubbleSort{
public static void main(String args[]){
final int ARRAY_SIZE = 10; // the size that you want
int [] randomIntArray = new int[ARRAY_SIZE];
for(int i = 0 ; i < ARRAY_SIZE ; i++){
randomIntArray[i] = (int) (Math.random()*10); //Push value into Array
}
//Check our Array value
for(int i = 0 ; i < ARRAY_SIZE ; i++){
System.out.printf("%d,",randomIntArray[i]);
}
System.out.println("\n=====After Sorted=====");
// let's use our sort alogrithm
for(int i = 0 ; i< ARRAY_SIZE ; i++){
for(int j = 0 ; j <ARRAY_SIZE-1 ; j++){
if(randomIntArray[j] > randomIntArray[j+1]){
// let's swap our value
int temp = randomIntArray[j];
randomIntArray[j] = randomIntArray[j+1];
randomIntArray[j+1] =temp;
}
}
}
//Check our Array value
for(int i = 0 ; i < ARRAY_SIZE ; i++){
System.out.printf("%d,",randomIntArray[i]);
}
// let's do binarySort
Scanner input = new Scanner(System.in);
int key = input.nextInt();
int low = 0;
int hight = randomIntArray.length;
while(low != hight){
boolean flag = false;
if(key == randomIntArray[(low+hight)/2]){
System.out.printf("index of key is %d",(low+hight)/2);
break;
}else if( key > randomIntArray[(low+hight)/2]){
low = (low+hight) / 2 + 1;
}else if (key < randomIntArray[(low+hight)/2]){
hight = ( low + hight) / 2 - 1;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment