SlideShare une entreprise Scribd logo
1  sur  82
Sorting algorithms
Eleonora Ciceri, Politecnico di Milano
Email: eleonora.ciceri@polimi.it
What is sorting
 Sorting is any process of arranging items systematically,
with two distinct meanings:
 Ordering: arranging items in a sequence ordered by some
criterion
 Categorizing: grouping items with similar properties
What is a sorting algorithm
 A sorting algorithm is an algorithm that puts elements of a
list in a certain order
78
26
4
12
90
4
12
26
78
90
Why do we need to sort?
 Sorted lists / sequences are useful in the following cases:
 1) Efficient lookup and search
56 31 2 47 54 19 64 85 23
56 31 2 47 54 19 64 85 23
56 31 2 47 54 19 64 85 23
56 31 2 47 54 19 64 85 23
56 31 2 47 54 19 64 85 23
56 31 2 47 54 19 64 85 23
Finding “19”
Non-ordered
list: 6 accesses
Why do we need to sort?
 Sorted lists / sequences are useful in the following cases:
 1) Efficient lookup and search
2 19 23 31 47 54 56 64 85
2 19 23 31 47 54 56 64 85
Finding “19”
Ordered list:
2 accesses
Why do we need to sort?
 Sorted lists / sequences are useful in the following cases:
 2) Merge sequences
2 5 9 13 20
1 4 8 21
1 2 4 5 8 9 13 20 21
What do we sort?
“Workers sort
parcels in a postal
facility”
(from: Wikipedia,
the free
encyclopedia)
What do we sort?
 We are going to order collections of data
 A couple of examples:
 Arrays
 Linked lists
Compare and swap to sort elements
Common building block of sorting
algorithms
 There are several algorithms one could use to order a list of
elements
 Although they are all different, they share a common building
block:
 They compare pairs of elements to decide which is their relative
order
 When needed, they swap the elements to restore their order
6 8 3 0 5 9Compare:
6 5 3 0 8 9Swap:
How to compare elements:
Lexicographic order
 Elements are usually ordered according to the lexicographic
(or, equivalently, lexicographical) order
 Also known as dictionary order
 The lexicographic order is a generalization of the way the
alphabetical order of words is based on the alphabetical
order of their component letters
How to compare elements:
Some examples
 Some examples
 Order numbers from the smallest to the largest
 Order words alphabetically
 Order people (implemented using a struct that contains name,
surname, age) alphabetically by surname
 People having the same surname are ordered according to their name
 People having the same name and surname are ordered according to their age
 Thus: this proves that comparisons could be based on
complex rules that sort out what to do in case of ties
Sorting algorithms time complexity
Which is the difference between sorting
algorithms?
 Each sorting algorithm is characterized by a particular sorting
strategy
 Are they all equal? NO!
 Some of them are naïve, some of them aren’t
 Naïve algorithms require a larger number of comparisons
 A larger number of comparisons amount to a larger time frame
spent to order elements in the collection
0
2000
4000
6000
8000
10000
12000
10 20 30 40 50 60 70 80 90 100
BubbleSort
MergeSort
(Time) complexity of an algorithm
 In computer science, the time complexity of an algorithm
quantifies the amount of time taken by the algorithm to run
 The time complexity:
 is expressed as a function of the length of the input
 is commonly expressed using big-O notation
Big-O notation
 “The big-O notation describes the limiting behavior of a
function when the number of elements it has to process tends
to infinity”
 We’ll try to simplify the concept in this way:
number of
elements complexity
N O(N2)
e.g., the number of
elements in the array
we want to order
e.g., the number of
comparisons we need to
perform to order the array
Big-O notation:
Which algorithm is “the best”?
 To us, an algorithm is “good” if it allows us to save time
 Let N be the number of elements we want to process
 E.g., number of elements in an array
 What if we have four algorithms whose time complexities are
as follows?
Algorithm
1
O(N)
Algorithm
2
O(N2)
Algorithm
3
O(log(N))
Big-O notation:
Which algorithm is “the best”?
 Time complexity can be treated as if it were a mathematical
function
 Being mathematical functions, these curves can be drawn on
a graph
Algorithm
1
O(N) f(N) = N
Algorithm
2
O(N2) f(N) = N2
Algorithm
3
O(log(N)) f(N) = log(N)
Algorithm
4
O(N*log(N)) f(N) = N*log(N)
Big-O notation:
Which algorithm is “the best”?
0
100
200
300
400
500
600
700
800
900
1000
1 5 10 15 20 25 30
f(N) = N
f(N) = N
f(N) = log(N)
f(N) = N*log(N)
2
Big-O notation:
Which algorithm is “the best”?
0
100
200
300
400
500
600
700
800
900
1000
1 5 10 15 20 25 30
f(N) = N
f(N) = N
f(N) = log(N)
f(N) = N*log(N)
2
The higher the number
of required operation,
the more the time,
the worse the algorithm!
So, how do we select a sorting
algorithm?
 We can identify several “families” of algorithms:
 Simple sorts
 Efficient sorts
 BubbleSort and variants
 Distribution sorts
So, how do we select a sorting
algorithm?
 We can identify several “families” of algorithms:
 Simple sorts
 Efficient sorts
 BubbleSort and variants
 Distribution sorts
Simple sorting algorithms are efficient on small data amounts (due to
low overhead), but generally do not perform well on large lists
 Insertion sort
 Selection sort
So, how do we select a sorting
algorithm?
 We can identify several “families” of algorithms:
 Simple sorts
 Efficient sorts
 BubbleSort and variants
 Distribution sorts
Efficient sorting algorithms are those algorithms whose average
complexity is the best you can find (O(N*log(N)))
 Merge sort
 Heap sort
 Quick sort
So, how do we select a sorting
algorithm?
 We can identify several “families” of algorithms:
 Simple sorts
 Efficient sorts
 BubbleSort and variants
 Distribution sorts
Bubble sort algorithm is very simple, and the same characteristics is
inherited by all its variants. However, it is highly inefficient (i.e., its time
complexity is very high: O(N2))
 Bubble sort
 Shell sort
 Comb sort
