Internal Sorting Algorithms
Internal Sorting Algorithms
ALGORITHMS AND
COMPLEXITY G
Prepared by: Maxil S. Urocay MSCS ongoing Prepared by: Maxil S. Urocay MSCS ongoing
1 2
WHAT IS SORTING?
- is a process that is required in many places and is
Prepared by: Maxil S. Urocay MSCS ongoing Prepared by: Maxil S. Urocay MSCS ongoing
3 4
1
2/15/25
5 6
Prepared by: Maxil S. Urocay MSCS ongoing Prepared by: Maxil S. Urocay MSCS ongoing
7 8
2
2/15/25
*each element is compared with adjacent element *if the array is sorted in descending order, every
multiple times. element needs to be moved to the other end.
Prepared by: Maxil S. Urocay MSCS ongoing Prepared by: Maxil S. Urocay MSCS ongoing
9 10
11 12
3
2/15/25
Prepared by: Maxil S. Urocay MSCS ongoing Prepared by: Maxil S. Urocay MSCS ongoing
13 14
Prepared by: Maxil S. Urocay MSCS ongoing Prepared by: Maxil S. Urocay MSCS ongoing
15 16
4
2/15/25
17 18
*the algorithms performs the same number of *when the list is in reverse order, selections sort
comparisons, and each pass perform n-1 also performs quadratic comparisons. The
comparisons for each element. algorithm still needs to perform n-1 comparisons
for each element.
Prepared by: Maxil S. Urocay MSCS ongoing Prepared by: Maxil S. Urocay MSCS ongoing
19 20
5
2/15/25
21 22
Prepared by: Maxil S. Urocay MSCS ongoing Prepared by: Maxil S. Urocay MSCS ongoing
23 24
6
2/15/25
25 26
*the algorithm needs to shift each element in the *happens when the list is sorted in reverse order.
sorted section multiple times to make room for
the element being inserted.
Prepared by: Maxil S. Urocay MSCS ongoing Prepared by: Maxil S. Urocay MSCS ongoing
27 28
7
2/15/25
29 30
Prepared by: Maxil S. Urocay MSCS ongoing Prepared by: Maxil S. Urocay MSCS ongoing
31 32
8
2/15/25
*occurs when the list is already sorted, and it still *will divide the list into halves and recursively sort
divides the list and merges the sorted halves, but them, performing a merge step at each level.
the time complexity remains O(n log n) because,
even if the data is sorted, the algorithm still needs
to divide the list into halves and merge the
results. Prepared by: Maxil S. Urocay MSCS ongoing Prepared by: Maxil S. Urocay MSCS ongoing
33 34
*whether the list is sorted, reverse sorted, or *is not an in-place sorting algorithm, so it requires
randomly ordered, merge sort always divides the additional space for the merging process.
list and merges the sublists in the same manner.
Prepared by: Maxil S. Urocay MSCS ongoing Prepared by: Maxil S. Urocay MSCS ongoing
35 36
9
2/15/25
Step 1 39 28 44 11 Step 3 28 39 11 41
Step 2 39 28 44 11
Step 4 11 28 39 41
Prepared by: Maxil S. Urocay MSCS ongoing Prepared by: Maxil S. Urocay MSCS ongoing
37 38
THANK OFFERS
PRODUCT YOU
45
10