UNIT-II-Data analysis and algorithm
UNIT-II-Data analysis and algorithm
Quick Sort is a sorting method that works by choosing a "pivot" and dividing the array into two parts:
The process is repeated for each part until the array is fully sorted.
It is called in-place because it doesn’t need extra memory for sorting, but it is unstable, meaning it can
change the order of equal elements.
Example with Array [24, 12, 35, 23, 45, 34, 20, 48]:
2. For the left part [24, 12, 35, 23, 45, 34, 20]:
3. Keep dividing and sorting until fully sorted: [12, 20, 23, 24, 34, 35, 45, 48].
#include <iostream>
int i = low - 1;
for (int j = low; j < high; j++) {
i++;
swap(arr[i], arr[j]);
return i + 1;
quickSort(arr, pi + 1, high);
---
1. Best Case: Pivot divides the array into two nearly equal halves. Time: .
2. Average Case: Similar to the best case. Time: .
3. Worst Case: Pivot is always the smallest or largest element (e.g., sorted/reversed array). Time: .
---
void divideAndConquer(Problem P) {
if (P is small enough) {
solve(P);
} else {
divideAndConquer(P1);
divideAndConquer(P2);
---
Stable Sorting: A sorting method is stable if it keeps the order of equal elements the same.
Merge Sort: It is a stable sorting method because it doesn’t change the order of equal elements.
---
Advantages:
1. Breaks big problems into small ones, making them easier to solve.
Disadvantages:
Question 7: What Are the Differences Between Merge Sort and Quick Sort?
Merge Sort:
1. Stable.
Quick Sort:
1. Unstable.
---
#include <iostream>
if (arr[mid] == key)
else
}
Example:
Key: 34.
Steps:
---
2. Worst Case: The array is divided until only one element is left. Time: .
---
#include <iostream>
int i = 0, j = 0, k = left;
arr[k++] = L[i++];
else
arr[k++] = R[j++];
}
void mergeSort(int arr[], int left, int right) {
---
Question 11: Trace Merge Sort for [24, 12, 35, 23, 45, 34, 20, 48].
---
1. Best/Average/Worst Case: Always splits the array into two halves. Time: .
2. Extra Memory: Needs extra space to hold temporary arrays during merging.
---