So, how do we select a sorting
algorithm?
 We can identify several “families” of algorithms:
 Simple sorts
 Efficient sorts
 BubbleSort and variants
 Distribution sorts
These algorithms distribute the input to intermediate structures, which are
gathered and placed on the output. They are useful in case of very large
data sets that do not fit in memory (since the intermediate structures can
be deployed on different machines)
 Counting sort
 Bucket sort
 Radix sort
0
2000
4000
6000
8000
10000
12000
10 20 30 40 50 60 70 80 90 100
BubbleSort
MergeSort
One of the most
inefficient algorithms
(O(N2))
One of the most
efficientalgorithms
(O(N*log(N)))
A quick note on time complexity
 An algorithm performance may vary with different input of the
same sizes
 [1 2 3 4 5] does not require any swap
 [5 4 3 2 1] requires a lot of swaps!
 We commonly attribute to each algorithm:
 A worst-case complexity (maximum amount of time it may require)
 An average-case complexity (averaged on all possible inputs)
 We won’t go into the details, since this is just an introduction to
sorting algorithms 
An inefficient sorting algorithm:
the Bubble Sort algorithm
Bubble sort: the idea
 The bubble sort algorithm repeatedly steps through the list of
elements to be sorted:
 Comparing each pair of adjacent elements
 Swapping them if they are in wrong order
 The algorithm stops when, by going through the whole array,
we do not require any swap (i.e., the array is fully ordered)
Bubble sort: the running example
 Array:
 [5 1 4 2 8]
 5 > 1, thus: swap required
Swapped something? False
End of iteration? False
Bubble sort: the running example
 Array:
 [1 5 4 2 8]
 5 > 4, thus: swap required
Swapped something? Yes
End of iteration? False
Bubble sort: the running example
 Array:
 [1 4 5 2 8]
 5 > 2, thus: swap required
Swapped something? Yes
End of iteration? False
Bubble sort: the running example
 Array:
 [1 4 2 5 8]
 5 < 8, thus: swap not required
Swapped something? Yes
End of iteration? Yes
Bubble sort: the running example
 Array:
 [1 4 2 5 8]
 We need to proceed, since in the current iteration we swapped
something
 Following operations:
 Exclude “8” from next iteration
 Restart with a new iteration
Swapped something? Yes
End of iteration? Yes
Bubble sort: the running example
 Array:
 [1 4 2 5 8]
 1 < 4, thus: swap not required
Swapped something? No
End of iteration? No
Bubble sort: the running example
 Array:
 [1 4 2 5 8]
 4 > 2, thus: swap required
Swapped something? No
End of iteration? No
Bubble sort: the running example
 Array:
 [1 2 4 5 8]
 4 < 5, thus: swap not required
Swapped something? Yes
End of iteration? Yes
Bubble sort: the running example
 Array:
 [1 2 4 5 8]
 We need to proceed, since in the current iteration we swapped
something
 Following operations:
 Exclude “5” from next iteration
 Restart with a new iteration
Swapped something? Yes
End of iteration? Yes
Bubble sort: the running example
 Array:
 [1 2 4 5 8]
 1 < 2, thus: swap not required
Swapped something? No
End of iteration? No
Bubble sort: the running example
 Array:
 [1 2 4 5 8]
 2 < 4, thus: swap not required
Swapped something? No
End of iteration? Yes
Bubble sort: the running example
 Array:
 [1 2 4 5 8]
 In the current iteration nothing was swapped
 The algorithm terminates
Swapped something? No
End of iteration? Yes
Bubble sort: the pseudo-code
procedure bubbleSort( A : list of sortable items )
n = length(A)
repeat
swapped = false
for i = 1 to n-1 inclusive do
if A[i-1] > A[i] then
swap(A[i-1], A[i])
swapped = true
end if
end for
n = n - 1
until not swapped
end procedure
(from: Wikipedia, the free encyclopedia)
Bubble sort in C++:
Iterate over the array
 We will use the concept of iterators over the array
 To iterate over the array:
int* first int* last
while (first < last) {
// do what you want
first++;
}
Bubble sort in C++:
The algorithm
void bubble_sort_int(int* first, int* last) {
bool swapped;
do {
swapped = false;
int* current = first + 1;
while (current < last) {
if (*(current-1) > *current) {
swap(current-1, current);
swapped = true;
}
current++;
}
last--;
} while (swapped);
}
Bubble sort in C++:
Swapping elements
void swap(int* first, int* second) {
int temp = *first;
*first = *second;
*second = temp;
}
An efficient sorting algorithm:
the Merge Sort algorithm
Merge sort: the idea
 The merge sort is a divide-and-conquer algorithm where:
 The unsorted list of N elements is divided into N sub-lists, each
containing one element
 Sub-lists are repeatedly merged to produce new sorted sub-lists
How to merge two sub-lists
 Sub-lists are merged so as produce a longer sorted sub-list
 At each step, we:
 compare the first element of sub-list A and the first element of
sub-list B
 select the smallest element
 insert it into the resulting list
How to merge two sub-lists:
An example
 How to merge these:
 to obtain this?
1 6 10
4 5 27
1 4 5 6 10 27
How to merge two sub-lists:
An example
1 6 10
4 5 27
1
1)
How to merge two sub-lists:
An example
1 6 10
4 5 27
1
6 10
4 5 27
1 4
1) 2)
How to merge two sub-lists:
An example
1 6 10
4 5 27
1
6 10
4 5 27
1 4
6 10
5 27
1 4 5
1) 2) 3)
How to merge two sub-lists:
An example
1 6 10
4 5 27
1
6 10
4 5 27
1 4
6 10
5 27
1 4 5
6 10
27
1 4 5 6
1) 2) 3) 4)
How to merge two sub-lists:
An example
1 6 10
4 5 27
1
6 10
4 5 27
1 4
6 10
5 27
1 4 5
6 10
27
1 4 5 6
10
27
1 4 5 6 10
1) 2) 3) 4)
5)
How to merge two sub-lists:
An example
1 6 10
4 5 27
1
6 10
4 5 27
1 4
6 10
5 27
1 4 5
6 10
27
1 4 5 6
10
27
1 4 5 6 10
27
/
1 4 5 6 10 27
1) 2) 3) 4)
5) 6)
Howtomergetwo
sub-lists:
Thepseudocode
function merge(left, right)
// merge lists until at least one of them is empty
while notempty(left) and notempty(right)
if first(left) <= first(right)
append first(left) to result
left = rest(left)
else
append first(right) to result
right = rest(right)
// left has elements left
while notempty(left)
append first(left) to result
left = rest(left)
// right has elements left
while notempty(right)
append first(right) to result
right = rest(right)
return result
(from: Wikipedia, the free encyclopedia)
Merge sort: the step-by-step algorithm
 We start with this list…
 …and, according to the algorithm, we need to keep dividing it
