-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1df2417
Showing
7 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package Java_DSA.Searching; | ||
import java.util.*; | ||
|
||
public class BinarySearch | ||
{ | ||
public static int search(int[] arr, int n, int element){ | ||
int first = 0; | ||
int last = n-1; | ||
|
||
while(first <= last) { | ||
int mid = (first + last)/2; | ||
|
||
if (element < arr[mid]) { | ||
last = mid - 1; | ||
} | ||
else if(element > arr[mid]){ | ||
first = mid + 1; | ||
} | ||
else { | ||
return mid; | ||
} | ||
} | ||
return -1; | ||
} | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
System.out.println("Enter no of elements : "); | ||
int n = sc.nextInt(); | ||
|
||
int[] arr = new int[n]; | ||
System.out.println("Enter the elements : "); | ||
for(int i = 0;i < n;i++){ | ||
arr[i] = sc.nextInt(); | ||
} | ||
System.out.println("Enter the searching element : "); | ||
int searchvalue = sc.nextInt(); | ||
|
||
int result = search(arr,n,searchvalue); | ||
if(result == -1){ | ||
System.out.println("Element is not present."); | ||
} | ||
else{ | ||
System.out.println("Element is present at index " + result); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package Java_DSA.Searching; | ||
|
||
import java.util.*; | ||
|
||
public class BinarySearchRecursion | ||
{ | ||
public static int search(int[] arr,int start,int n, int element){ | ||
int first = start; | ||
int last = n-1; | ||
if(first == last){ | ||
if (arr[first]==element) | ||
{ | ||
return first; | ||
} | ||
} | ||
else if(first < last){ | ||
int mid = (first + last)/2; | ||
if (element < arr[mid]) { | ||
last = mid - 1; | ||
return search(arr,first,mid-1,element); | ||
} | ||
else if(element > arr[mid]){ | ||
first = mid + 1; | ||
return search(arr,mid + 1,last,element); | ||
} | ||
else { | ||
return mid; | ||
} | ||
} | ||
else{ | ||
return -1; | ||
} | ||
return -1; | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
System.out.println("Enter no of elements : "); | ||
int n = sc.nextInt(); | ||
|
||
int[] arr = new int[n]; | ||
System.out.println("Enter the elements : "); | ||
for(int i = 0;i < n;i++){ | ||
arr[i] = sc.nextInt(); | ||
} | ||
System.out.println("Enter the searching element : "); | ||
int searchvalue = sc.nextInt(); | ||
|
||
int result = search(arr,0,n,searchvalue); | ||
if(result == -1){ | ||
System.out.println("Element is not present."); | ||
} | ||
else{ | ||
System.out.println("Element is present at index " + result); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package Java_DSA.Searching; | ||
import java.util.*; | ||
|
||
public class LinearSearch | ||
{ | ||
public static int search(int[] a, int n, int searchValue) | ||
{ | ||
for(int i=0; i<n ; i++) | ||
{ | ||
if( a[i] == searchValue ) | ||
return i; | ||
} | ||
return -1; | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
System.out.println("Enter no of elements : "); | ||
int n = sc.nextInt(); | ||
|
||
int[] arr = new int[n]; | ||
System.out.println("Enter the elements : "); | ||
for(int i = 0;i < n;i++){ | ||
arr[i] = sc.nextInt(); | ||
} | ||
System.out.println("Enter the searching element : "); | ||
int searchvalue = sc.nextInt(); | ||
|
||
int result = search(arr,n,searchvalue); | ||
if(result == -1){ | ||
System.out.println("Element is not present."); | ||
} | ||
else{ | ||
System.out.println("Element is present at index " + result); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package Java_DSA.Sorting; | ||
import java.util.*; | ||
|
||
public class BubbleSort | ||
{ | ||
public static void bubbleSort(int[] arr,int n){ | ||
int temp; | ||
int swap = 0; | ||
for(int i = 0;i < n-1;i++){ | ||
for(int j = i+1;j < n;j++){ | ||
if(arr[j] < arr[i]){ | ||
|
||
temp = arr[j]; | ||
arr[j] = arr[i]; | ||
arr[i] = temp; | ||
swap++; | ||
} | ||
} | ||
} | ||
System.out.println("Swaps : "+swap); | ||
} | ||
public static void print(int[] arr,int n){ | ||
for (int i = 0;i < n;i++){ | ||
System.out.print(arr[i] + " "); | ||
} | ||
System.out.println(); | ||
} | ||
public static void main(String[] args) { | ||
BubbleSort b = new BubbleSort(); | ||
Scanner sc = new Scanner(System.in); | ||
System.out.println("Enter no of elements : "); | ||
int n = sc.nextInt(); | ||
int[] arr = new int[n]; | ||
System.out.println("Enter the elements : "); | ||
for (int i = 0;i < n;i++){ | ||
arr[i] = sc.nextInt(); | ||
} | ||
print(arr,n); | ||
bubbleSort(arr,n); | ||
print(arr,n); | ||
} | ||
} |