group 4 presentation
group 4 presentation
Sorting
Algorithms
Terminology
What is Sorting?
Sorting Terminology
• In-place Sorting: An in-place sorting algorithm uses constant space for producing the output
(modifies the given array only) or copying elements to a temporary storage. Examples: Selection
Sort, Bubble Sort Insertion Sort and Heap Sort.
• Internal Sorting: Internal Sorting is when all the data is placed in the main
• memory or internal memory. In internal sorting, the problem cannot take input beyond its size.
Example: heap sort, bubble sort, selection sort, quick sort, shell sort, insertion sort.
• External Sorting : External Sorting is when all the data that needs to be sorted cannot be
placed in memory at a time, the sorting is called external sorting. External Sorting is used for the
massive amount of data. Examples: Merge sort, Tag sort, Polyphase sort, Four tape sort, External
radix sort, etc.
• Stable sorting: When two same items appear in the same order in sorted data as in the
original array called stable sort. Examples: Merge Sort, Insertion Sort, Bubble Sort.
CHARACTERISTICS OF SORTING ALGORITHM
• Auxiliary Space : This is the amount of extra space (apart from input array)
needed to sort. For example, Merge Sort requires O(n) and Insertion Sort O(1)
auxiliary space
• In-Place Sorting: An in-place sorting algorithm is one that does not require
additional memory to sort the data. This is important when the available
memory is limited or when the data cannot be moved.
• Data management: Sorting data makes it easier to search, retrieve, and analyze.
For example the order by operation in SQL queries requires sorting.
• Machine learning: Sorting is used to prepare data for training machine learning
models.
• Operating Systems: Sorting algorithms are used in operating systems for tasks
Bubble Sort
Overview
Algorithm
Consider that we want to sort a list in ascending order, here are the steps that
the algorithm would follow:
3.If the current element is greater than the next element, then swap both the
elements. If not, move to the next element.
To better understand bubble sort, recall the list that contains the elements 5,
3, 4, 2 initially.
Example
Pseudocode
function bubbleSort(arr):
n = length(arr)
for i = 0 to n-1:
for j = 0 to n-i-2:
if arr[j] > arr[j+1]:
swap(arr[j], arr[j+1])
Time Complexity
• The time complexity of bubble sort in the best-case scenario is O(n)
when the list is already sorted. In the average and worst-case scenarios,
it is O(n²) due to the nested loops.
Space Complexity
• Bubble sort is an in-place sorting algorithm, so the space complexity is
O(1).
Stability
• Bubble sort is a stable sorting algorithm since it does not change the
relative order of equal elements.
Optimized Bubble Sort Algorithm
Imagine the case where the list is already sorted. For example, our input list contains 2, 3, 4, 5 instead of 5, 3,
4, 2.
In the above algorithm, the loop would still run to compare all the elements. It might cause complex issues like
longer execution times.
> The value of the swap is set to true if, during any iteration, swapping was done.
> After an iteration, if the value of swapped is found to be false, it means the array is sorted, and no more
comparisons are required.
It will help in reducing the number of comparisons, and hence the execution time of the algorithm.
Pseudocode
Overview
Algorithm
mergeSort(leftHalf)
mergeSort(rightHalf)
i=j=k=0
• The time complexity of merge sort is O(n log n) in all cases (best,
average, and worst) since it divides the list into two halves and
merges them.
Space Complexity
Stability