Slide 7-8 - Sorting Algorithms
Slide 7-8 - Sorting Algorithms
Trong-Hop Do
University of Information Technology, HCM city
1
Sorting Algorithms
2
Comparison based sorting algorithms
Non-comparison based sorting algorithm
In-place and Stable sorting Algorithms
• A sorting algorithm is In-place if the algorithm does not use extra space for
manipulating the input but may require a small though nonconstant extra space for
its operation. Or we can say, a sorting algorithm sorts in-place if only a constant
number of elements of the input array are ever stored outside the array.
• A sorting algorithm is stable if it does not change the order of elements with the
same value.
• The sorting algorithm which will produce the first output will be known as stable
sorting algorithm because the original order of equal keys are maintained.
In-place and Stable sorting Algorithms
• A sorting algorithm is In-place if the algorithm does not use extra space for manipulating
the input but may require a small though nonconstant extra space for its operation. Or we
can say, a sorting algorithm sorts in-place if only a constant number of elements of the
input array are ever stored outside the array.
• A sorting algorithm is stable if it does not change the order of elements with the same
value.
Bubble Sort
First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two
elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in
order (8 > 5), algorithm does not swap them.
Bubble Sort
Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it
is completed. The algorithm needs one whole pass without any swap
to know it is sorted.
Bubble Sort
Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Bubble Sort
Bubble Sort
The above function always runs O(n^2) time even if the array is sorted. It can be
optimized by stopping the algorithm if inner loop didn’t cause any swap.
Bubble Sort
// An optimized version of Bubble Sort
void bubbleSort(int arr[], int n)
{
int i, j;
bool swapped;
for (i = 0; i < n-1; i++)
{
swapped = false;
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
swap(&arr[j], &arr[j+1]);
swapped = true;
}
}
// IF no two elements were swapped by inner loop, then break
if (swapped == false) break;
}
}
Bubble Sort
• Worst and Average Case Time Complexity: 𝑂(𝑛2 ). Worst case occurs
when array is reverse sorted.
• Best Case Time Complexity: O(n). Best case occurs when array is
already sorted.
• Auxiliary Space: O(1)
• Boundary Cases: Bubble sort takes minimum time (Order of n) when
elements are already sorted.
Sorting algorithms
• A={3,2,5,1,4}
• Sorting order is ascending
3 2 5 1 4
Selection Sort
i=0 j=1 2 3 4
3 2 5 1 4
min=0
Selection Sort
i=0 1 j=2 3 4
3 2 5 1 4
min=1
Selection Sort
i=0 1 2 j=3 4
3 2 5 1 4
min=1
Selection Sort
i=0 1 2 3 j=4
3 2 5 1 4
min=3
Selection Sort
i=0 1 2 3 j=4
3 2 5 1 4
min=3
Swap A[i] and A[min]
Selection Sort
0 i=1 j=2 3 4
1 2 5 3 4
min=1
Selection Sort
0 i=1 2 j=3 4
1 2 5 3 4
min=1
Selection Sort
0 i=1 2 3 j=4
1 2 5 3 4
min=1
Selection Sort
0 i=1 2 3 4
1 2 5 3 4
min=1
0 1 i=2 j=3 4
1 2 5 3 4
min=2
Selection Sort
0 1 i=2 3 j=4
1 2 5 3 4
min=3
Selection Sort
0 1 i=2 3 4
1 2 5 3 4
min=3
0 1 2 i=3 j=4
1 2 3 5 4
min=3
Selection Sort
0 1 2 i=3 4
1 2 3 5 4
min=3
Swap A[i] and A[min]
Selection Sort
0 1 2 3 4
1 2 3 4 5
End
Selection Sort
Unsorted: 3 2 5 1 4
Sorted: 1 2 3 4 5
Selection Sort
list = 64 25 12 22 11
• Second implementation is generally used when elements of the list are some kind of records
because in such a case data swapping becomes tedious and expensive due to the presence of a
//Implementation Method 1
void selectionSort(List A) {
Node *min, *i, *j;
i = A.pHead;
while (i) {
min = i; j = i->pNext;
while (j) {
if (j->info < min->info) min = j; j = j->pNext;
}
swap(i->info, min->info);
i = i->pNext;
}
}
Selection Sort
Method 2:
• Nodes are adjacent and the first node is the starting node.
• Nodes are adjacent and the first node isn’t the starting node.
• Nodes aren’t adjacent and the first node is the starting node.
• Nodes aren’t adjacent and the first node isn’t the starting node.
Selection Sort
//Implementation Method 2
void selectionSort(List &A) {
Node *qmin, *i, *j, *h; h = NULL; i = A.pHead;
while (i->pNext) {
qmin = h; j = i;
while (j->pNext) {
if (j->pNext->info < i->info) {qmin = j; i = qmin->pNext;}
j = j->pNext;
}
addAfter(A, removeAfter(A,qmin), h);
if (!h) h = A.pHead;
else h = h->pNext;
i = h->pNext;
}
}
Selection Sort
• The good thing about selection sort is it never makes more than O(n)
swaps and can be useful when memory write is a costly operation.
Selection Sort
Insertion sort is a simple sorting algorithm that works similar to the way you sort playing
cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from
the unsorted part are picked and placed at the correct position in the sorted part.
Algorithm
3: If the key element is smaller than its predecessor, compare it to the elements before.
Move the greater elements one position up to make space for the swapped element.
Insertion Sort
Insertion Sort
0 i=1 2 3 4
e=2 3 2 5 1 4
k=0
Insertion Sort
0 i=1 2 3 4
e=2 3 3 5 1 4
k=-1 Đưa phần tử e
vào vị trí k+1
Insertion Sort
0 1 i=2 3 4
e=5 2 3 5 1 4
k=1 Đưa phần tử e
vào vị trí k+1
Insertion Sort
0 1 2 i=3 4
e=1 2 3 5 1 4
k=2
Insertion Sort
0 1 2 i=3 4
e=1 2 3 5 5 4
k=1
Insertion Sort
0 1 2 i=3 4
e=1 2 3 3 5 4
k=0
Insertion Sort
0 1 2 i=3 4
e=1 2 2 3 5 4
k=-1 Đưa phần tử e
vào vị trí k+1
Insertion Sort
0 1 2 3 i=4
e=4 1 2 3 5 4
k=3
Insertion Sort
0 1 2 3 i=4
e=4 1 2 3 5 5
k=2 Đưa phần tử e
vào vị trí k+1
Insertion Sort
0 1 2 3 4
1 2 3 4 5
Insertion Sort
if (item == a[mid])
return mid + 1;
• 𝐴𝑗 < 𝐴𝑖 ∀ 𝑗 < 𝑖
• 𝐴𝑖 > 𝐴𝑖 ∀ 𝑗 > 𝑖
quickSort(A1), quickSort(A2)
A ← A1 ∪ {x} ∪ A2
Quick sort
Quick sort
• Implementing quick sort in the original method requires two array A1, A2
• Solution:
• This approach does not require mearging two sub array after sorting
Quick sort
if (b >= e) return;
int x = a[0], i = b, j = e;
while(i < j) {
}
Quick sort
b = 0, e = 4, x = 3
i=0 1 2 3 4 5 j=6
3 2 5 1 4 7 6
Quick sort
b = 0, e = 4, x = 3
i=0 1 2 j=3 4 5 6
3 2 5 1 4 7 6
Quick sort
b = 0, e = 4, x = 3
0 i=1 j=2 3 4 5 6
1 2 5 3 4 7 6
Quick sort
• b = 0, e = 4, x = 3
0 j=1 i=2 3 4 5 6
1 2 5 3 4 7 6
Quick sort
b = 0, e = 1, x = 1
i=0 j=1 2 3 4 5 6
1 2 5 3 4 7 6
Quick sort
b = 0, e = 1, x = 1
j=0
i=0 1 2 3 4 5 6
1 2 5 3 4 7 6
Quick sort
• b = 0, e = 1, x = 1
• j=-1 i=1 2 3 4 5 6
0
1 2 5 3 4 7 6
Quick sort
b = 2, e = 6, x = 5
0 1 i=2 3 4 5 j=6
1 2 5 3 4 7 6
Quick sort
b = 2, e = 6, x = 5
0 1 i=2 3 j=4 5 6
1 2 5 3 4 7 6
Quick sort
❖ x=5
j=3
0 1 2 i=3 4 5 6
1 2 4 3 5 7 6
Quick sort
• b = 2, e = 6, x = 5
0 1 2 i=3 j=4 5 6
1 2 4 3 5 7 6
Quick sort
b = 2, e = 3, x = 4
0 1 i=2 j=3 4 5 6
1 2 4 3 5 7 6
Quick sort
b = 2, e = 3, x = 4
0 1 i=2 j=3 4 5 6
1 2 4 3 5 7 6
Quick sort
• b = 2, e = 3, x = 4
0 1 j=2 i=3 4 5 6
1 2 3 4 5 7 6
Quick sort
b = 4, e = 6, x = 5
0 1 2 3 i=4 5 j=6
1 2 3 4 5 7 6
Quick sort
b = 4, e = 6, x = 5
j=6
0 1 2 3 i=4 5 6
1 2 3 4 5 7 6
Quick sort
• b = 4, e = 6, x = 5
0 1 2 j=3 4 • i=5 6
1 2 3 4 5 7 6
Quick sort
b = 5, e = 6, x = 7
0 1 2 3 4 i=5 j=6
1 2 3 4 5 7 6
Quick sort
b = 5, e = 6, x = 7
j=6
0 1 2 3 4 i=5 6
1 2 3 4 5 7 6
Quick sort
• b = 5, e = 6, x = 7
• □ Sorting two sub-array
0 1 2 3 j=4 5 i=6
1 2 3 4 5 7 6
Quick sort
b = 0, e = 6,
□ Result
0 1 2 3 4 5 6
1 2 3 4 5 7 6
Quick sort
• The first two terms are for two recursive calls, the last term is for the
partition process. k is the number of elements which are smaller than
pivot.
• The time taken by QuickSort depends upon the input array and
partition strategy. Following are three cases.
Quick sort
Quick sort
Quick sort
Merge sort
Merge sort
• It divides the input array into two halves, calls itself for the two
halves, and then merges the two sorted halves.
• The merge() function is used for merging two halves. The merge(arr,
l, m, r) is a key process that assumes that arr[l..m] and arr[m+1..r] are
sorted and merges the two sorted sub-arrays into one.
Merge sort
Merge sort
return
end if
partition(A,A1,A2)
mergeSort(A1)
mergeSort(A2)
merge(A1,A2,A)
Merge sort
Algorithm: (merge(A1 ,A2 ,A) )
A ← {}
while |A1|>0 and |A2|>0 do
if A1[0] < A2[0] then x ← A1[0], A1\{x}
else x ← A2[0], A2\{x} end if
A ← A ∪ {x}
end while
while |A1|>0 do
x ← A1[0], A1\{x} A ← A ∪ {x} end while
while |A2|>0 do
x ← A2[0], A2\{x} A ← A ∪ {x} end while
Merge sort
0 1 2 3 4 5 6
Sort A 3 2 5 1 4 7 6
A
3 5 4 6
A1
2 1 7
A
Merge sort
Sort A1
3 5 4 6
A11 3 4
A12 5 6
Merge sort
Sort A11 3 4
A111 3
A112 4
Merge sort
Merge A11
A111 3
A112 4
A11 3 4
Merge sort
Sort A12 5 6
A121 5
A122 6
Merge sort
Sort A12
A121 5
A122 6
A12 5 6
Merge sort
Merge A1
A11
3 4
A12 5 6
A1 3 4 5 6
Merge sort
Merge A
A1 3 4 5 6
A2 1 2 7
A 1 2 3 4 5 6 7
Merge sort
if (!A.pHead) return;
partition(A,A1,A2);
mergeSort(A1);
mergeSort(A2);
}
Merge sort
while (A1.pHead) {
p = A1.pHead; A1.pHead = p->pNext;
p->pNext = NULL;
addTail(A, p);
}
while (A2.pHead) {
p = A2.pHead; A2.pHead = p->pNext;
p->pNext = NULL;
addTail(A, p);
}
}
Merge sort
• Binary Heap
Heap sort
2. At this point, the largest item is stored at the root of the heap.
Replace it with the last item of the heap followed by reducing the size