You are on page 1of 18

Essay / Assignment Title: Efficient Python Programming

Programme title:

Name:

Year:
CONTENTS

Table of Contents
CONTENTS...................................................................................................................................................2
INTRODUCTION...........................................................................................................................................4
Algorithm: Selection Sort.............................................................................................................................5
Initialization.............................................................................................................................................5
Iteration...................................................................................................................................................5
Completion..............................................................................................................................................6
Performance Analysis of Selection Sort.......................................................................................................7
Time Complexity Analysis........................................................................................................................9
Visualizing Time Complexity....................................................................................................................9
Analysis of Space Complexity..................................................................................................................9
PYTHON CODE.......................................................................................................................................11
Philosophy of Selection Sort......................................................................................................................14
CONCLUDING REMARKS............................................................................................................................15
BIBLIOGRAPHY...........................................................................................................................................16
Reference List............................................................................................................................................16
Journals.................................................................................................................................................16
APPENDIX (if necessary)............................................................................................................................18

2
Statement of compliance with academic ethics and the avoidance of plagiarism

I honestly declare that this dissertation is entirely my own work and none of its part has been copied
from printed or electronic sources, translated from foreign sources and reproduced from essays of other
researchers or students. Wherever I have been based on ideas or other people texts I clearly declare it
through the good use of references following academic ethics.

(In the case that is proved that part of the essay does not constitute an original work, but a copy of an
already published essay or from another source, the student will be expelled permanently from the
postgraduate program).

Name and Surname (Capital letters):

.......................................................................................................................................

Date: ........................./........../.........

3
INTRODUCTION

Selection sort is a rudimentary and uncomplicated sorting algorithm that recreates a pivotal role in
comprehending the foundational notions of sorting algorithms in computer science. It is distinguished by
its plainness, which makes it a flawless starting point for people learning sorting methods. At its core,
selection sort works on the principle of constantly locating the minimum (or maximum) characteristic
from an unsorted piece from the list, and assigning it to the sorted neighborhood. This procedure
persists until the whole list is sorted in clambering or dismounting order. Selection sort's ease of
understanding and commission makes it a priceless tool for introductory programming periods and
stepping gravel for apprehending more complex sorting algorithms.

The primary idea behind selection sort can be compared to arranging a set of cards. Imagine you have a
deck of unsorted playing cards, and your study is to negotiate them in clambering decree by card value.
With selection sort, initiate with an empty "sorted" pile and a full "unsorted" pile. You continually scan
the unsorted pile for the card with the lowest value, remove it from the unsorted pile, and place it in the
sorted pile (Doerr and Qu, 2023). This strategy duplicates until the unsorted pile is barren, resulting in a
fully sorted sundeck of cards.

4
Algorithm: Selection Sort

Initialization

Figure 01: Initialization

(Programmed in OnlineGDB)

1. Initiate with the exclusive list thought as unsorted.


2. Possess a limitation between the sorted and unsorted characteristics. Initially, the sorted feature
is empty (usually initiating from the commencement of this specific list).
“def selection_sort(arr):

n = len(arr) # Length of the input list”

Iteration

Figure 02: Iteration

(Programmed in OnlineGDB)

1. Repeat the stages until the unsorted territory becomes empty


2. Find the most small element within this specific unsorted amount of the inventory (Fitro, 2023).
3. Change the lowest element with the leftmost characteristic in the unsorted portion.

5
“for i in range(n):

# Assume the current element is the minimum

min_index = i

# Iterate through the unsorted part to find the actual minimum

for j in range(i+1, n):

if arr[j] < arr[min_index]:

min_index = j

# Swap the found minimum element with the first element in the unsorted part

arr[i], arr[min_index] = arr[min_index], arr[i]”

Completion

Figure 03: Completion

(Programmed in OnlineGDB)

When the whole list is sorted, the unsorted part evolves senseless, and the sorted piece confines all the
characteristics in clambering order.

“# Example usage:

my_list = [64, 25, 12, 22, 11]

selection_sort(my_list)

print("Sorted array:", my_list)”

