0% found this document useful (0 votes)
9 views6 pages

Updated Contents v1 (AutoRecovered)

The document covers sorting algorithms, specifically bubble sort and quick sort. It provides step-by-step examples of how to sort an array using these methods, detailing the processes and time complexities involved. Additionally, it includes comparisons between bubble sort and selection sort.

Uploaded by

jsh
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)
9 views6 pages

Updated Contents v1 (AutoRecovered)

The document covers sorting algorithms, specifically bubble sort and quick sort. It provides step-by-step examples of how to sort an array using these methods, detailing the processes and time complexities involved. Additionally, it includes comparisons between bubble sort and selection sort.

Uploaded by

jsh
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/ 6

Updated Contents

Unit 1
1.Consider functions Function 1 and Function 2 expressed in pseudocode as follows:

Let f1(n)f1(n) and f2(n)f2(n) denote the number of times the statement "x=x+1""x=x+1" is
executed in Function 1 and Function 2, respectively.
Which of the following statements is/are TRUE?

2. Let f f and g g be functions of natural numbers given by f ( n ) = n f(n)=n and g ( n ) = n 2


g(n)=n 2 . Which of the following statements is/are TRUE?
Unit 2
1.Sort the given numbers 89, 48, 68, 90, 29, 34, 17 using bubble sort and calculate the time
complexity
ii) Distinguish selection sort with bubble sort

Sorting using Bubble Sort and Calculating Time Complexity


Bubble Sort Algorithm:
Bubble sort repeatedly swaps adjacent elements if they are in the wrong order. This process is
repeated n−1n-1n−1 times for nnn elements until the array is sorted.
Given Array:
Initial Array: [89,48,68,90,29,34,17]
Sorting Steps:
1. First Pass (Largest element moves to the end):
o Compare 89 and 48: Swap → [48, 89, 68, 90, 29, 34, 17]
o Compare 89 and 68: Swap → [48, 68, 89, 90, 29, 34, 17]
o Compare 89 and 90: No Swap
o Compare 90 and 29: Swap → [48, 68, 89, 29, 90, 34, 17]
o Compare 90 and 34: Swap → [48, 68, 89, 29, 34, 90, 17]
o Compare 90 and 17: Swap → [48, 68, 89, 29, 34, 17, 90]
Array after first pass: [48, 68, 89, 29, 34, 17, 90]
2. Second Pass (Second largest element moves to the second last position):
o Compare 48 and 68: No Swap
o Compare 68 and 89: No Swap
o Compare 89 and 29: Swap → [48, 68, 29, 89, 34, 17, 90]
o Compare 89 and 34: Swap → [48, 68, 29, 34, 89, 17, 90]
o Compare 89 and 17: Swap → [48, 68, 29, 34, 17, 89, 90]
Array after second pass: [48, 68, 29, 34, 17, 89, 90]
3. Third Pass:
o Compare 48 and 68: No Swap
o Compare 68 and 29: Swap → [48, 29, 68, 34, 17, 89, 90]
o Compare 68 and 34: Swap → [48, 29, 34, 68, 17, 89, 90]
o Compare 68 and 17: Swap → [48, 29, 34, 17, 68, 89, 90]
Array after third pass: [48, 29, 34, 17, 68, 89, 90]
4. Fourth Pass:
o Compare 48 and 29: Swap → [29, 48, 34, 17, 68, 89, 90]
o Compare 48 and 34: Swap → [29, 34, 48, 17, 68, 89, 90]
o Compare 48 and 17: Swap → [29, 34, 17, 48, 68, 89, 90]
Array after fourth pass: [29, 34, 17, 48, 68, 89, 90]
5. Fifth Pass:
o Compare 29 and 34: No Swap
o Compare 34 and 17: Swap → [29, 17, 34, 48, 68, 89, 90]
Array after fifth pass: [29, 17, 34, 48, 68, 89, 90]
6. Sixth Pass:
o Compare 29 and 17: Swap → [17, 29, 34, 48, 68, 89, 90]
Array after sixth pass: [17, 29, 34, 48, 68, 89, 90]
7. Seventh Pass:
o No swaps are needed; the array is sorted.
Sorted Array:
[17,29,34,48,68,89,90]

2. Example of Quick Sort


Problem:
Sort the array using Quick Sort:
Array: [10,80,30,90,40,50,70]

Step-by-Step Solution:
Quick Sort Overview:
1. Choose a pivot element.
2. Partition the array so that:
o Elements smaller than the pivot go to the left.
o Elements larger than the pivot go to the right.
3. Recursively apply the above steps to the left and right sub-arrays.

Step 1: Initial Array


[10,80,30,90,40,50,70]
 Choose the last element as the pivot: Pivot=70
 Partitioning:
 Compare each element with the pivot and rearrange:
o 10<70: Stay in place.
o 80>70: Move to the right side.
o 30<7: Move to the left side of the pivot.
o 90>70: Move to the right side.
o 40<70: Move to the left side.
o 50<70: Move to the left side.
Result after Partitioning: [10,30,40,50,70,90,80]
 The pivot 70 is now in its correct position at index 4.

Step 2: Sort Left Sub-array


Left sub-array: [10,30,40,50]
Pivot: 50 (last element of the sub-array).
Partitioning:
 Compare elements with the pivot 505050:
o 10<50: Stay in place.
o 30<50: Stay in place.
o 40<50: Stay in place.
Result after Partitioning: [10,30,40,50]
 The pivot 50 is now in its correct position at index 3.

Step 3: Sort Right Sub-array


Right sub-array: [90,80]
Pivot: 80 (last element of the sub-array).
Partitioning:
 Compare 90>80: Swap.
Result after Partitioning: [80,90]
The pivot 80 is now in its correct position at index 5.

Final Sorted Array:


After sorting all sub-arrays:
[10,30,40,50,70,80,90]

You might also like