It161 Lab-11

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

DATE:

25 - 03 - 2022

IT 161 LAB - 11

ATHAK YADAV
ID: 202151033 SECTION: - 1
 QUESTIONS
Q1:

(a) Study the C program of Selection Sort algorithm given in the following reference and

understand the logic involved in sorting.

https://www.edureka.co/blog/selection-sort-in-c/#:~:text=with%20it%20then%2C-
,Selection%20Sort%20in%20C,smaller%20than%20the%20first%20element.

(b) Run the code with different sets of numbers (including negative numbers and zeros too) and

observe the results.

Q2:

(a) Study the C program of Bubble Sort algorithm given in the following reference and

understand the logic involved in sorting.

https://www.programiz.com/dsa/bubble-sort

(b) Run the code with different sets of numbers (including negative numbers and zeros too) and

observe the results. Also observe how arrays are passed to the functions.

Q3:

Compare the computational complexity of the above two algorithms and make a brief note about

it, to the extent YOU understand it, in your report.


 BUBBLE SORTING
CODE LOGIC:

It is a sorting algorithm where the sorting between the elements is done when the elements are
adjacent to each other. The number swaps with the number adjacent to itself and changes its position,
the condition for this to take place is there must be a difference quantity between the numbers
swapped.
Example:
(5 1 4 2 8) → (1 5 4 2 8), Here, algorithm compares the first two elements, and swaps since 5 > 1.
(1 5 4 2 8) → (1 4 5 2 8), Swap since 5 > 4
(1 4 5 2 8) → (1 4 2 5 8), Swap since 5 > 2
(1 4 2 5 8) → (1 4 2 5 8), Now, since these elements are already in order (8 > 5), algorithm does
not swap them.

The numbers in bold represents the numbers to be swapped.

Complexity:
Worse case: - 0(n2)

Best case: - 0(n)

Average case: - 0(n2)


CODE:

OUTPUT:
here in the above case an array is initialized in int main() which contains the elements to be sorted,

the user has defined a takes something return nothing type function named void bubblesort(int
array[], int size) globally which does all the work of sorting the elements of the array which is
provided in int main().

When the function is called the elements in data[] array which is defined in int main is stored in
array[] which is defined in void bubblesort(int array[], int size) function, same goes with size
variable which is defined both in int main() and bubblesort(int array[], int size) function.

Note : size variable in both int main() and bubblesort(int array[], int size) function is different.

Further when the algorithm completes the result is printed by calling printArray(int Array[], int
size) function which is also a takes something returns nothing type, this function has defined a int
array[] which takes its values from int array[] array which is defined in bubblesort(int array[], int
size) function
Note: both int array[] array which is defined in bubblesort(int array[], int size) function and int
array[] array which is defined in printArray(int Array[], int array[]) are different.

\
 SELECTION SORTING
CODE LOGIC:

Selection sorting algorithm is based on the logic where the first element is set to minimum, we
compare the first set minimum element to other elements of the set, if one finds an element greater
than the set minimum element then the sorting algorithm prevail, where the sorting takes place
between the set minimum element and the found greater element. Vice versa case is also true where
the set element is maximum in legitimate case.
Example:

Here the first element is swapped between the smallest element in comparison with itself, after going
through some iterations.

Here the second element is swapped between the smallest element in comparison with itself, after
going through some iterations.

Here in a case where the set element can’t find the minimum value in comparison to itself it swaps
between itself.

Complexity:
Worse case: - 0(n2)

Best case: - 0(n2)

Average case: - 0(n2)


CODE:

OUTPUT:
 COMPARISION:
COMMON DIFFERENCE BETWEEN SELECTION SORT
AND BUBBLE SORT:

Bubble sorting: This algorithm is based on sorting between the first least or maximum element in
comparison with the set element.

Selection sorting: This algorithm is based on sorting between the maximum or minimum element in
comparison with the set element, this doesn’t sort between the first found maximum or minimum
element

Based on the above observed result we can conclude that selection sort is more efficient compared
to bubble sort.

You might also like