Skip to content

Commit

Permalink
java dsa
Browse files Browse the repository at this point in the history
  • Loading branch information
tejaspundpal committed Dec 30, 2022
0 parents commit 1df2417
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/Java-DSA.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions Searching/BinarySearch.java
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);
}
}
}
57 changes: 57 additions & 0 deletions Searching/BinarySearchRecursion.java
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);
}
}
}
37 changes: 37 additions & 0 deletions Searching/LinearSearch.java
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);
}
}
}
42 changes: 42 additions & 0 deletions Sorting/BubbleSort.java
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);
}
}

0 comments on commit 1df2417

Please sign in to comment.