0% found this document useful (0 votes)
10 views4 pages

DSA With Java Step by Step Notes

Uploaded by

rohit.d.bhagat01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

DSA With Java Step by Step Notes

Uploaded by

rohit.d.bhagat01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Data Structures and Algorithms (DSA) with Java

Beginner Topics

## 1. Arrays (Beginner Level)

**Problem 1:** Find the second largest element in an array.

**Solution:**

```java

public static int findSecondLargest(int[] arr) {

int largest = Integer.MIN_VALUE, secondLargest = Integer.MIN_VALUE;

for (int num : arr) {

if (num > largest) {

secondLargest = largest;

largest = num;

} else if (num > secondLargest && num < largest) {

secondLargest = num;

return secondLargest;

```

## 2. Strings

**Problem 2:** Check if a string is a palindrome.

**Solution:**

```java
public static boolean isPalindrome(String str) {

int left = 0, right = str.length() - 1;

while (left < right) {

if (str.charAt(left) != str.charAt(right)) return false;

left++;

right--;

return true;

```

Intermediate Topics

## 3. Sorting Algorithms

**Problem:** Implement Merge Sort.

**Solution:**

```java

public static void mergeSort(int[] arr, int left, int right) {

if (left < right) {

int mid = left + (right - left) / 2;

mergeSort(arr, left, mid);

mergeSort(arr, mid + 1, right);

merge(arr, left, mid, right);

private static void merge(int[] arr, int left, int mid, int right) {
int n1 = mid - left + 1, n2 = right - mid;

int[] L = new int[n1], R = new int[n2];

for (int i = 0; i < n1; i++) L[i] = arr[left + i];

for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j];

int i = 0, j = 0, k = left;

while (i < n1 && j < n2) {

if (L[i] <= R[j]) arr[k++] = L[i++];

else arr[k++] = R[j++];

while (i < n1) arr[k++] = L[i++];

while (j < n2) arr[k++] = R[j++];

```

Advanced Topics

## 4. Graph Algorithms

**Problem:** Find the shortest path in a graph using Dijkstra's Algorithm.

**Solution:**

```java

public static void dijkstra(int[][] graph, int src) {

int V = graph.length;

int[] dist = new int[V];

boolean[] visited = new boolean[V];

Arrays.fill(dist, Integer.MAX_VALUE);
dist[src] = 0;

for (int count = 0; count < V - 1; count++) {

int u = findMinDistance(dist, visited);

visited[u] = true;

for (int v = 0; v < V; v++) {

if (!visited[v] && graph[u][v] != 0 && dist[u] != Integer.MAX_VALUE

&& dist[u] + graph[u][v] < dist[v]) {

dist[v] = dist[u] + graph[u][v];

printSolution(dist);

private static int findMinDistance(int[] dist, boolean[] visited) {

int min = Integer.MAX_VALUE, minIndex = -1;

for (int v = 0; v < dist.length; v++) {

if (!visited[v] && dist[v] < min) {

min = dist[v];

minIndex = v;

return minIndex;

```

You might also like