0% found this document useful (0 votes)
16 views7 pages

DAA Sort and Search

The document provides detailed explanations of various sorting algorithms (Insertion Sort, Selection Sort, Merge Sort, Quick Sort, Heap Sort, Radix Sort, Bubble Sort) and a searching algorithm (Binary Search), including diagrams, step-by-step workings, and Java code implementations. Each algorithm is structured with its initial state, working process, and a link for interactive visualization. The document concludes by offering to convert the content into a downloadable PDF or to continue with additional topics.

Uploaded by

uday vaidya
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)
16 views7 pages

DAA Sort and Search

The document provides detailed explanations of various sorting algorithms (Insertion Sort, Selection Sort, Merge Sort, Quick Sort, Heap Sort, Radix Sort, Bubble Sort) and a searching algorithm (Binary Search), including diagrams, step-by-step workings, and Java code implementations. Each algorithm is structured with its initial state, working process, and a link for interactive visualization. The document concludes by offering to convert the content into a downloadable PDF or to continue with additional topics.

Uploaded by

uday vaidya
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

Absolutely!

Here's the complete, organized document with each algorithm explained in detail—
including:

• ✅ Diagram
• ✅ How it works (step-by-step)
• ✅ Java Code
• ✅ Interactive Visualization link

📘 Sorting and Searching – Full Notes (with


Step-by-Step Working & Java Code)

🔹 1. Insertion Sort
📈 Diagram:

Initial: [5, 3, 4, 1]
Step 1: [3, 5, 4, 1]
Step 2: [3, 4, 5, 1]
Step 3: [1, 3, 4, 5]

⚙ Working (Steps):

1. Start from index 1 (second element).


2. Compare with previous elements and insert in correct position.
3. Repeat for all elements.

💻 Java Code:

public void insertionSort(int[] arr) {


for (int i = 1; i < arr.length; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}

🔗 Visualize Insertion Sort


🔹 2. Selection Sort
📈 Diagram:

Initial: [4, 2, 5, 1]
Step 1: [1, 2, 5, 4]
Step 2: [1, 2, 5, 4]
Step 3: [1, 2, 4, 5]

⚙ Working:

1. Find minimum element in the unsorted part.


2. Swap with the first unsorted element.
3. Move boundary of sorted part and repeat.

💻 Java Code:

public void selectionSort(int[] arr) {


for (int i = 0; i < arr.length - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIdx]) minIdx = j;
}
int temp = arr[i];
arr[i] = arr[minIdx];
arr[minIdx] = temp;
}
}

🔗 Visualize Selection Sort

🔹 3. Binary Search
📈 Diagram:

Array: [1, 3, 5, 7, 9]
Target: 5
Step 1: mid = 2 (5), Found!

⚙ Working:

1. Find middle index.


2. If mid == target, done.
3. If mid > target, search left half.

2
4. If mid < target, search right half.

💻 Java Code:

public int binarySearch(int[] arr, int target) {


int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] < target) low = mid + 1;
else high = mid - 1;
}
return -1;
}

🔗 Visualize Binary Search

🔹 4. Merge Sort
📈 Diagram:

[4, 1, 3, 2]
→ [4,1] & [3,2]
→ [1,4] & [2,3]
→ [1,2,3,4]

⚙ Working:

1. Divide array into halves.


2. Recursively sort halves.
3. Merge sorted halves.

💻 Java Code:

public void mergeSort(int[] arr, int l, int r) {


if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
private void merge(int[] arr, int l, int m, int r) {
int[] temp = new int[r - l + 1];
int i = l, j = m + 1, k = 0;
while (i <= m && j <= r)
temp[k++] = arr[i] <= arr[j] ? arr[i++] : arr[j++];
while (i <= m) temp[k++] = arr[i++];
while (j <= r) temp[k++] = arr[j++];

3
for (i = 0; i < temp.length; i++) arr[l + i] = temp[i];
}

🔗 Visualize Merge Sort

🔹 5. Quick Sort
📈 Diagram:

[5, 3, 1, 4, 2], pivot = 2


→ [1, 2] + 2 + [3, 4, 5]

⚙ Working:

1. Choose a pivot.
2. Partition array into less than / greater than pivot.
3. Recursively quicksort both parts.

💻 Java Code:

public void quickSort(int[] arr, int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
private int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
}
}
int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp;
return i + 1;
}

🔗 Visualize Quick Sort

🔹 6. Heap Sort
📈 Diagram:

4
Input: [3, 1, 5]
→ Max heap: [5, 1, 3]
→ Sorted: [1, 3, 5]

⚙ Working:

1. Build max heap.


2. Swap root with last, reduce heap size.
3. Heapify and repeat.

💻 Java Code:

public void heapSort(int[] arr) {


int n = arr.length;
for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i);
for (int i = n - 1; i > 0; i--) {
int temp = arr[0]; arr[0] = arr[i]; arr[i] = temp;
heapify(arr, i, 0);
}
}
private void heapify(int[] arr, int n, int i) {
int largest = i, left = 2 * i + 1, right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) largest = left;
if (right < n && arr[right] > arr[largest]) largest = right;
if (largest != i) {
int temp = arr[i]; arr[i] = arr[largest]; arr[largest] = temp;
heapify(arr, n, largest);
}
}

🔗 Visualize Heap Sort

🔹 7. Radix Sort
📈 Diagram:

[170, 45, 75, 90]


Sort by unit → tens → hundreds
→ [45, 75, 90, 170]

⚙ Working:

1. Sort by each digit from least significant to most.


2. Use stable counting sort internally.

💻 Java Code:

public void radixSort(int[] arr) {

5
int max = Arrays.stream(arr).max().getAsInt();
for (int exp = 1; max / exp > 0; exp *= 10)
countingSort(arr, exp);
}
private void countingSort(int[] arr, int exp) {
int[] output = new int[arr.length];
int[] count = new int[10];
for (int i : arr) count[(i / exp) % 10]++;
for (int i = 1; i < 10; i++) count[i] += count[i - 1];
for (int i = arr.length - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
System.arraycopy(output, 0, arr, 0, arr.length);
}

🔗 Visualize Radix Sort

🔹 8. Bubble Sort
📈 Diagram:

[5, 3, 1]
→ [3, 5, 1]
→ [3, 1, 5]
→ [1, 3, 5]

⚙ Working:

1. Compare adjacent elements.


2. Swap if they’re out of order.
3. Repeat until sorted.

💻 Java Code:

public void bubbleSort(int[] arr) {


for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp;
}
}
}
}

🔗 Visualize Bubble Sort

6
Would you like me to turn this into a downloadable PDF, or continue with string matching and
exhaustive search as well?

You might also like