0% found this document useful (0 votes)
6 views

DSLExpt 4

Uploaded by

kalpeshpatil1707
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

DSLExpt 4

Uploaded by

kalpeshpatil1707
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Group B

Experiment No 4
Practical Title:
Write a Python program to store first year percentage of students in array. Write function for
Sorting array of floating-point numbers in ascending order using
a) Selection Sort
b) Bubble sort and display top five scores

Aim: To implement Selection Sort and Bubble Sort

Prerequisite: Python Programming.

Objectives: 1) Implement Selection Sort, Bubble sort algorithm in Python programming


2) Create record for student’s percentage.

Input: 1) First year percentage of students in floating point


Outcome: 1) Sorted List of students according to their percentage.
2) List of top five scores.

Theory:
Sorting:
A Sorting Algorithm is used to rearrange a given array or list of elements according to a comparison operator on the
elements. The comparison operator is used to decide the new order of elements in the respective data structure.
Concept of selection sort:
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from
the unsorted part and putting it at the beginning.
The algorithm maintains two subarrays in a given array.
● The subarray which already sorted.
● The remaining subarray was unsorted.

In every iteration of the selection sort, the minimum element (considering ascending order) from the unsorted subarray is
picked and moved to the sorted subarray.
How selection sort works?
Let’s consider the following array as an example: arr[] = {64, 25, 12, 22, 11}
First pass:
● For the first position in the sorted array, the whole array is traversed from index 0 to 4 sequentially. The first position
where 64 is stored presently, after traversing whole array it is clear that 11 is the lowest value.
64 25 12 22 11
● Thus, replace 64 with 11. After one iteration 11, which happens to be the least value in the array, tends to appear in the
first position of the sorted list.
11 25 12 22 64
Second Pass:
● For the second position, where 25 is present, again traverse the rest of the array in a sequential manner.
11 25 12 22 64
● After traversing, we found that 12 is the second lowest value in the array and it should appear at the second place in the
array, thus swap these values.
11 12 25 22 64
Third Pass:
● Now, for third place, where 25 is present again traverse the rest of the array and find the third least value present in the
array.
11 12 25 22 64
● While traversing, 22 came out to be the third least value and it should appear at the third place in the array, thus swap 22
with element present at third position.
11 12 22 25 64
Fourth pass:
● Similarly, for fourth position traverse the rest of the array and find the fourth least element in the array
● As 25 is the 4th lowest value hence, it will place at the fourth position.
11 12 22 25 64
Fifth Pass:
● At last the largest value present in the array automatically get placed at the last position in the array
● The resulted array is the sorted array.
11 12 22 25 64

Algorithm:
● Initialize minimum value (min_idx) to location 0.
● Traverse the array to find the minimum element in the array.
● While traversing if any element smaller than min_idx is found then swap both the values.
● Then, increment min_idx to point to the next element.
● Repeat until the array is sorted.

Concept of Bubble sort:


Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the
wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity is quite high.
How does Bubble Sort Work?
Input: arr[] = {5, 1, 4, 2, 8}
First Pass:
● Bubble sort starts with very first two elements, comparing them to check which one is greater.

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

Second Pass:
● Now, during second iteration it should look like this:

● ( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
● ( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
● ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
● ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

Third Pass:
● Now, the array is already sorted, but our algorithm does not know if it is completed.
● The algorithm needs one whole pass without any swap to know it is sorted.

● ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
● ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
● ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
● ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

Algorithm:
● Run a nested for loop to traverse the input array using two variables i and j, such that 0 ≤ i < n-1 and 0 ≤ j < n-i-1
● If arr[j] is greater than arr[j+1] then swap these adjacent elements, else move on
● Print the sorted array
The Time Complexity of the Bubble Sort Algorithm
● Bubble sort employs two loops: an inner loop and an outer loop.
● The inner loop performs O(n) comparisons deterministically.
Worst Case
● In the worst-case scenario, the outer loop runs O(n) times.
● As a result, the worst-case time complexity of bubble sort is O(n x n) = O(n x n) (n2).

Best Case
● In the best-case scenario, the array is already sorted, but just in case, bubble sort performs O(n)
comparisons.
● As a result, the time complexity of bubble sort in the best-case scenario is O(n).
Average Case
● Bubble sort may require (n/2) passes and O(n) comparisons for each pass in the average case.
● As a result, the average case time complexity of bubble sort is O(n/2 x n) = O(n/2 x n) = O(n/2 x n) = O(n/2
x n) = O (n2).
The Space Complexity of the Bubble Sort Algorithm
● Bubble sort requires only a fixed amount of extra space for the flag, i, and size variables.
● As a result, the space complexity of bubble sort is O. (1).
● It is an in-place sorting algorithm, which modifies the original array's elements to sort the given array.
In this tutorial, you will see some of the benefits and drawbacks of the bubble sort.

Algorithm:
Write algorithm for above program.
Flowchart:

Draw flowchart for above algorithm

Conclusion:

We have successfully implemented selection sort and bubble sort algorithm for sorting students scores and displayed top
five students

You might also like