in “left sub-list” and “right sub-list”…
Merge sort: the step-by-step algorithm
 …and split again, since sub-lists length is greater than 1…
Merge sort: the step-by-step algorithm
 … and again.
Merge sort: the step-by-step algorithm
 Now it’s time to merge the lists, in pairs, so that the resulting
list is ordered
 We start from the bottom, i.e., from the shorter sub-lists we
extracted before
This is now
ordered
Merge sort: the step-by-step algorithm
 Merge…
This is now
ordered
Merge sort: the step-by-step algorithm
 …and merge.
This is now
ordered
Trying to generalize: Which operations
are needed at each iteration?
 Split phase:
 If the current list has length 1, stop with the split phase
 If the current list has length > 1, split it in two parts (left, right)
 Merge phase:
 Merge the left and right parts into a unique, ordered list
Trying to generalize: What happens to a list?
 Given a list:
Split in
two parts
(left and right)
Split in
two parts
(left and right)
Do something
to order the sub-list
Do something
to order the sub-list
Merge the
parts in a
single list
Merge the
parts in a
single list
Trying to generalize: What happens to a list?
 Algorithmically:
[left, right] = split(list)
left = order(left)
right = order(right)
list = merge(left, right)
Trying to generalize: What happens to a list?
 Algorithmically:
[left, right] = split(list)
left = mergeSort(left)
right = mergeSort(right)
list = merge(left, right)
This is a
recursive call
to the merge sort function
Trying to generalize: What happens to a list?
 Now we finalize the algorithm:
function mergeSort(list) {
if (length(list) == 1)
return list
[left, right] = split(list)
left = mergeSort(left)
right = mergeSort(right)
list = merge(left, right)
return list
}
Trying to generalize: What happens to a list?
 Now we finalize the algorithm:
function mergeSort(list) {
if (length(list) == 1)
return list
[left, right] = split(list)
left = mergeSort(left)
right = mergeSort(right)
list = merge(left, right)
return list
}
This function splits the
list in two parts having
the same length
This function merges
the sub-lists so that the
result is ordered
Merge sort in C++:
Iterate over the array
 We will use the concept of iterators over the array
 To iterate over the array:
int* first int* last
while (first < last) {
// do what you want
first++;
}
Merge sort in C++:
Iterate over the array
 We will use the concept of iterators over the array
 To iterate over the array:
int* first int* last
while (first < last) {
// do what you want
first++;
}
Remember:
last always points
outside the array
Merge sort in C++:
Split the array in two parts
 If the length of the array is even:
 The sub-lists are as follows:
 Left: from first (included) to middle (excluded)
 Right: from middle (included) to last (excluded)
int* first int* lastint* middle
Merge sort in C++:
Split the array in two parts
 If the length of the array is even:
 The sub-lists are as follows:
 Left: from first (included) to middle (excluded)
 Right: from middle (included) to last (excluded)
int* first int* lastint* middle
Merge sort in C++:
Split the array in two parts
 If the length of the array is even:
 The sub-lists are as follows:
 Left: from first (included) to middle (excluded)
 Right: from middle (included) to last (excluded)
int* first int* lastint* middle
Merge sort in C++:
Split the array in two parts
 If the length of the array is odd:
 The sub-lists are as follows:
 Left: from first (included) to middle (excluded)
 Right: from middle (included) to last (excluded)
 In this case: right is longer than left
int* first int* lastint* middle
Merge sort in C++:
The recursive algorithm
void merge_sort_int(int* first, int* last) {
int N = last - first;
if (N <= 1)
return;
int* middle = first + N/2;
merge_sort_int(first, middle);
merge_sort_int(middle, last);
merge(first, middle, last);
}
MergesortinC++:
Themergefunction
void merge(int* first, int* middle, int* last) {
std::size_t N = last - first;
int *result = new int[N],
*result_current = result,
*left_current = first,
*right_current = middle;
while (left_current < middle && right_current <
last) {
if (*left_current <= *right_current) {
*result_current = *left_current;
left_current++;
}
else {
*result_current = *right_current;
right_current++;
}
result_current++;
}
fill_with_remaining_elements(result_current,
left_current, middle);
fill_with_remaining_elements(result_current,
right_current, last);
for (std::size_t i = 0; i < N; i++)
first[i] = result[i];
delete[] result;
}
MergesortinC++:
Themergefunction
void merge(int* first, int* middle, int* last) {
std::size_t N = last - first;
int *result = new int[N],
*result_current = result,
*left_current = first,
*right_current = middle;
while (left_current < middle && right_current <
last) {
if (*left_current <= *right_current) {
*result_current = *left_current;
left_current++;
}
else {
*result_current = *right_current;
right_current++;
}
result_current++;
}
fill_with_remaining_elements(result_current,
left_current, middle);
fill_with_remaining_elements(result_current,
right_current, last);
for (std::size_t i = 0; i < N; i++)
first[i] = result[i];
delete[] result;
}
result is a temporary
buffer that contains
the ordered array
At the end of the function,
result is restored in the
original array and deleted
Merge sort in C++:
The merge function
void fill_with_remaining_elements(int*& result_current,
int* sub_list_current, int* sub_list_last) {
while (sub_list_current < sub_list_last) {
*result_current = *sub_list_current;
sub_list_current++;
result_current++;
}
}
References
References
 https://en.wikipedia.org/wiki/Sorting
 https://en.wikipedia.org/wiki/Sorting_algorithm
 https://en.wikipedia.org/wiki/Time_complexity
 https://en.wikipedia.org/wiki/Big_O_notation
 https://en.wikipedia.org/wiki/Bubble_sort
 https://en.wikipedia.org/wiki/Merge_sort
