0% found this document useful (0 votes)
8 views11 pages

UNIT-II-Data analysis and algorithm

Includes merge sorting and quick sorting and other relating questions

Uploaded by

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

UNIT-II-Data analysis and algorithm

Includes merge sorting and quick sorting and other relating questions

Uploaded by

Saira adress
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIT-II

Question 1: What is Quick Sort, and How Does It Work?

Quick Sort is a sorting method that works by choosing a "pivot" and dividing the array into two parts:

One part has elements smaller than the pivot.

The other part has elements larger than the pivot.

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]:

1. Pick pivot = 48. Partition:

[24, 12, 35, 23, 45, 34, 20 | 48].

Pivot is already in the right place.

2. For the left part [24, 12, 35, 23, 45, 34, 20]:

Pick pivot = 20. Partition:

[12, 20 | 35, 23, 45, 34, 24].

3. Keep dividing and sorting until fully sorted: [12, 20, 23, 24, 34, 35, 45, 48].

Question 2: Write a Recursive Quick Sort Algorithm in C++.

#include <iostream>

using namespace std;

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++;

swap(arr[i], arr[j]);

swap(arr[i + 1], arr[high]);

return i + 1;

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);

---

Question 3: What is the Time Complexity of Quick Sort?

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: .

---

Question 4: What is the Divide-and-Conquer Approach?

Divide-and-Conquer is a problem-solving method where:

1. Divide: Break the problem into smaller parts.

2. Conquer: Solve each smaller part individually.

3. Combine: Merge the solutions to solve the original problem.


Example in C++:

void divideAndConquer(Problem P) {

if (P is small enough) {

solve(P);

} else {

divide P into subproblems P1, P2, ...;

divideAndConquer(P1);

divideAndConquer(P2);

combine results of P1, P2, ...;

---

Question 5: What is Stable Sorting, and Is Merge Sort Stable?

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.
---

Question 6: What Are the Advantages and Disadvantages of Divide-and-Conquer?

Advantages:

1. Breaks big problems into small ones, making them easier to solve.

2. Allows parallel processing.

3. Efficient for large datasets.

Disadvantages:

1. Requires extra memory for some methods (e.g., Merge Sort).

2. Recursive calls can slow down performance due to overhead.


---

Question 7: What Are the Differences Between Merge Sort and Quick Sort?

Merge Sort:

1. Stable.

2. Needs extra memory for merging.

3. Time complexity is always .

Quick Sort:

1. Unstable.

2. No extra memory needed.


3. Can degrade to in the worst case.

---

Question 8: Write a Recursive Binary Search Algorithm in C++.

#include <iostream>

using namespace std;

int binarySearch(int arr[], int low, int high, int key) {

if (low > high)

return -1; // Key not found

int mid = (low + high) / 2;

if (arr[mid] == key)

return mid; // Key found

else if (arr[mid] > key)

return binarySearch(arr, low, mid - 1, key); // Search in left part

else

return binarySearch(arr, mid + 1, high, key); // Search in right part

}
Example:

Array: [12, 23, 34, 45, 56].

Key: 34.

Steps:

Mid = 2, Key matches at index 2.

---

Question 9: What is the Time Complexity of Binary Search?

1. Best Case: The key is found immediately. Time: .

2. Worst Case: The array is divided until only one element is left. Time: .
---

Question 10: Write a Merge Sort Algorithm in C++.

#include <iostream>

using namespace std;

void merge(int arr[], int left, int mid, int right) {

int n1 = mid - left + 1;

int n2 = right - mid;

int L[n1], R[n2];

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

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

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++];

}
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);

---

Question 11: Trace Merge Sort for [24, 12, 35, 23, 45, 34, 20, 48].

1. Split into smaller arrays:

[24, 12, 35, 23] and [45, 34, 20, 48].

Further split: [24, 12], [35, 23], etc.

2. Merge back while sorting:

[12, 24], [23, 35], [20, 34], [45, 48].


3. Final merge: [12, 20, 23, 24, 34, 35, 45, 48].

---

Question 12: What is the Time Complexity of Merge Sort?

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.

---

You might also like