In this algorithm, the outer circle (controlled by i) iterates via each list element, regaling it as the
everyday minimum, while the internal loop (controlled by j) tracks for the authentic minimum within the
unsorted piece (Dakhel et al. 2023). Once the minimum is encountered, it is switched with the leftmost
characteristic in this specific unsorted part, gradually extending this specific sorted part and loosening
this specific unsorted component until the whole list is being sorted.

6
Performance Analysis of Selection Sort

In this interpretation comment, we will delve more deeply into the time and space complexity of the
selection sort algorithm, and also supply graphical phrases of its time complexity using graphs and
Python principle representatives for better performance.

Figure 03: CODE for performance analysis part 01

7
(Programmed in OnlineGDB)

Figure 03: CODE for performance analysis part 02

(Programmed in OnlineGDB)

8
Time Complexity Analysis
Selection sort works by constantly encountering the lowest part from this specific unsorted part of this
specific list and trading it with this specific leftmost element in the unsorted portion to break down the
term complexity examination:

● Outer Loop (i): The outer loop recounts through each list element. It has to perform
comparisons and trades for each component.
● Inner Loop (j): The internal loop is nested within the outermost loop. It iterates through the
enduring unsorted components of the list to experience the lowest characteristic. This loop
performs comparisons.

Analysis of the number of needed comparisons, and specific swaps:

● In the first iteration of this specific outer loop, the inner ring makes (n-1) comparisons.
● In this specific second iteration, it makes (n-2) comparisons.
● This mark continues until the last iteration, where it makes 1 comparison.
The total number of comparisons can be expressed as this specific sum of the first (n-1) natural
numbers:

Total no. of Comparisons = (n-1) + (n-2) + ... + 1 = (n * (n-1)) / 2

This gives us a time complexity of O(n^2) since we have a quadratic association between the numeral of
elements (n) and the number of comparisons.

Visualizing Time Complexity


Visualization of the time complexity of selection sort using a graph. compare it with other sorting
algorithms, such as bubble sort and insertion sort, to see how their performance scales with input size
(n).

The graph will show the performance times for selection sort, bubble sort, and insertion sort as the
input size (n) additions (Durrani and Hayan, 2023). Observe that the selection sort's commission time
grows quadratically.

Analysis of Space Complexity


Selection sort is the in-place sorting algorithm, which suggests it do not require extra memory for
sorting. However, of the input size, it operates by swapping components within the given list. Therefore,
its space sophistication is constant, denoted as O(1). This is one of the godsends of selection sort in
representations of memory efficiency.

9
In summary, the selection sort's time complexity is O(n^2), and its space sophistication is O(1). While it is
not the numerous efficient sorting algorithm for enormous datasets, its simplicity and low recollection
usage make it suitable for academic purposes and small lists (Mahmoudinazlou and Kwon, 2023). When
dealing with large datasets, more efficient sorting algorithms like quicksort or mergesort are selected.

10
PYTHON CODE

“import matplotlib.pyplot as plt

import timeit

import random

def selection_sort(arr):

n = len(arr)

for i in range(n):

min_index = i

for j in range(i + 1, n):

if arr[j] < arr[min_index]:

min_index = j

arr[i], arr[min_index] = arr[min_index], arr[i]

def bubble_sort(arr):

n = len(arr)

for i in range(n):

for j in range(0, n-i-1):

if arr[j] > arr[j+1]:

arr[j], arr[j+1] = arr[j+1], arr[j]

def insertion_sort(arr):

n = len(arr)

for i in range(1, n):

11
key = arr[i]

j=i-1

while j >= 0 and key < arr[j]:

arr[j + 1] = arr[j]

j -= 1

arr[j + 1] = key

if __name__ == "__main__":

input_sizes = [100, 500, 1000, 2000, 5000]

selection_sort_times = []

bubble_sort_times = []

insertion_sort_times = []

for size in input_sizes:

arr = [random.randint(1, 10000) for _ in range(size)]

selection_time = timeit.timeit(lambda: selection_sort(arr.copy()), number=10)

bubble_time = timeit.timeit(lambda: bubble_sort(arr.copy()), number=10)

insertion_time = timeit.timeit(lambda: insertion_sort(arr.copy()), number=10)