References
 http://stackoverflow.com/questions/24650626/how-to-
implement-classic-sorting-algorithms-in-modern-c

Contenu connexe

Tendances

Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-SortTareq Hasan
 
3.6 radix sort
3.6 radix sort3.6 radix sort
3.6 radix sortKrish_ver2
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sortMadhu Bala
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsAakash deep Singhal
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Hossain Md Shakhawat
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort AlgorithmLemia Algmri
 
Sorting types and Algorithms
Sorting types and AlgorithmsSorting types and Algorithms
Sorting types and AlgorithmsAli Khan
 
CS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsCS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsKrishnan MuthuManickam
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithmsRajendran
 
Different types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with AnimationDifferent types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with AnimationZakaria Hossain
 
Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentationirdginfo
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sortDistrict Administration
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data StructuresSHAKOOR AB
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals reviewElifTech
 
Algorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms IIAlgorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms IIMohamed Loey
 

Tendances (20)

Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
3.6 radix sort
3.6 radix sort3.6 radix sort
3.6 radix sort
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Radix sort
Radix sortRadix sort
Radix sort
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Sorting types and Algorithms
Sorting types and AlgorithmsSorting types and Algorithms
Sorting types and Algorithms
 
Heaps
HeapsHeaps
Heaps
 
CS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsCS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of Algorithms
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Different types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with AnimationDifferent types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with Animation
 
Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals review
 
Algorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms IIAlgorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms II
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 

En vedette

Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithmsmultimedia9
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Muhammad Hammad Waseem
 
Sorting bubble-sort anim
Sorting   bubble-sort animSorting   bubble-sort anim
Sorting bubble-sort animFajar Zain
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting AlgorithmsShivam Singh
 
Sorting algorithms v01
Sorting algorithms v01Sorting algorithms v01
Sorting algorithms v01Dusan Vuckovic
 
Parallel sorting algorithm
Parallel sorting algorithmParallel sorting algorithm
Parallel sorting algorithmRicha Kumari
 
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sortgeeortiz
 
Computer notes - Mergesort
Computer notes - MergesortComputer notes - Mergesort
Computer notes - Mergesortecomputernotes
 
Bubble sorting lab manual
Bubble sorting lab manualBubble sorting lab manual
Bubble sorting lab manualmaamir farooq
 
An Introduction To Python - Working With Data
An Introduction To Python - Working With DataAn Introduction To Python - Working With Data
An Introduction To Python - Working With DataBlue Elephant Consulting
 
Radix sort presentation
Radix sort presentationRadix sort presentation
Radix sort presentationRatul Hasan
 
PANCASILA (makalah falsafah pancasila)
PANCASILA (makalah falsafah pancasila) PANCASILA (makalah falsafah pancasila)
PANCASILA (makalah falsafah pancasila) tita_chubie
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting AlgorithmsPranay Neema
 

En vedette (20)

Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
Sorting bubble-sort anim
Sorting   bubble-sort animSorting   bubble-sort anim
Sorting bubble-sort anim
 
Sorting
SortingSorting
Sorting
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting Algorithm
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Sorting algorithms v01
Sorting algorithms v01Sorting algorithms v01
Sorting algorithms v01
 
Parallel sorting algorithm
Parallel sorting algorithmParallel sorting algorithm
Parallel sorting algorithm
 
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sort
 
Computer notes - Mergesort
Computer notes - MergesortComputer notes - Mergesort
Computer notes - Mergesort
 
Algorithm - Mergesort & Quicksort
Algorithm - Mergesort & Quicksort Algorithm - Mergesort & Quicksort
Algorithm - Mergesort & Quicksort
 
Mergesort
MergesortMergesort
Mergesort
 
Bubble sorting lab manual
Bubble sorting lab manualBubble sorting lab manual
Bubble sorting lab manual
 
An Introduction To Python - Working With Data
An Introduction To Python - Working With DataAn Introduction To Python - Working With Data
An Introduction To Python - Working With Data
 
Matlab
MatlabMatlab
Matlab
 
Radix sort presentation
Radix sort presentationRadix sort presentation
Radix sort presentation
 
PANCASILA (makalah falsafah pancasila)
PANCASILA (makalah falsafah pancasila) PANCASILA (makalah falsafah pancasila)
PANCASILA (makalah falsafah pancasila)
 
Sorting
SortingSorting
Sorting
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 

Similaire à Sorting algorithms

Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingEduardo Bergavera
 
Data Structures- Part1 overview and review
Data Structures- Part1 overview and reviewData Structures- Part1 overview and review
Data Structures- Part1 overview and reviewAbdullah Al-hazmy
 
Csallner algorithms1
Csallner algorithms1Csallner algorithms1
Csallner algorithms1seshagiri rao
 
Chapter 5 searching and sorting handouts with notes
Chapter 5   searching and sorting handouts with notesChapter 5   searching and sorting handouts with notes
Chapter 5 searching and sorting handouts with notesmailund
 
Sorting Techniques for Data Structures.pptx
Sorting Techniques for Data Structures.pptxSorting Techniques for Data Structures.pptx
Sorting Techniques for Data Structures.pptxKalpana Mohan
 
Radix and Shell sort
Radix and Shell sortRadix and Shell sort
Radix and Shell sorthannatamayao
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to AlgorithmsMohamed Essam
 
Is sort andy-le
Is sort andy-leIs sort andy-le
Is sort andy-leSumedha
 
Algorithm By AMT.pptx
Algorithm By AMT.pptxAlgorithm By AMT.pptx
Algorithm By AMT.pptxAungMyintTun3
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)jehan1987
 

Similaire à Sorting algorithms (20)

Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Data Structures- Part1 overview and review
Data Structures- Part1 overview and reviewData Structures- Part1 overview and review
Data Structures- Part1 overview and review
 
Csallner algorithms1
Csallner algorithms1Csallner algorithms1
Csallner algorithms1
 
Ds
DsDs
Ds
 
daa unit 1.pptx
daa unit 1.pptxdaa unit 1.pptx
daa unit 1.pptx
 
