You are on page 1of 5

DATA STRUCTURES

NAME: K. ABINASH
CLASS: CSE-D
REG NO: AUU23EGCSE118

SHORT ANSWER QUESTIONS- 5

1. Compare and contrast the types of sorting techniques.


Ans: Sorting techniques can be broadly classified into two categories:
comparison-based and non-comparison-based. Comparison-based techniques,
such as bubble sort, merge sort, and quicksort, compare elements to determine
their relative order. They have a time complexity of O(n log n) in the average
and worst cases. Non-comparison-based techniques, like counting sort and radix
sort, do not directly compare elements. Instead, they use specific properties of
the data to sort. These techniques can achieve linear time complexity O(n) but
may have limitations, such as requiring specific data characteristics or
additional space.
In summary, comparison-based techniques are versatile but slower, while non-
comparison-based techniques are faster but more specialized.

2. Explain the algorithm for bubble sort.


Ans: Bubble sort is a simple sorting algorithm that repeatedly steps through the
list, compares adjacent elements, and swaps them if they are in the wrong order.
The pass through the list is repeated until the list is sorted.
The algorithm works as follows:
Start from the first element and compare it with the next element.
If the first element is greater than the second element, swap them.
Move to the next pair of elements and repeat step 2.
Continue this process until the end of the list is reached.
Repeat steps 1-4 for each element in the list until no swaps are needed,
indicating that the list is sorted.
Bubble sort has a time complexity of O(n^2) in the worst and average cases,
making it inefficient for large datasets.

3. Write a C-program for sorting integers in ascending order using


insertion sort.
Ans: Here's a simple C program for sorting integers in ascending order using
insertion sort:
#include <stdio.h>

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


int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printf("Sorted array: \n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
This program uses the insertion sort algorithm to sort an array of integers in
ascending order. It iterates through the array, inserting each element into its
correct position in the sorted portion of the array. The time complexity of
insertion sort is O(n^2) in the worst case.

4. Demonstrate the selection sort results for each pass for the following
initial array of elements 21, 6 ,3 ,57 ,13 ,9 ,14 ,18 ,2.

Ans: Selection sort works by repeatedly finding the minimum element from the
unsorted part of the array and swapping it with the first unsorted element. Here's
a step-by-step demonstration of selection sort for the given array {21, 6, 3, 57,
13, 9, 14, 18, 2}:

Pass 1:

Original Array: [21, 6, 3, 57, 13, 9, 14, 18, 2]

After first pass: [2, 6, 3, 57, 13, 9, 14, 18, 21]

Explanation: 2 is the smallest element, so it is swapped with 21.


Pass 2:

After second pass: [2, 3, 6, 57, 13, 9, 14, 18, 21]

Explanation: 3 is the next smallest element, so it is swapped with 6.

Pass 3:

After third pass: [2, 3, 6, 57, 13, 9, 14, 18, 21]

Explanation: 6 is already in the correct position.

Pass 4:

After fourth pass: [2, 3, 6, 9, 13, 57, 14, 18, 21]

Explanation: 9 is the next smallest element, so it is swapped with 57.

Pass 5:

After fifth pass: [2, 3, 6, 9, 13, 57, 14, 18, 21]

Explanation: 13 is the next smallest element, so it is swapped with 57.

Pass 6:

After sixth pass: [2, 3, 6, 9, 13, 14, 57, 18, 21]

Explanation: 14 is the next smallest element, so it is swapped with 57.

Pass 7:

After seventh pass: [2, 3, 6, 9, 13, 14, 18, 57, 21]

Explanation: 18 is the next smallest element, so it is swapped with 57.

Pass 8:

After eighth pass: [2, 3, 6, 9, 13, 14, 18, 21, 57]

Explanation: 21 is the next smallest element, so it is swapped with 57.

After the eighth pass, the array is fully sorted. Selection sort has a time
complexity of O(n^2), where n is the number of elements in the array.
5. Differentiate Quick sort and Merge sort techniques.

Ans: Quick sort and merge sort are both efficient sorting algorithms, but they
differ in their approaches and performance characteristics.

Quick sort is a comparison-based algorithm that uses a divide-and-conquer


strategy. It selects a pivot element and partitions the array into two sub-arrays
such that elements less than the pivot are on the left and elements greater are on
the right. It then recursively sorts the sub-arrays. Quick sort has an average-case
time complexity of O(n log n) and a worst-case complexity of O(n^2) when the
pivot selection is poor.

Merge sort, on the other hand, is also a divide-and-conquer algorithm but


always divides the array into two equal halves. It then recursively sorts the sub-
arrays and merges them back together. Merge sort has a guaranteed time
complexity of O(n log n) in all cases, making it more stable than quick sort, but
it requires additional space for the merging process.

You might also like