Selenium is open Source Automation testing tool.SEO Adviser is consultant provide SEO ,SMO and PPC services, How to grow business on Google,Bing,Yahoo and other Search Engine.
Sunday, 27 April 2014
Call India, Cheapest Call to India and Send Text Message to India form Anywhere: Searching programming in Java
Call India, Cheapest Call to India and Send Text Message to India form Anywhere: Searching programming in Java: Linear serch in JAVA: public class L LinearSearch { public static int linerSearch(int[] arr, int key){ ...
Searching programming in Java
Linear serch in JAVA:
public class LLinearSearch
{
public static int linerSearch(int[]
arr, int key){
int size
= arr.length;
for(int i=0;i<size;i++){
if(arr[i]
== key){
return i;
}
}
return -1;
}
public static void main(String
a[]){
int[]
arr1= {23,45,21,55,234,1,34,90};
int searchKey
= 34;
System.out.println("Key
"+searchKey+" found at index: "+linerSearch(arr1, searchKey));
int[]
arr2= {123,445,421,595,2134,41,304,190};
searchKey
= 421;
System.out.println("Key
"+searchKey+" found at index: "+linerSearch(arr2, searchKey));
}
}
Binary serch In JAVA:
public class BinarySearch
{
public int binarySearch(int[]
inputArr, int key) {
int start
= 0;
int end
= inputArr.length - 1;
while (start
<= end) {
int mid
= (start + end) / 2;
if (key
== inputArr[mid]) {
return mid;
}
if (key
< inputArr[mid]) {
end
= mid - 1;
}
else {
start
= mid + 1;
}
}
return -1;
}
public static void main(String[]
args) {
BinarySearch
mb = new BinarySearch();
int[]
arr = {2, 4, 6, 8, 10, 12, 14, 16};
System.out.println("Key
14's position: "+mb.binarySearch(arr, 14));
int[]
arr1 = {6,34,78,123,432,900};
System.out.println("Key
432's position: "+mb.binarySearch(arr1, 432));
}
}
Labels:
Binary search,
linear search
Call India, Cheapest Call to India and Send Text Message to India form Anywhere: Java-Programing
Call India, Cheapest Call to India and Send Text Message to India form Anywhere: Java-Programing: Java is a pure object orientated Language. Java launched by sun-micro-system but currently buy by Oracle so We can say now, it is part of O...
Sorting programming in java
I am here telling you about Java programming.I am telling here about sorting programming as given below:
Program 1: Bubble Sort
Use your package;
Import Scanner
class BubbleSort {
public static void main(String []args) {
int n, i, j, temp;
Scanner in = new Scanner(System.in);
System.out.println("Input number of integers to Bubble sort");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter array " + n + " integers");
for (i = 0; i < n; i++)
array[i] = in.nextInt();// for array input by user.
for (i = 0; i < ( n - 1 ); i++) {
for (j = 0; j < n - i - 1; j++) {
if (array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
System.out.println("Sorted list of numbers");
for (i = 0; i < n; i++)
System.out.println(array[i]);
}
}
Program 1: Insertion Sort
Use your package;
Import Scanner
Program 3: Selection Sort
Use your package;
Import Scanner
Program 1: Insertion Sort
Use your package;
Import Scanner
public class MyInsertionSort {
public static void main(String[] args) {
int[] arr = {9,8,18,5,96,45,2,74 };
insertionSort(arr);
}
private static void printNumbers(int[] arr) {
for (int i = 0; i < input.length; i++) {
System.out.print(arr[i] + ", ");
}
System.out.println("\n");
}
public static void insertionSort(int array[]) {
int n = array.length;
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = key;
printNumbers(array);
}
}
}
Use your package;
Import Scanner
Selection:
public class SelectionSort {
public static int[] SSelectionSort(int[] arr){
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[index])
index = j;
int temp = arr[index];
arr[index] = arr[i];
arr[i] = temp;
}
return arr;
}
public static void main(String a[]){
int[] arr1 = {12,5,98,48,78,96,6,2};
int[] arr2 = SSelectionSort(arr1);
for(int i:arr2){
System.out.print(i);
System.out.print(", ");
}
}
}
Use your package;
Import Scanner
public class QuickSort {
private int array[];
private int length;
public void sort(int[] inputArr) {
if (inputArr == null || inputArr.length == 0) {
return;
}
this.array = inputArr;
length = inputArr.length;
quickSort(0, length - 1);
}
private void quickSort(int lowerIndex, int higherIndex) {
int i = lowerIndex;
int j = higherIndex;
int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
while (i <= j) {
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
exchangeNumbers(i, j);
//move index to next position on both sides
i++;
j--;
}
}
if (lowerIndex < j)
quickSort(lowerIndex, j);
if (i < higherIndex)
quickSort(i, higherIndex);
}
private void exchangeNumbers(int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void main(String a[]){
QuickSort sorter = new QuickSort();
int[] input = {87,74,47,85,76,5,15,2};
sorter.sort(input);
for(int i:input){
System.out.print(i);
System.out.print(" ");
}
}
}
Program 4: Merg Sort
Use your package;
Import Scanner
public class MergeSort {
private int[] array;
private int[] tempArr;
private int length;
public static void main(String a[]){
int[] inputArr = {3,9,8,2,4,7,78,62};
MergeSort mm = new MergeSort();
mm.sort(inputArr);
for(int i:inputArr){
System.out.print(i);
System.out.print(" ");
}
}
public void sort(int inputArr[]) {
this.array = inputArr;
this.length = inputArr.length;
this.tempArr = new int[length];
doMergeSort(0, length - 1);
}
private void doMergeSort(int lowerIndex, int higherIndex) {
if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
doMergeSort(lowerIndex, middle);
doMergeSort(middle + 1, higherIndex);
mergeParts(lowerIndex, middle, higherIndex);
}
}
private void mergeParts(int lowerIndex, int middle, int higherIndex) {
for (int i = lowerIndex; i <= higherIndex; i++) {
tempArr[i] = array[i];
}
int i = lowerIndex;
int j = middle + 1;
int k = lowerIndex;
while (i <= middle && j <= higherIndex) {
if (tempArr[i] <= tempArr[j]) {
array[k] = tempArr[i];
i++;
} else {
array[k] = tempArr[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = tempArr[i];
k++;
i++;
}
}
}
Labels:
Buble sort,
Insertion sort,
Merge sort,
Quick sort,
Selection sort.
Subscribe to:
Posts (Atom)