Chapter 5 searching and sorting handouts with notes
Chapter 5   searching and sorting handouts with notesChapter 5   searching and sorting handouts with notes
Chapter 5 searching and sorting handouts with notes
 
Sorting Techniques for Data Structures.pptx
Sorting Techniques for Data Structures.pptxSorting Techniques for Data Structures.pptx
Sorting Techniques for Data Structures.pptx
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
Sorting
SortingSorting
Sorting
 
sorting_part1.ppt
sorting_part1.pptsorting_part1.ppt
sorting_part1.ppt
 
Radix and Shell sort
Radix and Shell sortRadix and Shell sort
Radix and Shell sort
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Sorting algorithm
Sorting algorithmSorting algorithm
Sorting algorithm
 
Is sort andy-le
Is sort andy-leIs sort andy-le
Is sort andy-le
 
Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sort
 
Shellshort ppt
Shellshort pptShellshort ppt
Shellshort ppt
 
Time Complexity Analysis in Data Structure.docx
Time Complexity Analysis in Data Structure.docxTime Complexity Analysis in Data Structure.docx
Time Complexity Analysis in Data Structure.docx
 
Sortsearch
SortsearchSortsearch
Sortsearch
 
Algorithm By AMT.pptx
Algorithm By AMT.pptxAlgorithm By AMT.pptx
Algorithm By AMT.pptx
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 

Plus de Eleonora Ciceri

DDD - 5 - Domain Driven Design_ Repositories.pdf
DDD - 5 - Domain Driven Design_ Repositories.pdfDDD - 5 - Domain Driven Design_ Repositories.pdf
DDD - 5 - Domain Driven Design_ Repositories.pdfEleonora Ciceri
 
DDD - 4 - Domain Driven Design_ Architectural patterns.pdf
DDD - 4 - Domain Driven Design_ Architectural patterns.pdfDDD - 4 - Domain Driven Design_ Architectural patterns.pdf
DDD - 4 - Domain Driven Design_ Architectural patterns.pdfEleonora Ciceri
 
DDD - 3 - Domain Driven Design: Event sourcing.pdf
DDD - 3 - Domain Driven Design: Event sourcing.pdfDDD - 3 - Domain Driven Design: Event sourcing.pdf
DDD - 3 - Domain Driven Design: Event sourcing.pdfEleonora Ciceri
 
DDD - 2 - Domain Driven Design: Tactical design.pdf
DDD - 2 - Domain Driven Design: Tactical design.pdfDDD - 2 - Domain Driven Design: Tactical design.pdf
DDD - 2 - Domain Driven Design: Tactical design.pdfEleonora Ciceri
 
DDD - 1 - A gentle introduction to Domain Driven Design.pdf
DDD - 1 - A gentle introduction to Domain Driven Design.pdfDDD - 1 - A gentle introduction to Domain Driven Design.pdf
DDD - 1 - A gentle introduction to Domain Driven Design.pdfEleonora Ciceri
 
Artificial Intelligence: an introduction.pdf
Artificial Intelligence: an introduction.pdfArtificial Intelligence: an introduction.pdf
Artificial Intelligence: an introduction.pdfEleonora Ciceri
 
Linked lists - Exercises
Linked lists - ExercisesLinked lists - Exercises
Linked lists - ExercisesEleonora Ciceri
 
Dynamic content generation
Dynamic content generationDynamic content generation
Dynamic content generationEleonora Ciceri
 
Multimedia Information Retrieval and User Behavior
Multimedia Information Retrieval and User BehaviorMultimedia Information Retrieval and User Behavior
Multimedia Information Retrieval and User BehaviorEleonora Ciceri
 
The CrowdSearch framework
The CrowdSearch frameworkThe CrowdSearch framework
The CrowdSearch frameworkEleonora Ciceri
 

Plus de Eleonora Ciceri (18)

DDD - 5 - Domain Driven Design_ Repositories.pdf
DDD - 5 - Domain Driven Design_ Repositories.pdfDDD - 5 - Domain Driven Design_ Repositories.pdf
DDD - 5 - Domain Driven Design_ Repositories.pdf
 
DDD - 4 - Domain Driven Design_ Architectural patterns.pdf
DDD - 4 - Domain Driven Design_ Architectural patterns.pdfDDD - 4 - Domain Driven Design_ Architectural patterns.pdf
DDD - 4 - Domain Driven Design_ Architectural patterns.pdf
 
DDD - 3 - Domain Driven Design: Event sourcing.pdf
DDD - 3 - Domain Driven Design: Event sourcing.pdfDDD - 3 - Domain Driven Design: Event sourcing.pdf
DDD - 3 - Domain Driven Design: Event sourcing.pdf
 
DDD - 2 - Domain Driven Design: Tactical design.pdf
DDD - 2 - Domain Driven Design: Tactical design.pdfDDD - 2 - Domain Driven Design: Tactical design.pdf
DDD - 2 - Domain Driven Design: Tactical design.pdf
 
DDD - 1 - A gentle introduction to Domain Driven Design.pdf
DDD - 1 - A gentle introduction to Domain Driven Design.pdfDDD - 1 - A gentle introduction to Domain Driven Design.pdf
DDD - 1 - A gentle introduction to Domain Driven Design.pdf
 
Artificial Intelligence: an introduction.pdf
Artificial Intelligence: an introduction.pdfArtificial Intelligence: an introduction.pdf
Artificial Intelligence: an introduction.pdf
 
Trees
TreesTrees
Trees
 
Linked lists - Exercises
Linked lists - ExercisesLinked lists - Exercises
Linked lists - Exercises
 
Doubly Linked Lists
Doubly Linked ListsDoubly Linked Lists
Doubly Linked Lists
 
Linked lists
Linked listsLinked lists
Linked lists
 
AJAX - An introduction
AJAX - An introductionAJAX - An introduction
AJAX - An introduction
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
JDBC in Servlets
JDBC in ServletsJDBC in Servlets
JDBC in Servlets
 
Client side scripting
Client side scriptingClient side scripting
Client side scripting
 
HTML5 - An introduction
HTML5 - An introductionHTML5 - An introduction
HTML5 - An introduction
 
Dynamic content generation
Dynamic content generationDynamic content generation
Dynamic content generation
 
