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

Week 8

This document discusses sorting algorithms and their analysis in Python. It introduces common sorting algorithms like merge sort, quicksort, insertion sort, selection sort, and bubble sort. It explains why sorting is important for optimizing data searching and readability. Key steps of merge sort are outlined, including dividing, sorting, and merging subsections. Finally, it discusses Python's built-in sorted() function and Timsort algorithm.

Uploaded by

Jiten Oman
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Week 8

This document discusses sorting algorithms and their analysis in Python. It introduces common sorting algorithms like merge sort, quicksort, insertion sort, selection sort, and bubble sort. It explains why sorting is important for optimizing data searching and readability. Key steps of merge sort are outlined, including dividing, sorting, and merging subsections. Finally, it discusses Python's built-in sorted() function and Timsort algorithm.

Uploaded by

Jiten Oman
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Data Analytics Systems & Algorithms

COMP 20036
Week 8
Sorting and algorithm analysis

Introduction to Sorting Analysis


• MergeSort
• Sorted() Function
• QuickSort
Python - Sorting Algorithms

Why??

Sorting refers to arranging data in a particular format.


Sorting algorithm specifies the way to arrange data in a particular order.
Most common orders are in numerical or lexicographical order.

The importance of sorting lies in the fact that data searching can be
optimized to a very high level, if data is stored in a sorted manner.
Sorting is also used to represent data in more readable formats.
Sorting, searching and algorithm analysis
• The sorting of a list of values is a common computational task which has
been studied extensively. The classic description of the task is as follows:
• Given a list of values and a function that compares two values, order the
values in the list from smallest to largest.
• The values might be integers, or strings or even other kinds of objects. We
will examine two algorithms:
• Selection sort, which relies on repeated selection of the next smallest item
• Merge sort, which relies on repeated merging of sections of the list that are
already sorted
Some implementations of sorting in python.
• Bubble Sort
• Merge Sort
• Insertion Sort
• Selection Sort
Bubble Sort
It is a comparison-based algorithm in
which each pair of adjacent elements is
compared and the elements are swapped
if they are not in order.
Merge Sort
Merge sort first divides the array into
equal halves and then combines
them in a sorted manner.
Insertion Sort
Insertion sort involves finding the right place for a given
element in a sorted list. So in beginning we compare the
first two elements and sort them by comparing them.
Then we pick the third element and find its proper
position among the previous two sorted elements. This
way we gradually go on adding more elements to the
already sorted list by putting them in their proper position.
Selection Sort
In selection sort we start by finding the minimum value in
a given list and move it to a sorted list. Then we repeat
the process for each of the remaining elements in the
unsorted list. The next element entering the sorted list is
compared with the existing elements and placed at its
correct position. So at the end all the elements from the
unsorted list are sorted.
QuickSort in Python
• QuickSort can be implemented both iteratively and recursively. We’ll mainly
focus on the recursive implementation, as it is far more convenient, intuitive,
and simplistic – iterative implementation is generally unrecommended.
Arrays are used as an example here, but QuickSort can be implemented on
other data structures (like linked lists) as well.
• The algorithm can be broken down into three parts​​:
• Partitioning the array about the pivot.
• Passing the smaller arrays to the recursive calls.
• Joining the sorted arrays that are returned from the recursive call and the
pivot.
Merge sort
• Merge sort: Repeatedly divides data in half, sorts each half, and combines
the sorted halves into a sorted whole.
The algorithm:
• Divide the list into two roughly equal halves.
• Sort the left half.
• Sort the right half.
• Merge the two sorted halves into one sorted list.

• Often implemented recursively.


• An example of a "divide and conquer" algorithm.
• Invented by John von Neumann in 1945
Merge sort example
index 0 1 2 3 4 5 6 7
value 22 18 12 -4 58 7 31 42
split

22 18 12 -4 58 7 31 42
split split

22 18 12 -4 58 7 31 42
split split split split

22 18 12 -4 58 7 31 42
merge merge merge merge
18 22 -4 12 7 58 31 42
merge merge
-4 12 18 22 7 31 42 58
merge
-4 7 12 18 22 31 42 58
The algorithm for merge sort may be written
as this list of steps:
• Create a temporary storage list which is the same size as the list to be sorted.
• Start by treating each element of the list as a sorted one-element sub-section of the original list.
• Move through all the sorted sub-sections, merging adjacent pairs as follows:
• Use two variables to point to the indices of the smallest uncopied items in the two sorted sub-sections, and a
third variable to point to the index of the start of the temporary storage.
• Copy the smaller of the two indexed items into the indicated position in the temporary storage. Increment
the index of the sub-section from which the item was copied, and the index into temporary storage.
• If all the items in one sub-section have been copied, copy the items remaining in the other sub-section to the
back of the list in temporary storage. Otherwise return to step 3 ii.
• Copy the sorted list in temporary storage back over the section of the original list which was occupied by the
two sub-sections that have just been merged.
• If only a single sorted sub-section remains, the entire list is sorted and we are done. Otherwise return to the
start of step 3.
Python’s sorting algorithm
• Python’s default sorting algorithm, which is used by the built-in sorted
function as well as the sort method of list objects, is called Timsort.
• It’s an algorithm developed by Tim Peters in 2002 for use in Python.
• Timsort is a modified version of merge sort which uses insertion sort to
arrange the list of items into conveniently mergeable sections.
Python sorted() function
• The sorted() function in python returns a sorted list of the iterable object
i.e. list, dictionary, and tuple. By default, it sorts the given object in
ascending order.

• In the case of strings and characters, sorting is done based on their ASCII
values.
tuple = (2, -1, 7, 5)
list = sorted(tuple)
print(list)

• sorted(iterable_object, key=key, reverse=False)


Q&A Sessions

Thank You

You might also like