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

Assignment 3

data structure assignment study work
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)
11 views4 pages

Assignment 3

data structure assignment study work
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
You are on page 1/ 4

ASSIGNMENT 3

Q1. Name two divide and conquer algorithms for sorting.

• Merge Sort

• Merge Sort divides the array into two halves, recursively sorts each half, and then
merges the sorted halves to produce the final sorted array.
• The merging process is where the "conquer" part occurs, as it combines two sorted
subarrays into a single sorted array.
• Time Complexity: O(nlog⁡n)O(n \log n)O(nlogn) for all cases (best, average, and
worst).

• Quick Sort

• Quick Sort selects a "pivot" element, partitions the array so that elements less than the
pivot are on the left and elements greater than the pivot are on the right, and then
recursively sorts the partitions.
• The divide step occurs at each recursive call, where the array is divided into subarrays
around the pivot.
• Time Complexity: O(nlog⁡n)O(n \log n)O(nlogn) on average, but
O(n2)O(n^2)O(n2) in the worst case (typically mitigated by using randomized or
median pivot selection).

Q2. Apply quick sort algorithm to sort the following data. Justify the steps. 42,
29, 74, 11, 65, 58 .

1. initial Array: 42,29,74,11,65,58

First Partition (Pivot = 58)

1. Choose Pivot: 58 (last element)


2. Partitioning: Move elements smaller than 58 to the left of the pivot and elements greater than 58 to the
right.
o Start with i = -1.
o Traverse each element (from left to right):
▪ 42: less than 58 → increment i to 0, swap with itself.
▪ 29: less than 58 → increment i to 1, swap with itself.
▪ 74: greater than 58 → no change.
▪ 11: less than 58 → increment i to 2, swap with 74.
▪ Array becomes: 42,29,11,74,65,58
▪ 65: greater than 58 → no change.
o Finally, swap the pivot (58) with the element at i + 1 (index 3):
▪ Array becomes: 42,29,11,58,65,74
3. Result after Partition: 42,29,11,58,65,74
o Pivot position: 3
o Subarrays to sort: 42,29,11 (left of pivot) and 65,74 (right of pivot)
Q3. Write a ‘C’ program for insertion sort and discuss its efficiency.

#include <stdio.h>

void insertionSort(int arr[], int n) {


for (int i = 1; i < n; 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;
}
}

void printArray(int arr[], int n) {


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int arr[] = {42, 29, 74, 11, 65, 58};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}

Q4. Write an algorithm/C Function to implement Selection Sort Method.

Selection Sort Algorithm

1. Start from the beginning of the array.


2. For each position iii in the array (from 0 to n−2n-2n−2):
o Assume the smallest element is at position iii.
o Search through the rest of the array (from i+1i+1i+1 to n−1n-1n−1) to find the
smallest element.
o If a smaller element is found, update the position of the smallest element.
o Swap the smallest element found with the element at position iii.
3. Repeat until the array is fully sorted.
Q5. Write an algorithm/C Function to implement Bubble Sort Method.

Bubble Sort Algorithm

1. Start from the beginning of the array.


2. For each element at position iii (from 0 to n−2n-2n−2):
o Compare the current element with the next element.
o If the current element is greater than the next, swap them.
3. Repeat this process for each pass over the array until no swaps are needed, indicating
that the array is sorted.

Q6. Write an algorithm/C Function to implement Quick Sort Method.

Quick Sort Algorithm

1. Choose a Pivot: Select an element from the array to be the pivot (commonly the last
element).
2. Partition the Array: Rearrange the elements in the array so that all elements less
than the pivot are to its left and all elements greater are to its right.
3. Recursively Apply Quick Sort: Recursively apply the above steps to the sub-arrays
formed by dividing at the pivot.
4. The process continues until each sub-array has only one element or is empty, resulting
in a sorted array.

Q7. Write an algorithm/C Function to implement Merge Sort Method.

Merge Sort Algorithm

1. Divide: Divide the unsorted list into nnn sublists, each containing one element.
2. Merge: Repeatedly merge sublists to produce new sorted sublists until there is only
one sublist remaining, which is the sorted list.
3. During the merge process, compare elements from two sublists and combine them
into a new sorted list.

Q8. Write down precondition and algorithm of binary search method?

Preconditions of Binary Search Method:

1. The array must be sorted (either in ascending or descending order).


2. The array must be random-access (such as an array in C or Java) since binary search
needs to access the middle element directly.
3. The search value must be comparable to the elements in the array (e.g., numbers,
strings).
4. The array should not be empty; it must contain at least one element.
Binary Search Algorithm:

Binary Search works by repeatedly dividing the search interval in half. If the value of the
search key is less than the item in the middle of the array, the search continues in the left half,
or if the value is greater, it continues in the right half.

Algorithm (Iterative Approach):

1. Input: A sorted array arr[], and a target value target.


2. Output: The index of target if found, otherwise -1 (indicating the element is not present).

You might also like