Multimedia Information Retrieval and User Behavior
Multimedia Information Retrieval and User BehaviorMultimedia Information Retrieval and User Behavior
Multimedia Information Retrieval and User Behavior
 
The CrowdSearch framework
The CrowdSearch frameworkThe CrowdSearch framework
The CrowdSearch framework
 

Dernier

EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 

Dernier (20)

EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 

Sorting algorithms

  • 1. Sorting algorithms Eleonora Ciceri, Politecnico di Milano Email: eleonora.ciceri@polimi.it
  • 2. What is sorting  Sorting is any process of arranging items systematically, with two distinct meanings:  Ordering: arranging items in a sequence ordered by some criterion  Categorizing: grouping items with similar properties
  • 3. What is a sorting algorithm  A sorting algorithm is an algorithm that puts elements of a list in a certain order 78 26 4 12 90 4 12 26 78 90
  • 4. Why do we need to sort?  Sorted lists / sequences are useful in the following cases:  1) Efficient lookup and search 56 31 2 47 54 19 64 85 23 56 31 2 47 54 19 64 85 23 56 31 2 47 54 19 64 85 23 56 31 2 47 54 19 64 85 23 56 31 2 47 54 19 64 85 23 56 31 2 47 54 19 64 85 23 Finding “19” Non-ordered list: 6 accesses
  • 5. Why do we need to sort?  Sorted lists / sequences are useful in the following cases:  1) Efficient lookup and search 2 19 23 31 47 54 56 64 85 2 19 23 31 47 54 56 64 85 Finding “19” Ordered list: 2 accesses
  • 6. Why do we need to sort?  Sorted lists / sequences are useful in the following cases:  2) Merge sequences 2 5 9 13 20 1 4 8 21 1 2 4 5 8 9 13 20 21
  • 7. What do we sort? “Workers sort parcels in a postal facility” (from: Wikipedia, the free encyclopedia)
  • 8. What do we sort?  We are going to order collections of data  A couple of examples:  Arrays  Linked lists
  • 9. Compare and swap to sort elements
  • 10. Common building block of sorting algorithms  There are several algorithms one could use to order a list of elements  Although they are all different, they share a common building block:  They compare pairs of elements to decide which is their relative order  When needed, they swap the elements to restore their order 6 8 3 0 5 9Compare: 6 5 3 0 8 9Swap:
  • 11. How to compare elements: Lexicographic order  Elements are usually ordered according to the lexicographic (or, equivalently, lexicographical) order  Also known as dictionary order  The lexicographic order is a generalization of the way the alphabetical order of words is based on the alphabetical order of their component letters
  • 12. How to compare elements: Some examples  Some examples  Order numbers from the smallest to the largest  Order words alphabetically  Order people (implemented using a struct that contains name, surname, age) alphabetically by surname  People having the same surname are ordered according to their name  People having the same name and surname are ordered according to their age  Thus: this proves that comparisons could be based on complex rules that sort out what to do in case of ties
  • 14. Which is the difference between sorting algorithms?  Each sorting algorithm is characterized by a particular sorting strategy  Are they all equal? NO!  Some of them are naïve, some of them aren’t  Naïve algorithms require a larger number of comparisons  A larger number of comparisons amount to a larger time frame spent to order elements in the collection
  • 15. 0 2000 4000 6000 8000 10000 12000 10 20 30 40 50 60 70 80 90 100 BubbleSort MergeSort
  • 16. (Time) complexity of an algorithm  In computer science, the time complexity of an algorithm quantifies the amount of time taken by the algorithm to run  The time complexity:  is expressed as a function of the length of the input  is commonly expressed using big-O notation
  • 17. Big-O notation  “The big-O notation describes the limiting behavior of a function when the number of elements it has to process tends to infinity”  We’ll try to simplify the concept in this way: number of elements complexity N O(N2) e.g., the number of elements in the array we want to order e.g., the number of comparisons we need to perform to order the array
  • 18. Big-O notation: Which algorithm is “the best”?  To us, an algorithm is “good” if it allows us to save time  Let N be the number of elements we want to process  E.g., number of elements in an array  What if we have four algorithms whose time complexities are as follows? Algorithm 1 O(N) Algorithm 2 O(N2) Algorithm 3 O(log(N))
  • 19. Big-O notation: Which algorithm is “the best”?  Time complexity can be treated as if it were a mathematical function  Being mathematical functions, these curves can be drawn on a graph Algorithm 1 O(N) f(N) = N Algorithm 2 O(N2) f(N) = N2 Algorithm 3 O(log(N)) f(N) = log(N) Algorithm 4 O(N*log(N)) f(N) = N*log(N)
  • 20. Big-O notation: Which algorithm is “the best”? 0 100 200 300 400 500 600 700 800 900 1000 1 5 10 15 20 25 30 f(N) = N f(N) = N f(N) = log(N) f(N) = N*log(N) 2
  • 21. Big-O notation: Which algorithm is “the best”? 0 100 200 300 400 500 600 700 800 900 1000 1 5 10 15 20 25 30 f(N) = N f(N) = N f(N) = log(N) f(N) = N*log(N) 2 The higher the number of required operation, the more the time, the worse the algorithm!
  • 22. So, how do we select a sorting algorithm?  We can identify several “families” of algorithms:  Simple sorts  Efficient sorts  BubbleSort and variants  Distribution sorts
  • 23. So, how do we select a sorting algorithm?  We can identify several “families” of algorithms:  Simple sorts  Efficient sorts  BubbleSort and variants  Distribution sorts Simple sorting algorithms are efficient on small data amounts (due to low overhead), but generally do not perform well on large lists  Insertion sort  Selection sort
  • 24. So, how do we select a sorting algorithm?  We can identify several “families” of algorithms:  Simple sorts  Efficient sorts  BubbleSort and variants  Distribution sorts Efficient sorting algorithms are those algorithms whose average complexity is the best you can find (O(N*log(N)))  Merge sort  Heap sort  Quick sort
  • 25. So, how do we select a sorting algorithm?  We can identify several “families” of algorithms:  Simple sorts  Efficient sorts  BubbleSort and variants  Distribution sorts Bubble sort algorithm is very simple, and the same characteristics is inherited by all its variants. However, it is highly inefficient (i.e., its time complexity is very high: O(N2))  Bubble sort  Shell sort  Comb sort
  • 26. So, how do we select a sorting algorithm?  We can identify several “families” of algorithms:  Simple sorts  Efficient sorts  BubbleSort and variants  Distribution sorts These algorithms distribute the input to intermediate structures, which are gathered and placed on the output. They are useful in case of very large data sets that do not fit in memory (since the intermediate structures can be deployed on different machines)  Counting sort  Bucket sort  Radix sort
  • 27. 0 2000 4000 6000 8000 10000 12000 10 20 30 40 50 60 70 80 90 100 BubbleSort MergeSort One of the most inefficient algorithms (O(N2)) One of the most efficientalgorithms (O(N*log(N)))
  • 28. A quick note on time complexity  An algorithm performance may vary with different input of the same sizes  [1 2 3 4 5] does not require any swap  [5 4 3 2 1] requires a lot of swaps!  We commonly attribute to each algorithm:  A worst-case complexity (maximum amount of time it may require)  An average-case complexity (averaged on all possible inputs)  We won’t go into the details, since this is just an introduction to sorting algorithms 
  • 29. An inefficient sorting algorithm: the Bubble Sort algorithm
  • 30. Bubble sort: the idea  The bubble sort algorithm repeatedly steps through the list of elements to be sorted:  Comparing each pair of adjacent elements  Swapping them if they are in wrong order  The algorithm stops when, by going through the whole array, we do not require any swap (i.e., the array is fully ordered)
  • 31. Bubble sort: the running example  Array:  [5 1 4 2 8]  5 > 1, thus: swap required Swapped something? False End of iteration? False
  • 32. Bubble sort: the running example  Array:  [1 5 4 2 8]  5 > 4, thus: swap required Swapped something? Yes End of iteration? False
  • 33. Bubble sort: the running example  Array:  [1 4 5 2 8]  5 > 2, thus: swap required Swapped something? Yes End of iteration? False
  • 34. Bubble sort: the running example  Array:  [1 4 2 5 8]  5 < 8, thus: swap not required Swapped something? Yes End of iteration? Yes
  • 35. Bubble sort: the running example  Array:  [1 4 2 5 8]  We need to proceed, since in the current iteration we swapped something  Following operations:  Exclude “8” from next iteration  Restart with a new iteration Swapped something? Yes End of iteration? Yes
  • 36. Bubble sort: the running example  Array:  [1 4 2 5 8]  1 < 4, thus: swap not required Swapped something? No End of iteration? No
  • 37. Bubble sort: the running example  Array:  [1 4 2 5 8]  4 > 2, thus: swap required Swapped something? No End of iteration? No
  • 38. Bubble sort: the running example  Array:  [1 2 4 5 8]  4 < 5, thus: swap not required Swapped something? Yes End of iteration? Yes
  • 39. Bubble sort: the running example  Array:  [1 2 4 5 8]  We need to proceed, since in the current iteration we swapped something  Following operations:  Exclude “5” from next iteration  Restart with a new iteration Swapped something? Yes End of iteration? Yes
  • 40. Bubble sort: the running example  Array:  [1 2 4 5 8]  1 < 2, thus: swap not required Swapped something? No End of iteration? No
  • 41. Bubble sort: the running example  Array:  [1 2 4 5 8]  2 < 4, thus: swap not required Swapped something? No End of iteration? Yes
  • 42. Bubble sort: the running example  Array:  [1 2 4 5 8]  In the current iteration nothing was swapped  The algorithm terminates Swapped something? No End of iteration? Yes
  • 43. Bubble sort: the pseudo-code procedure bubbleSort( A : list of sortable items ) n = length(A) repeat swapped = false for i = 1 to n-1 inclusive do if A[i-1] > A[i] then swap(A[i-1], A[i]) swapped = true end if end for n = n - 1 until not swapped end procedure (from: Wikipedia, the free encyclopedia)
  • 44. Bubble sort in C++: Iterate over the array  We will use the concept of iterators over the array  To iterate over the array: int* first int* last while (first < last) { // do what you want first++; }
  • 45. Bubble sort in C++: The algorithm void bubble_sort_int(int* first, int* last) { bool swapped; do { swapped = false; int* current = first + 1; while (current < last) { if (*(current-1) > *current) { swap(current-1, current); swapped = true; } current++; } last--; } while (swapped); }
  • 46. Bubble sort in C++: Swapping elements void swap(int* first, int* second) { int temp = *first; *first = *second; *second = temp; }
  • 47. An efficient sorting algorithm: the Merge Sort algorithm
  • 48. Merge sort: the idea  The merge sort is a divide-and-conquer algorithm where:  The unsorted list of N elements is divided into N sub-lists, each containing one element  Sub-lists are repeatedly merged to produce new sorted sub-lists
  • 49. How to merge two sub-lists  Sub-lists are merged so as produce a longer sorted sub-list  At each step, we:  compare the first element of sub-list A and the first element of sub-list B  select the smallest element  insert it into the resulting list
  • 50. How to merge two sub-lists: An example  How to merge these:  to obtain this? 1 6 10 4 5 27 1 4 5 6 10 27
  • 51. How to merge two sub-lists: An example 1 6 10 4 5 27 1 1)
  • 52. How to merge two sub-lists: An example 1 6 10 4 5 27 1 6 10 4 5 27 1 4 1) 2)
  • 53. How to merge two sub-lists: An example 1 6 10 4 5 27 1 6 10 4 5 27 1 4 6 10 5 27 1 4 5 1) 2) 3)
  • 54. How to merge two sub-lists: An example 1 6 10 4 5 27 1 6 10 4 5 27 1 4 6 10 5 27 1 4 5 6 10 27 1 4 5 6 1) 2) 3) 4)
  • 55. How to merge two sub-lists: An example 1 6 10 4 5 27 1 6 10 4 5 27 1 4 6 10 5 27 1 4 5 6 10 27 1 4 5 6 10 27 1 4 5 6 10 1) 2) 3) 4) 5)
  • 56. How to merge two sub-lists: An example 1 6 10 4 5 27 1 6 10 4 5 27 1 4 6 10 5 27 1 4 5 6 10 27 1 4 5 6 10 27 1 4 5 6 10 27 / 1 4 5 6 10 27 1) 2) 3) 4) 5) 6)
  • 57. Howtomergetwo sub-lists: Thepseudocode function merge(left, right) // merge lists until at least one of them is empty while notempty(left) and notempty(right) if first(left) <= first(right) append first(left) to result left = rest(left) else append first(right) to result right = rest(right) // left has elements left while notempty(left) append first(left) to result left = rest(left) // right has elements left while notempty(right) append first(right) to result right = rest(right) return result (from: Wikipedia, the free encyclopedia)
  • 58. Merge sort: the step-by-step algorithm  We start with this list…  …and, according to the algorithm, we need to keep dividing it in “left sub-list” and “right sub-list”…
  • 59. Merge sort: the step-by-step algorithm  …and split again, since sub-lists length is greater than 1…
  • 60. Merge sort: the step-by-step algorithm  … and again.
  • 61. Merge sort: the step-by-step algorithm  Now it’s time to merge the lists, in pairs, so that the resulting list is ordered  We start from the bottom, i.e., from the shorter sub-lists we extracted before This is now ordered
  • 62. Merge sort: the step-by-step algorithm  Merge… This is now ordered
  • 63. Merge sort: the step-by-step algorithm  …and merge. This is now ordered
  • 64. Trying to generalize: Which operations are needed at each iteration?  Split phase:  If the current list has length 1, stop with the split phase  If the current list has length > 1, split it in two parts (left, right)  Merge phase:  Merge the left and right parts into a unique, ordered list
  • 65. Trying to generalize: What happens to a list?  Given a list: Split in two parts (left and right) Split in two parts (left and right) Do something to order the sub-list Do something to order the sub-list Merge the parts in a single list Merge the parts in a single list
  • 66. Trying to generalize: What happens to a list?  Algorithmically: [left, right] = split(list) left = order(left) right = order(right) list = merge(left, right)
  • 67. Trying to generalize: What happens to a list?  Algorithmically: [left, right] = split(list) left = mergeSort(left) right = mergeSort(right) list = merge(left, right) This is a recursive call to the merge sort function
  • 68. Trying to generalize: What happens to a list?  Now we finalize the algorithm: function mergeSort(list) { if (length(list) == 1) return list [left, right] = split(list) left = mergeSort(left) right = mergeSort(right) list = merge(left, right) return list }
  • 69. Trying to generalize: What happens to a list?  Now we finalize the algorithm: function mergeSort(list) { if (length(list) == 1) return list [left, right] = split(list) left = mergeSort(left) right = mergeSort(right) list = merge(left, right) return list } This function splits the list in two parts having the same length This function merges the sub-lists so that the result is ordered
  • 70. Merge sort in C++: Iterate over the array  We will use the concept of iterators over the array  To iterate over the array: int* first int* last while (first < last) { // do what you want first++; }
  • 71. Merge sort in C++: Iterate over the array  We will use the concept of iterators over the array  To iterate over the array: int* first int* last while (first < last) { // do what you want first++; } Remember: last always points outside the array
  • 72. Merge sort in C++: Split the array in two parts  If the length of the array is even:  The sub-lists are as follows:  Left: from first (included) to middle (excluded)  Right: from middle (included) to last (excluded) int* first int* lastint* middle
  • 73. Merge sort in C++: Split the array in two parts  If the length of the array is even:  The sub-lists are as follows:  Left: from first (included) to middle (excluded)  Right: from middle (included) to last (excluded) int* first int* lastint* middle
  • 74. Merge sort in C++: Split the array in two parts  If the length of the array is even:  The sub-lists are as follows:  Left: from first (included) to middle (excluded)  Right: from middle (included) to last (excluded) int* first int* lastint* middle
  • 75. Merge sort in C++: Split the array in two parts  If the length of the array is odd:  The sub-lists are as follows:  Left: from first (included) to middle (excluded)  Right: from middle (included) to last (excluded)  In this case: right is longer than left int* first int* lastint* middle
  • 76. Merge sort in C++: The recursive algorithm void merge_sort_int(int* first, int* last) { int N = last - first; if (N <= 1) return; int* middle = first + N/2; merge_sort_int(first, middle); merge_sort_int(middle, last); merge(first, middle, last); }
  • 77. MergesortinC++: Themergefunction void merge(int* first, int* middle, int* last) { std::size_t N = last - first; int *result = new int[N], *result_current = result, *left_current = first, *right_current = middle; while (left_current < middle && right_current < last) { if (*left_current <= *right_current) { *result_current = *left_current; left_current++; } else { *result_current = *right_current; right_current++; } result_current++; } fill_with_remaining_elements(result_current, left_current, middle); fill_with_remaining_elements(result_current, right_current, last); for (std::size_t i = 0; i < N; i++) first[i] = result[i]; delete[] result; }
  • 78. MergesortinC++: Themergefunction void merge(int* first, int* middle, int* last) { std::size_t N = last - first; int *result = new int[N], *result_current = result, *left_current = first, *right_current = middle; while (left_current < middle && right_current < last) { if (*left_current <= *right_current) { *result_current = *left_current; left_current++; } else { *result_current = *right_current; right_current++; } result_current++; } fill_with_remaining_elements(result_current, left_current, middle); fill_with_remaining_elements(result_current, right_current, last); for (std::size_t i = 0; i < N; i++) first[i] = result[i]; delete[] result; } result is a temporary buffer that contains the ordered array At the end of the function, result is restored in the original array and deleted
  • 79. Merge sort in C++: The merge function void fill_with_remaining_elements(int*& result_current, int* sub_list_current, int* sub_list_last) { while (sub_list_current < sub_list_last) { *result_current = *sub_list_current; sub_list_current++; result_current++; } }
  • 81. References  https://en.wikipedia.org/wiki/Sorting  https://en.wikipedia.org/wiki/Sorting_algorithm  https://en.wikipedia.org/wiki/Time_complexity  https://en.wikipedia.org/wiki/Big_O_notation  https://en.wikipedia.org/wiki/Bubble_sort  https://en.wikipedia.org/wiki/Merge_sort