0% found this document useful (0 votes)
61 views10 pages

Sorting Algorithms: Bubble & Merge Sort

Algorithms are step-by-step plans for solving problems. Sorting algorithms rearrange elements in ascending or descending order. Bubble sort is a simple sorting algorithm that repeatedly compares adjacent elements and swaps them if out of order. Merge sort is more efficient and uses a divide and conquer approach to recursively split the list in half until individual elements, then merges the sorted halves back together. It has average time complexity of O(n log n).

Uploaded by

desh15
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views10 pages

Sorting Algorithms: Bubble & Merge Sort

Algorithms are step-by-step plans for solving problems. Sorting algorithms rearrange elements in ascending or descending order. Bubble sort is a simple sorting algorithm that repeatedly compares adjacent elements and swaps them if out of order. Merge sort is more efficient and uses a divide and conquer approach to recursively split the list in half until individual elements, then merges the sorted halves back together. It has average time complexity of O(n log n).

Uploaded by

desh15
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

D Learning Academy by Deshani Jayasuriya

Algorithms
Algorithms are step-by-step plans for solving problems. Algorithms can be designed using pseudo-code,
flowcharts, written descriptions and program code. There are also some standard algorithms for searching and
sorting.

What are sorting algorithms


• Rearranging the elements of a list in ascending or descending order is called sorting.
• Sorting algorithms are based on comparing values or some may be based on analyzing properties
of values.
• For example, if you know that your list only holds values from 0 to 9, you can just count how
many of each in one sweep (O(n)) and construct the sorted list.
Examples of sorting applications:

• a directory of files sorted by name or date


• bank checks sorted by account #
• addresses in a mailing list sorted by zip code
• hits found by a search engine sorted by relevance
• credit card transactions sorted by date
Sorting Categories

• Sorting by Insertion-> insertion sort, shellsort

• Sorting by Exchange-> bubble sort, quicksort

• Sorting by Selection-> selection sort, heapsort

• Sorting by Merging-> merge sort

• Sorting by Distribution-> radix sort

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are
in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity
is quite high.

• Compares adjacent array elements

– Exchanges their values if they are wrong order

• Smaller values bubble up to the top of the array

– Larger values sink to the bottom

2 9 5 4 8 1 2 5 4 8 1 9 2 4 5 1 8 9 2 4 1 5 8 9 1 2 4 5 8 9
2 5 9 4 8 1 2 4 5 8 1 9 2 4 5 1 8 9 2 1 4 5 8 9
2 5 4 9 8 1 2 4 5 8 1 9 2 4 1 5 8 9
2 5 4 8 9 1 2 4 5 1 8 9
2 5 4 8 1 9

(a) 1st pass (b) 2nd pass (c) 3rd pass (d) 4th pass (e) 5th pass

1
D Learning Academy by Deshani Jayasuriya

How does Bubble Sort Work?

Input: arr[] = {5, 1, 4, 2, 8}

