Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6909232

Browse files
implementation of Jump seaching , Binary seach + Linear search using array.
1 parent 01a49d7 commit 6909232

File tree

3 files changed

+336
-0
lines changed

3 files changed

+336
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package com.udemy.dsapart1.searching;
2+
import java.util.Arrays;
3+
import java.util.Scanner;
4+
5+
/**
6+
* NOTE : This search approach required sorted input array data sets.
7+
*
8+
*/
9+
public class BinarySearching {
10+
11+
public static void main(String[] args) {
12+
Scanner scInp=new Scanner(System.in);
13+
System.out.print("\nEnter the size of Array : ");
14+
int sizeOfArr = scInp.nextInt();
15+
double[] inputArr = new double[sizeOfArr];
16+
System.out.println("\nEnter the Array elements : ");
17+
pushElementsIntoArray(inputArr);
18+
System.out.println("\nInput Array : " + Arrays.toString(inputArr));
19+
performMergeSort(inputArr);
20+
System.out.println("\nRequired Merge Sorted array : "+Arrays.toString(inputArr));
21+
System.out.println("\n**************** Performing Binary seaching ********************");
22+
System.out.print("\nEnter the element to be searched : ");
23+
double targetItem = scInp.nextDouble();
24+
int resultIndex=performBinarySearchingOnSortedArray(inputArr,targetItem);
25+
System.out.println("\nTarget element found at index : "+resultIndex);
26+
System.out.println("\n**************** Performing Recursive Binary seaching ********************");
27+
System.out.print("\nEnter the element to be searched : ");
28+
double target2Item = scInp.nextDouble();
29+
int result2Index=recursiveBinarySearching(inputArr,targetItem,0,inputArr.length-1);
30+
System.out.println("\nTarget element found at index : "+result2Index);
31+
}
32+
33+
34+
/**
35+
* This algorithms time complexity - O(logN)
36+
* @param inputArr - double[]
37+
* @param targetItem - double
38+
* @return resultIndex - int
39+
*
40+
*/
41+
private static int performBinarySearchingOnSortedArray(double[] inputArr, double targetItem) {
42+
int resultIndex=-1;
43+
int startIndex=0;
44+
int endIndex=inputArr.length-1;
45+
int midIndex;
46+
while (startIndex<=endIndex) {
47+
midIndex=(startIndex+endIndex/2);
48+
if(inputArr[midIndex]==targetItem) {
49+
return midIndex;
50+
}
51+
if (inputArr[midIndex]<targetItem) {
52+
return startIndex=midIndex+1;
53+
}
54+
else {
55+
return endIndex=midIndex-1;
56+
}
57+
}
58+
return resultIndex;
59+
}
60+
61+
62+
/**
63+
* This algorithms time complexity - O(logN)
64+
* @param inputArr - double[]
65+
* @param targetItem - double
66+
* @return resultIndex - int
67+
*
68+
*/
69+
private static int recursiveBinarySearching(double[] inputArr, double targetItem,int startIndex,int endIndex) {
70+
int resultIndex=-1;
71+
int midIndex;
72+
if(endIndex<startIndex)
73+
{
74+
return resultIndex;
75+
}
76+
midIndex=(startIndex+endIndex/2);
77+
if(inputArr[midIndex]==targetItem) {
78+
return midIndex;
79+
}
80+
if (inputArr[midIndex]<targetItem) {
81+
startIndex=midIndex+1;
82+
return recursiveBinarySearching(inputArr,targetItem,startIndex,endIndex);
83+
}
84+
else {
85+
endIndex=midIndex-1;
86+
return recursiveBinarySearching(inputArr,targetItem,startIndex,endIndex);
87+
}
88+
}
89+
90+
91+
/**
92+
* Method to perform the Merge Sort algorithm using divide & Conquer approach.
93+
*
94+
* @param inputArr2
95+
* Time complexity - n*log n.
96+
*
97+
*/
98+
private static void performMergeSort(double[] inputArr2) {
99+
if (inputArr2.length < 2) {
100+
return;
101+
}
102+
int midIndex = (inputArr2.length / 2);
103+
double leftSubArray[] = new double[midIndex];
104+
double rightSubArray[] = new double[inputArr2.length - midIndex];
105+
// fill the elements to left sub-array
106+
for (int i = 0; i < midIndex; i++) {
107+
leftSubArray[i] = inputArr2[i];
108+
}
109+
// fill the elements to right sub-array
110+
for (int i = midIndex; i < inputArr2.length; i++) {
111+
rightSubArray[i-midIndex] = inputArr2[i];
112+
}
113+
//recursive call on sorted left-sub array & right sub-array.
114+
performMergeSort(leftSubArray);
115+
performMergeSort(rightSubArray);
116+
//Pass sorted sub-arrays
117+
doSubArrayMergeOperation(leftSubArray,rightSubArray,inputArr2);
118+
}
119+
120+
121+
/**
122+
*
123+
* This method will perform merge operation on input sorted sub-array
124+
* @param leftSubArray
125+
* @param rightSubArray
126+
* @param inputArr2
127+
*
128+
*/
129+
private static void doSubArrayMergeOperation(double[] leftSubArray, double[] rightSubArray, double[] inputArr2) {
130+
int i = 0, j = 0, k = 0;
131+
while (i < leftSubArray.length && j < rightSubArray.length) {
132+
if (leftSubArray[i] <= rightSubArray[j]) {
133+
inputArr2[k++] = leftSubArray[i++];
134+
}
135+
else {
136+
inputArr2[k++] = rightSubArray[j++];
137+
}
138+
}
139+
while (i < leftSubArray.length) {
140+
inputArr2[k++] = leftSubArray[i++];
141+
}
142+
while (j < rightSubArray.length) {
143+
inputArr2[k++] = rightSubArray[j++];
144+
}
145+
}
146+
147+
148+
public static void pushElementsIntoArray(double inputArr[]) {
149+
Scanner scInp1Obj = new Scanner(System.in);
150+
for (int i = 0; i < inputArr.length; i++) {
151+
System.out.print("\nEnter the element No " + (i + 1) + " : ");
152+
inputArr[i] = scInp1Obj.nextDouble();
153+
}
154+
}
155+
156+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package com.udemy.dsapart1.searching;
2+
import java.util.Arrays;
3+
import java.util.Scanner;
4+
5+
/**
6+
* NOTE : This search approach required sorted input array data sets.
7+
*
8+
*/
9+
public class JumpSearching {
10+
11+
public static void main(String[] args) {
12+
Scanner scInp=new Scanner(System.in);
13+
System.out.print("\nEnter the size of Array : ");
14+
int sizeOfArr = scInp.nextInt();
15+
double[] inputArr = new double[sizeOfArr];
16+
System.out.println("\nEnter the Array elements : ");
17+
pushElementsIntoArray(inputArr);
18+
System.out.println("\nInput Array : " + Arrays.toString(inputArr));
19+
performMergeSort(inputArr);
20+
System.out.println("\nRequired Merge Sorted array : "+Arrays.toString(inputArr));
21+
System.out.println("\n**************** Performing Jump seaching ********************");
22+
System.out.print("\nEnter the element to be searched : ");
23+
double targetItem = scInp.nextDouble();
24+
int resultIndex=performJumpSearchingOnSortedArray(inputArr,targetItem);
25+
System.out.println("\nTarget element found at index : "+resultIndex);
26+
}
27+
28+
29+
/**
30+
* This algorithms time complexity - O(sqrt(N))
31+
* @param inputArr - double[]
32+
* @param targetItem - double
33+
* @return resultIndex - int
34+
*
35+
*/
36+
private static int performJumpSearchingOnSortedArray(double[] inputArr, double targetItem) {
37+
int resultIndex=-1;
38+
int startIndex=0;
39+
int arraySize=inputArr.length;
40+
int blockSize=(int) Math.sqrt(arraySize);
41+
System.out.println("\nBlock Size of Array : "+blockSize);
42+
int endIndex=blockSize-1;
43+
while (inputArr[endIndex]<targetItem && startIndex<arraySize) {
44+
startIndex=endIndex+1;
45+
endIndex=endIndex+blockSize;
46+
if(endIndex>arraySize-1)
47+
{
48+
endIndex=arraySize-1;
49+
}
50+
}
51+
//Performing linear search
52+
for (int i = startIndex; i <= endIndex; i++) {
53+
if (inputArr[i] == targetItem) {
54+
resultIndex = i;
55+
}
56+
}
57+
return resultIndex;
58+
}
59+
60+
61+
/**
62+
* Method to perform the Merge Sort algorithm using divide & Conquer approach.
63+
*
64+
* @param inputArr2
65+
* Time complexity - n*log n.
66+
*
67+
*/
68+
private static void performMergeSort(double[] inputArr2) {
69+
if (inputArr2.length < 2) {
70+
return;
71+
}
72+
int midIndex = (inputArr2.length / 2);
73+
double leftSubArray[] = new double[midIndex];
74+
double rightSubArray[] = new double[inputArr2.length - midIndex];
75+
// fill the elements to left sub-array
76+
for (int i = 0; i < midIndex; i++) {
77+
leftSubArray[i] = inputArr2[i];
78+
}
79+
// fill the elements to right sub-array
80+
for (int i = midIndex; i < inputArr2.length; i++) {
81+
rightSubArray[i-midIndex] = inputArr2[i];
82+
}
83+
//recursive call on sorted left-sub array & right sub-array.
84+
performMergeSort(leftSubArray);
85+
performMergeSort(rightSubArray);
86+
//Pass sorted sub-arrays
87+
doSubArrayMergeOperation(leftSubArray,rightSubArray,inputArr2);
88+
}
89+
90+
91+
/**
92+
*
93+
* This method will perform merge operation on input sorted sub-array
94+
* @param leftSubArray
95+
* @param rightSubArray
96+
* @param inputArr2
97+
*
98+
*/
99+
private static void doSubArrayMergeOperation(double[] leftSubArray, double[] rightSubArray, double[] inputArr2) {
100+
int i = 0, j = 0, k = 0;
101+
while (i < leftSubArray.length && j < rightSubArray.length) {
102+
if (leftSubArray[i] <= rightSubArray[j]) {
103+
inputArr2[k++] = leftSubArray[i++];
104+
}
105+
else {
106+
inputArr2[k++] = rightSubArray[j++];
107+
}
108+
}
109+
while (i < leftSubArray.length) {
110+
inputArr2[k++] = leftSubArray[i++];
111+
}
112+
while (j < rightSubArray.length) {
113+
inputArr2[k++] = rightSubArray[j++];
114+
}
115+
}
116+
117+
118+
public static void pushElementsIntoArray(double inputArr[]) {
119+
Scanner scInp1Obj = new Scanner(System.in);
120+
for (int i = 0; i < inputArr.length; i++) {
121+
System.out.print("\nEnter the element No " + (i + 1) + " : ");
122+
inputArr[i] = scInp1Obj.nextDouble();
123+
}
124+
}
125+
126+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.udemy.dsapart1.searching;
2+
import java.util.Arrays;
3+
import java.util.Scanner;
4+
5+
/**
6+
* NOTE : This search approach don't required sorted input array data sets.
7+
*
8+
*/
9+
public class LinearSearching {
10+
11+
public static void main(String[] args) {
12+
Scanner scInp=new Scanner(System.in);
13+
System.out.print("\nEnter the size of Array : ");
14+
int sizeOfArr = scInp.nextInt();
15+
double[] inputArr = new double[sizeOfArr];
16+
System.out.println("\nEnter the Array elements : ");
17+
pushElementsIntoArray(inputArr);
18+
System.out.println("\nInput Array : " + Arrays.toString(inputArr));
19+
System.out.println("\n**************** Performing Linear seaching ********************");
20+
System.out.print("\nEnter the element to be searched : ");
21+
double targetItem = scInp.nextDouble();
22+
int resultIndex=performLinearSearching(inputArr,targetItem);
23+
System.out.println("\nTarget element found at index : "+resultIndex);
24+
}
25+
26+
27+
/**
28+
* This algorithm has linear time complexity - O(N)
29+
* @param inputArr - double[]
30+
* @param targetItem - double
31+
* @return resultIndex - int
32+
*
33+
*/
34+
private static int performLinearSearching(double[] inputArr, double targetItem) {
35+
int resultIndex = -1;
36+
for (int i = 0; i < inputArr.length; i++) {
37+
if (inputArr[i] == targetItem) {
38+
return resultIndex = i;
39+
}
40+
}
41+
return resultIndex;
42+
}
43+
44+
45+
46+
public static void pushElementsIntoArray(double inputArr[]) {
47+
Scanner scInp1Obj = new Scanner(System.in);
48+
for (int i = 0; i < inputArr.length; i++) {
49+
System.out.print("\nEnter the element No " + (i + 1) + " : ");
50+
inputArr[i] = scInp1Obj.nextDouble();
51+
}
52+
}
53+
54+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /