Trabalho FIO

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

SORTING ALGORITHMS

Mariana Primor, 260631


Prof. Miloš Šeda.
2023

1
Index
1. Introduction to Sorting Algorithms………………………………………
3
2. Comparison-based Algorithms………………………………………..…3
2.1. Bubble Sort…….…………………………………………………….4
2.2. Insertion Sort…………………………………………………………
5
2.3. Selection
Sort………………………………………………………...6
2.4. Merge Sort…………………………………………………………...7
2.5. Quick Sort……………………………………………………………7
2.6. Heap Sort…………………………………………………………….8
3. Non-Comparison-based Algorithms……………………………………..9
3.1. Counting
Sort………………………………………………………...9
3.2. Radix Sort…………………………………………………………..10
4. Real World Applications………………………………………………..11
4.1. Databases…………………………………………………………...11
4.2. E-commerce………………………………………………………...11
4.3. Telecommunications…………………………………………….….11
4.4. GPS and Mapping Services…………………………………….…..12
5. Future Developments……………………………………………….….12
6.Conclusion………………………………………………………….…..12
7.Bibliography……………………………………………………………13

Illustration Index
Figure 1. Bubble Sort Illustration…………………………………………..4
Figure 2. Insertion Sort
Illustration………………………………………....5
Figure 3. Selection Sort Illustration…………………………………………
6

2
Figure 4. Merge Sort Illustration……………………………………...
…….7
Figure 5. Quick Sort Illustration……………………………………..……..8
Figure 6. Counting Sort Illustration………………………………...………
9
Figure7.Radix Sort Illustration……………………………………………10

1. Introduction to Sorting Algorithms


When we talk about computer science and data processing, few operations
are as fundamental as sorting. Whether we are arranging a list of names in a
specific order or optimizing data retrieval, between many other operations,
the choice of an algorithm plays a central role.

Sorting algorithms are, basically, a set of instructions that take an array or


list as an input and arrange the items into a particular order.

Some common sorting algorithms are Bubble Sort, Insertion Sort, Selection
Sort, Merge Sort, etc. and all of them will be explored throughout the
presentation, with the mention of best-case and worst-case scenarios and
practical considerations that influence the selection of a particular
algorithm for a given task.

Sorting algorithms can be categorized based on their underlying principles,


which are Comparison-based algorithms and non-comparison-based
algorithms. They also are very distinguished when it comes to complexity
and efficiency, which will be also explored in this text.

This document will serve as a comprehensive tool to this approach to data


organizing.

2. Comparison-based Algorithms

3
Comparison-based algorithms are a class of sorting algorithms that rely on
pairwise comparisons between elements to determine their relative order.

These algorithms compare elements and use the results of these


comparisons to rearrange them into the desired order.

Types of such algorithms are:

2.1. Bubble sort


This is a simple sorting algorithm that repeatedly steps through a list,
compares the elements, and swaps them if they are in the wrong order. This
process is repeated until the list is fully sorted. It gets the name Bubble Sort
because smaller elements “bubble” to the top of the list.

This type of algorithm is not very efficient and is mainly used for
educational purposes or for small datasets.

Figure1. Bubble sort Illustration

An easy code example for this algorithm in python could be:

def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range (0, n - i - 1):

4
if arr[j] > arr [j + 1]:
arr[j], arr [j + 1] = arr[j + 1], arr[j]

2.2. Insertion sort


Insertion sort is a simple algorithm where the final sorted array is built one
item at a time by repeatedly taking elements from the unsorted part and
inserting them into their correct position in the sorted part.

This is an efficient method if the datasets are small or partially sorted and
has the advantage that it does not require additional memory for the sorting.

Figure 2. Insertion Sort Illustration

Example for this python code is:

def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j=i-1

5
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key

2.3. Selection sort


This algorithm works by dividing the input list into two parts, a sorted and
an unsorted region. The smallest element from the unsorted region is
repeatedly selected and swapped with the first element of the unsorted
region. The sorted region grows, and the unsorted shrinks until the desired
list is completed. It is obvious that this can work the same way if the
desired order implicates that the algorithm starts by selecting the largest
element.

As well as the previous examples, this is usually not for performance-


critical applications but is often used for educational purposes due to its
simplicity.

6
Figure 3. Selection sort Illustration

Example for this python code is:

def selection_sort(arr):
n = len(arr)
for i in range(n - 1):
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]

2.4. Merge sort


The merge sort is an algorithm that divides an unsorted list into n sub-lists,
each one containing one element, and then repeatedly merges sub-lists to
produce new sorted sub-lists until there is only on sub-list left remaining,
which is the sorted, completed list.

7
From all of the algorithms presented up until now, it is the most efficient
one because it is used in large datasets. It does, however, require additional
space for the merging process, so it is not an in-place sorting method.

Figure 4. Merge sort Illustration

2.5. Quick sort


Quick sort is another widely used algorithm that works by firstly selecting
a pivot element from the array and partitioning the other elements into two
sub-arrays according to whether they are smaller or greater than the pivot.
The sub-arrays are then sorted.