selection_sort_times.append(selection_time)

bubble_sort_times.append(bubble_time)

insertion_sort_times.append(insertion_time)

plt.plot(input_sizes, selection_sort_times, label='Selection Sort')

plt.plot(input_sizes, bubble_sort_times, label='Bubble Sort')

12
plt.plot(input_sizes, insertion_sort_times, label='Insertion Sort')

plt.xlabel('Input Size (n)')

plt.ylabel('Execution Time (seconds)')

plt.title('Sorting Algorithm Performance')

plt.legend()

plt.grid(True)

plt.show()”

13
Philosophy of Selection Sort

Simplicity and Intuitiveness: The selection sort is renowned for its simplicity and ease of understanding.
Its straight approach makes it an immaculate choice for academic purposes, serving as a foundational
illustration for those who know about sorting algorithms. Its philosophy underlines the importance of
clarity as a means of conveying elemental concepts.

In-Place Sorting: Selection sort functions as an in-place sorting algorithm. This suggests it rearranges the
components within the information list itself, without the starvation for other remembering or data
structures. This in-place sorting ideology is beneficial in scenarios where remembering efficiency is a
consideration, as it underestimates the use of extra resources.

Quadratic Time Complexity: One of the defining features of selection sort is its quadratic time
complexity, expressed as O(n^2), where n defines the number of features in the list. This quadratic
association between the input size and the numeral of operations serves as a reminder that plainness in
algorithm structure can come at the cost of efficiency. Selection sort's ideology highlights this trade-off
and boosts consideration of more efficient algorithms for more considerable datasets.

Stability and Adaptiveness: Selection sort is a durable sorting algorithm, which conveys it preserves the
comparative order of equal characteristics. Additionally, it exhibits adaptiveness, completing more
efficiently when the information list is somewhat sorted. These factors are part of its philosophy,
highlighting its strengths in certain scenarios and broadcasting a deeper acquaintance of algorithmic
behavior.

Illustration of Algorithmic Concepts: The selection sort functions as an outstanding example of total
algorithmic concepts, such as iteration, dependent statements, and nested loops. Its philosophy centers
around operating simple algorithms to lead these core compasses and build a powerful basis in
computer science.

14
CONCLUDING REMARKS

In conclusion, assignment sort's philosophy pivots around clarity, education, in-place sorting, and the
exposition of introductory algorithmic images. While it may not be the numerous efficient sorting
algorithms for large datasets, it plays a practical role in familiarizing and comprehending the principles
of sorting algorithms and algorithm configuration. As learners progress in their proficiency of algorithms,
they often probe more efficient sorting techniques. Selection sort conforms as an implied initiating point
in this journey, upgrading crucial pondering about algorithm section and designing invaluable
applications. Its ideology emphasizes the idea that occasionally, simplicity and clearness are as
substantial as efficiency in algorithmic determination-making.

15
BIBLIOGRAPHY

Reference List

Journals
Dakhel, A.M., Majdinasab, V., Nikanjam, A., Khomh, F., Desmarais, M.C. and Jiang, Z.M.J., 2023. Github
copilot ai pair programmer: Asset or liability?. Journal of Systems and Software, 203, p.111734.

Doerr, B. and Qu, Z., 2023, June. Runtime analysis for the NSGA-II: Provable speed-ups from crossover.
In Proceedings of the AAAI Conference on Artificial Intelligence (Vol. 37, No. 10, pp. 12399-12407).

Durrani, M.O.K. and Hayan, S.A., 2023. Asymptotic Performances of Popular Programming Languages for
Popular Sorting Algorithms. Semiconductor Optoelectronics, 42(1), pp.149-169.

Fitro, A., 2023. Comparison of Efficiency Data Soring Algorithms based on Execution Time. International
Journal of Scientific Research in Computer Science, Engineering and Information Technology, pp.15-21.

Mahmoudinazlou, S. and Kwon, C., 2023. A Hybrid Genetic Algorithm for the min-max Multiple Traveling
Salesman Problem. arXiv preprint arXiv:2307.07120.

16
17
APPENDIX (if necessary)

18

You might also like