Sorting Group 5
Sorting Group 5
ALGORITHM
(SORTING ALGORITHM)
GROUP 5 MEMBERS
1. Robiu Olalere 21/52HA104
2. Amodu Ayomide Matthew 21/52HA043
3. Amaogu Faith Ihedinma 21/52HA042
4. Awarun David Ayotomiwa 21/52HA047
5. Aliu Ibrahim Hammed 21/52HA041
6. Ayinla Fathiu Iyanuoluwa 21/52HA049
7. Ayeni Divine-gift Adewale 21/52HA048
8. Asuku Anavaami David 21/52HA046
9. Bolarinwa Mubarak Ajibola
21/25PJ018
10. Ayoola Israel Muyiwa 21/52HA050
11. Aribidesi Abodunrin mariyam 21/25PJ016
12. Babalola Olawale Abdulqaadir 21/52HA051
13. Amoo Oluwafemi Abraham 21/52HA045
14. Artemas Hyelda maina 21/25pj017
SORTING ALGORITHM
Sorting is a technique to rearrange the elements of a list in ascending or descending order, which can be numerical, lexicographical, or any user-
defined.
Why Study Sorting.
When an input is sorted, many problems become easy(e.g.. Searching, Min, Max, K-th Smallest), Sorting has a variety of interesting algorithmic
solutions that embody many ideas.
Iterative
Recursive
Divide and conquer
Best/worst/Average case Bounds
Randomized Algorithm
Application of Sorting
• Uniqueness testing
• Deleting duplicates
• Prioritizing events
• Frequency counting
• Reconstructing the original order
• Set inter section/union
CLASSIFICATION OF SORTING
Internal Sorting: If all the data that is to be stored can be accommodated at a time in
memory, it is internal sorting, it uses only the primary memory. There is a limitation for
internal sorts, they can only process relatively small lists due to memory. E.g. Selection
sort(Selection and Heap sort Algorithm), insertion(Insertion and Shell sort Algorithm),
Exchange(Bubble and Quick sort Algorithm)
External Sorting: Sorting large amount of data requires memory. This process
uses external memory to store the data which is not fit into the main
memory. All external sorts are based on process of merging. Different parts
of data are sorted separately and merged together. E.g. Merge sort.
TERMINOLOGIES
Merge sort is based on the divide and conquer paradigm that splits the data into two
halves, recursively sorts them and then merges them back together in sorted order.
Merge sort has a time complexity of O(n log n), making it suitable for large datasets
Best, Average, worst case: O(nLogn)
Merge sort is a stable sorting algorithm, meaning that equal elements maintain their
original order.
Merge sort can be parallel, making it a good choice for multi-core processors.
Real world application in database sorting, file system sorting and data analysis
Merge sort has a more complex implementation compared to other sorting algorithms.
Merge sort requires additional memory to store the merged list.
ALGORITHM & EXAMPLES
//list(0 to m-1)
//list(m to list.count-1)
SELECTION SORT
Selection sort is a simple sorting algorithm that works by repeatedly finding the minimum
element from the unsorted part of the list and swapping it with the first unsorted element.
it divides the list into two parts: the sorted portion and the unsorted portion. It iteratively
selects the smallest element from the unsorted portion and swaps it with the first element of
that portion.
Selection Sort can disrupt the relative order of equal elements if swapping occurs.
Selection Sort is simple but inefficient compared to algorithms like Quick Sort or Merge Sort,
especially for large datasets. However, it can be a good choice for small lists or situations
where memory usage is a constraint.
First element: Choose the first element of the list as the pivot.
Last element: Choose the last element of the list as the pivot.
Median of three: Choose the median of the first, middle, and last elements as the pivot.
Random pivot: Choose a random element from the list as the pivot.
ALGORITHM & EXAMPLES
HEAP SORT
Heap sort is a comparison-based algorithm that utilizes a binary heap data structure, it first builds a heap
from the input list, then repeatedly extracts the maximum or minimum element from the heap and places it
into the sorted portion of the list. The heap is reconstructed after each extraction.
Heapify is the process of maintaining the heap property after insertion or deletion of an element, it involves
comparing the element with it’s children and swapping if necessary
Heap Sort is efficient for large datasets with guaranteed O(nlogn) performance, but it is not stable and
involves more complexity in implementation compared to simpler algorithms like Bubble Sort or Insertion
Sort.
• Best Case: O(nlogn)
• Average Case: O(nlogn)
• Worst Case: O(nlogn)
• A max-heap is a binary tree where the parent node is greater than or equal to its children.
ALGORITHM & EXAMPLES
algorithm HeapSort(list)
2) Pre: list ≠ ∅
3) Post: list has been sorted into values of ascending order
4) BuildMaxHeap(list)
5) for i ← list.Count - 1 to 1 step -1
6) Swap(list[0], list[i]) // Swap the root (max) with the last element
7) Heapify(list, 0, i) // Rebuild the heap for the reduced heap
8) end for
9) return list
10) end HeapSort
algorithm BuildMaxHeap(list)
1) for i ← list.Count / 2 - 1 down to 0
2) Heapify(list, i, list.Count)
3) end for
4) end BuildMaxHeap
algorithm Heapify(list, i, heapSize)
1) largest ← i
2) left ← 2 * i + 1
3) right ← 2 * i + 2
4) if left < heapSize and list[left] > list[largest]
5) largest ← left
6) end if
7) if right < heapSize and list[right] > list[largest]
8) largest ← right
9) end if
10) if largest ≠ i
11) Swap(list[i], list[largest])
12) Heapify(list, largest, heapSize) // Recursively ensure the heap property
13) end if
14) end Heapify
COMPARISON WITH OTHER SORTING ALGORITHM
REAL LIFE USE CASES BY INDUSTRIES
SUMMARY
Sorting algorithms organize data into a specific order, with various methods suited to different
needs:
Bubble Sort: Simple but slow; great for small datasets.
Insertion Sort: Efficient for small or nearly sorted data.
Selection Sort: Memory-efficient but slow; ideal for constrained environments.
Merge Sort: Fast and stable, perfect for large datasets and external sorting.
Quick Sort: Highly efficient in-memory sort; widely used but not stable.
Shell Sort: A faster variation of Insertion Sort for medium-sized data.
Heap Sort: Memory-efficient and reliable for large datasets.
Radix Sort: Non-comparative, ideal for numbers or fixed-length data.
Applications: E-commerce sorting (Quick Sort), task scheduling (Heap Sort), database
indexing (Merge Sort), and more. Choose based on dataset size, stability needs, and memory
constraints.
THANK YOU
ARIGATO GOZAIMOSU