With good pivot selection strategies and optimizations, Quick sort is often
faster in practice than others, making it a popular choice for sorting.

8
Figure 5. Quick sort Illustration

2.6. Heap sort


This method consists of using a heap data structure to build a
max-heap/min-heap from the elements and then repeatedly extract the
maximum/minimum element, rebuilding the heap after each extraction.

Basically, in simple terms, we make a heap where the biggest element is


constantly on top. Then, we put that element in our sorted list and make
sure that the heap is still following the max-heap order. This process is
repeated until the heap is empty, and our sorted list is completed.

It does not need additional memory for sorting, although it is not very used
in practical performance on modern computers.

3. Non-comparison-based Algorithm
Non-comparison-based algorithms are sorting algorithms that do not rely
on comparing individual elements to determine their order. Instead, these

9
algorithms exploit other characteristics of data, such as counting
occurrences or considering the structure of the elements.

3.1. Counting sort


The basic idea is counting the number of occurrences of each element in
the input list, after that process is finished, it calculates the cumulative
count of elements. Finally, the last step is to place elements in the sorted
order.

This algorithm works well when the range of input values is reasonably
small, and it preserves the relative order of equal elements. It’s not suitable
for large input values because it requires additional space proportional to
the range.

Figure 6. Counting Sort Illustration

3.2. Radix Sort


The last sorting algorithm to be described is the Radix sort, which works by
distributing elements into buckets based on their individual digits or

10
positions. It processes the input from the least significant digit (LSD) to the
most significant or vice-versa, placing the elements into buckets based on
the value of the current digit.

What this means is that there is a determination of the maximum number of


digits in the input list, then pass through the digits and start from the LSD
or MSD, placing them into buckets based on the values and concatenate the
buckets to create a new list. Finally, there is the repetition of the process.

Figure 7. Radix Sort Illustration

4. Real World Applications


Sorting algorithms have numerous applications across various domains due
to their fundamental role in organizing and retrieving data efficiently.

11
Each field in which we apply these algorithms can find advantages and
disadvantages, therefore it is hard to precisely mention which algorithm is
ideal for a certain application.

Some of those real world applications are:

4.1. Databases
Sorting is crucial for database systems to efficiently organize records based
on different preferred fields.

An example of this can be the records of patients from a hospital, sorting


employees by name or salary in a company, etc.

4.2. E-commerce
Online shopping platforms use sorting algorithms to display products in a
user-friendly manner, for example when we filter our search according to
price, popularity, customer satisfaction, between many others.

This will help both clients and the business.

4.3. Telecommunications
Call routing systems use sorting to manage and prioritize incoming calls
based on factors such as caller priority, locations, urgency, etc.

4.4. GPS and Mapping Services


Routing services utilize these sorting services to optimize routes and
display them in a user-friendly manner.

12
Many other aspects of our lives are ruled by sorting algorithms and without
these the world would not function the way that it does.

5. Future Developments
Discussing future developments in this area involves considering
advancements in technology, computing architectures, and the evolving
nature of data. However, there are some points to be mentioned about the
future of sorting algorithms.

 Machine learning: Integration of machine learning techniques for


adaptive sorting algorithms can be done in the future. These
algorithms could learn patterns in data and dynamically adjust their
strategies.
 Energy-efficient sorting: With a growing emphasis on energy
efficiency, sorting algorithms may be developed to minimize energy
consumption, contributing to the world’s cause of sustainability.

6. Conclusion
This work has described a vast variety of sorting algorithms, both
traditional comparison-based and non-comparison, giving insights about
each method and its purposes.

Comparison-based algorithms, including Bubble Sort, Insertion Sort,


Selection Sort, Merge Sort, Quick Sort, and Heap Sort, navigate the
complexity of data through pairwise comparisons. Python code snippets
were introduced into the work for a small demonstration of how these
algorithms are developed.

The pragmatic applications of non-comparison-based algorithms,


exemplified by Counting Sort and Radix Sort, offer an alternative

13
paradigm. These algorithms go beyond mere comparisons, demonstrating
efficiency in scenarios where traditional methods may prove less effective.

In conclusion, the significance of sorting algorithms lies in their


fundamental role within the digital landscape. As we navigate the ever-
technological terrain, the methodologies explored here continue to shape
the systematic functioning of systems, adding order to the complexity of
data that most of us cannot comprehend.

These methods have ended up being powerful engineering tools and the
journey through the exploration of soring algorithms must not come to an
end, because the more we dive and discover their intricacies , the more our
world will evolve in unimaginable manners.

7. Bibliography
1.https://www.freecodecamp.org/news/sorting-algorithms-explained-with-
2.examples-in-python-java-and-c/

3.https://en.wikipedia.org/wiki/Sorting_algorithm

4.https://www.geeksforgeeks.org/sorting-algorithms/

5.https://m.youtube.com/watch?v=l7-
f9gS8VOs&pp=ygUWI3NvcnRhbGdvcml0aG1zc29ydGluZw%3D%3D

6.Help of chatGPT online tool.

14
15

You might also like