First Pass:
• Bubble sort starts with very first two elements, comparing them to check which one is greater.
• ( 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.
Second Pass:
• Now, during second iteration it should look like this:
• ( 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 )
Third Pass:
• 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.
• ( 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 )
Illustration:

2
D Learning Academy by Deshani Jayasuriya
Recommended Problem

3
D Learning Academy by Deshani Jayasuriya
Merge sort

A merge sort is a more complex sort, but also a highly efficient one. A merge sort uses a technique called
Divide and Conquer. The list is repeatedly divided into two until all the elements are separated
individually. Pairs of elements are then compared, placed into order and combined. The process is then
repeated until the list is recompiled as a whole.

Merge Sort: Idea

• Divide: divide the n-element sequence to be sorted into two subsequences of n/2 elements each
• Conquer: sort the two subsequences recursively using merge sort
• Combine: merge the two sorted subsequences to produce the sorted answer

2 9 5 4 8 1 67
split
2 9 5 4 8 1 6 7
split divide
2 9 5 4 8 1 6 7
split
2 9 5 4 8 1 6 7
merge
2 9 4 5 1 8 6 7
conqure
merge
2 4 5 9 1 6 7 8

merge
1 2 4 5 6 7 89

4
D Learning Academy by Deshani Jayasuriya
• Merge Algorithm
– Access the first item from both sequences
– While not finished with either sequence
• Compare the current items from the two sequences, copy the smaller current item to
the output sequence, and access the next item from the input sequence whose item
was copied
– Copy any remaining items from the first sequence to the output sequence
– Copy any remaining items from the second sequence to the output sequence

Consider this unsorted list:

The list is split into half:

The process repeats:

Until all elements are individually separated:

5
D Learning Academy by Deshani Jayasuriya
The algorithm looks at the individual elements and compares them as pairs. Each pair is sorted into
order:

The pairs are then compared, starting with the first number in each pair. If the left-hand number is
smaller than the right-hand number, it is placed in order. The comparison then moves up to the second
number on the left-hand side and the process repeats. If the right-hand number is smaller, it is placed in
order and the comparison moves to the next number on that side.

Here, 7 is the first left hand number and 5 is the first right hand number. 7 is bigger than 5, so 5 is placed
in order:

5
The next right-hand number is 10. 7 is smaller than 10, so 7 is placed in order:
5 7
The next left-hand number is 11. 11 is bigger than 10, so 10 is placed in order:
5 7 10
There are no more right-hand numbers to compare, so the remaining left-hand numbers are placed in
order:
5 7 10 11
The process is repeated for the initial right-hand division:

Eventually the list is recompiled:

The list is now sorted into the correct order.

The Merge Sort algorithm is a sorting algorithm that is based on the Divide and Conquer paradigm. In this
algorithm, the list of numbers is initially divided into two equal halves and then they are combined in a sorted
manner.

The first stage is where the list is split until it forms individual elements called sub-lists. After this, the 'merge'
stage begins. Here the sub-lists are paired up and are arranged according to the order stated
(ascending/descending). these paired lists are paired again to form groups of 4 and are again arranged according
to the intended/ given order. This process happens until the sub-lists form one list.

6
D Learning Academy by Deshani Jayasuriya
Merge Sort Working Process:

Think of it as a recursive algorithm continuously splits the array in half until it cannot be further divided. This
means that if the array becomes empty or has only one element left, the dividing will stop, i.e. it is the base case
to stop the recursion. If the array has multiple elements, split the array into halves and recursively invoke the
merge sort on each of the halves. Finally, when both halves are sorted, the merge operation is applied. Merge
operation is the process of taking two smaller sorted arrays and combining them to eventually make a larger one.

Illustration:

To know the functioning of merge sort, lets consider an array arr[] = {38, 27, 43, 3, 9, 82, 10}

• At first, check if the left index of array is less than the right index, if yes then calculate its mid-point

Now, as we already know that merge sort first divides the whole array iteratively into equal halves, unless the
atomic values are achieved.

• Here, we see that an array of 7 items is divided into two arrays of size 4 and 3 respectively.

• Now, again find that is left index is less than the right index for both arrays, if found yes, then again calculate
mid points for both the arrays.

• Now, further divide these two arrays into further halves, until the atomic units of the array is reached and further
division is not possible.

7
D Learning Academy by Deshani Jayasuriya
• After dividing the array into smallest units, start merging the elements again based on comparison of size of
elements
• Firstly, compare the element for each list and then combine them into another list in a sorted manner.

• After the final merging, the list looks like this:

The following diagram shows the complete merge sort process for an example array {38, 27, 43, 3, 9, 82, 10}.

If we take a closer look at the diagram, we can see that the array is recursively divided into two halves till the size
becomes 1. Once the size becomes 1, the merge processes come into action and start merging arrays back till the
complete array is merged.

8
D Learning Academy by Deshani Jayasuriya
Recursive steps of merge sort

Algorithm:

step 1: start
step 2: declare array and left, right, mid variable
step 3: perform merge function.
if left > right
return
mid= (left+right)/2
mergesort(array, left, mid)
mergesort(array, mid+1, right)
merge(array, left, mid, right)
step 4: Stop
Merge sort, advantages and disadvantages

9
D Learning Academy by Deshani Jayasuriya

Exercises
[Link] the following exercises using Bubble sort

5 2 1 3 7 4

2 3 1 5 6 4

1 4 3 6 2 5

6 5 1 3 2 4

[Link] is the use of pass in bubble sort?

[Link] 3 types of sorting techniques and briefly explain them.

10

Common questions

Powered by AI

Bubble Sort and Merge Sort differ significantly in their approach and efficiency. Bubble Sort operates by repeatedly comparing adjacent elements and swapping them if they are in the wrong order. It continuously makes passes through the data until no swaps are needed, which indicates that the array is sorted . This leads to an average and worst-case time complexity of O(n^2), making it inefficient for large datasets. In contrast, Merge Sort uses a 'divide and conquer' strategy, dividing the array into smaller sub-arrays, sorting them, and then merging them back together. This results in a time complexity of O(n log n), making it more efficient for larger datasets . Merge Sort also requires additional space for its divide and conquer operations, unlike Bubble Sort, which sorts in place .

The choice between a sorting algorithm with low space complexity and one with better time complexity involves significant trade-offs. Algorithms like Bubble Sort offer in-place sorting with O(1) space complexity, appealing for memory-constrained environments . However, they have a higher time complexity (O(n^2)), which may not be acceptable for large datasets. In contrast, Merge Sort provides a stable time complexity of O(n log n) suited for large-scale sorting but incurs additional space overhead due to auxiliary storage needs for its divide-and-merge operations . The trade-off often depends on the specific constraints and priorities of the scenario: if minimizing memory use is paramount, a more space-efficient sorting might be chosen despite performance slows; conversely, if execution speed is crucial, accepting a larger memory footprint might be preferred . Understanding the context and system constraints is vital in algorithm selection to balance these factors effectively .

Merge Sort is preferred over Bubble Sort for sorting larger datasets due to its efficiency and scalability. Merge Sort offers a consistent time complexity of O(n log n) across all cases, making it suitable for substantial datasets, whereas Bubble Sort has a time complexity of O(n^2), which becomes inefficient as data sizes grow . Merge Sort, through its divide and conquer method, ensures each element is processed and merged optimally, unlike Bubble Sort which involves unnecessary repeated passes for large collections . Additionally, Merge Sort efficiently handles data that doesn't fit into memory, making it ideal for external sorting involving large amounts of data .

Recursion in the Merge Sort algorithm is integral to its divide and conquer methodology. Each recursive split divides the array until sub-arrays of size one are achieved . On large datasets, recursion effectively manages the breaking down of data into manageable pieces, facilitating easier sorting and merging . However, recursion also has implications for memory usage, as each recursive call adds a new stack frame, increasing stack size and potentially leading to stack overflow for extremely large datasets if not adequately managed . This needs sufficient stack space or an iterative alternative for environments with stack size limitations. Despite this, the controlled depth (logarithmic relative to input size) generally makes recursion manageable, with efficient use in large-scale applications . Thus, understanding the trade-offs in memory and compute resources is crucial when applying Merge Sort to large datasets .

Bubble Sort exhibits adaptive behavior by potentially reducing the number of passes needed when the array is already nearly sorted. In the best-case scenario, Bubble Sort can achieve a time complexity of O(n) if the array is already sorted, because it would require only one full pass without any swaps to confirm the order . This adaptation is due to the algorithm's ability to terminate early when no more swaps are needed after a single pass. However, its worst-case scenario remains O(n^2), reflecting its inefficiency when dealing with randomly ordered data or larger datasets .

The 'merge' operation is crucial in the Merge Sort algorithm as it is responsible for combining sorted sub-arrays to produce a single, sorted array. After the initial phase of repeatedly dividing the array into halves until individual elements are achieved, the merge operation begins . During merging, the algorithm compares the smallest elements of each sub-array and places the smaller element into the output array. This process is repeated, copying the remaining elements from each sub-array into the output array . The merge step ensures that each pair of divided arrays are combined in order while maintaining the overall efficiency of the algorithm. This operation results in a sorted sequence, with the time complexity of O(n), which is efficiently layered on top of the recursive depth of the sort, maintaining the overall complexity of O(n log n).

Merge Sort implements the divide and conquer paradigm by recursively splitting the array into two halves until sub-arrays of size one are reached, marking the base case . Each sub-array is then individually sorted and combined in the 'merge' step by comparing and arranging elements in order . By dividing the problem into sub-problems, solving them independently, and then combining the results efficiently, Merge Sort achieves a time complexity of O(n log n). The logarithmic division depth corresponds to the number of times the array is halved, while the linear merge step complexity integrates these sorted sub-arrays into a single sorted array . This gives Merge Sort its characteristic efficiency, especially compared to simpler methods like Bubble Sort .

In Bubble Sort, 'passes' refer to the full iterations through the array where adjacent elements are compared and possibly swapped to ensure smaller elements 'bubble' to the beginning of the array while larger elements 'sink' to the end . The sorting process continues until a pass completes with no swaps, indicating that the array is sorted . This reliance on multiple passes, often as many as n in the worst case for an array of n elements, contributes to its inefficiency, with a time complexity of O(n^2). The necessity of unnecessary passes, especially if sorted earlier, affects performance, making it unsuitable for large data sets .

Different types of sorting algorithms are important in practice because they offer varied performance characteristics suited to different use cases and data types. Insertion sorts, like insertion sort and shellsort, can be quick for small arrays or nearly sorted data. Exchange sorts, such as bubble sort and quicksort, suit applications where memory usage is a constraint. Selection sorts, including heapsort and selection sort itself, provide deterministic performance and are easy to implement for moderate-sized datasets. Merging methods like merge sort, with their O(n log n) efficiency and stability, handle large datasets efficiently. Distribution sorts, for instance, radix sort, are valuable for specialized cases where data properties allow for unique sorting approaches, achieving linear complexity in favorable situations . By understanding the strengths and limitations of each type, developers can select the most effective sorting approach depending on dataset size, initial order, and performance requirements .

The stability of a sorting algorithm, where equal elements retain their relative positions post-sorting, has significant implications for its application in the real world. Stability is crucial when sorting records by multiple fields; for instance, if records are sorted by surname and then by first name, a stable algorithm ensures that names remain in the initial order when only surnames are sorted. Stable algorithms like merge sort are preferred for maintaining data integrity where secondary data properties matter . In contrast, unstable algorithms might disrupt these properties, making them less suitable for tasks requiring multi-level sorting based on correlated data . Stability ensures predictability in sorting outcomes, critical in applications like databases, where accurate data correlations must be preserved. .

You might also like