Created
May 2, 2017 04:48
-
-
Save claudhu/cd20568dcd78091887087732405cea53 to your computer and use it in GitHub Desktop.
This file contains hidden or 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; | |
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