## The Mastery of Computer Programming: Primary Algorithms - Sykalo Eugene 2023

# External sorting

Sorting and Searching

## Introduction to External Sorting

External sorting is a technique used to sort large data sets that cannot fit into main memory. When dealing with data sets that are too large to fit into memory, external sorting becomes necessary. In external sorting, the data set is broken up into smaller pieces that can fit into memory, sorted, and then merged back together.

External sorting is used in many applications, such as sorting large databases, sorting data for scientific analysis, and sorting data for business analytics. By using external sorting techniques, it is possible to sort data sets that are much larger than the available memory of the computer.

The basic idea behind external sorting is to divide the data set into smaller pieces that can fit into memory, sort these pieces, and then merge them back together. This process can be repeated until the entire data set is sorted.

One of the most popular algorithms used for external sorting is the merge-sort algorithm. In the merge-sort algorithm, the data set is divided into two halves, each half is sorted recursively, and then the two halves are merged back together. The merge-sort algorithm is well-suited for external sorting because it can easily merge two sorted lists into a single sorted list.

External sorting is a complex topic, and there are many different algorithms and techniques used to efficiently sort large data sets that cannot fit into main memory. By understanding the basics of external sorting and the algorithms used for it, you can gain a better understanding of how to handle large data sets and efficiently sort them.

## The Merge-Sort Algorithm

The merge-sort algorithm is a popular sorting algorithm that is well-suited for external sorting. In the merge-sort algorithm, the data set is divided into two halves, each half is sorted recursively, and then the two halves are merged back together.

The basic idea behind the merge-sort algorithm is to divide the data set into smaller and smaller pieces until each piece is a single element. These single-element pieces are then merged back together in a series of steps until the entire data set is sorted.

The merge-sort algorithm has a time complexity of O(n log n), which makes it a very efficient sorting algorithm for large data sets. It is also a stable sorting algorithm, which means that the order of equal elements is preserved after sorting.

One of the key advantages of the merge-sort algorithm is that it can easily merge two sorted lists into a single sorted list. This makes it well-suited for external sorting, where the data set is broken up into smaller pieces that can fit into memory, sorted, and then merged back together.

The merge-sort algorithm can be implemented in a variety of ways, but the basic steps are always the same:

- Divide the data set into two halves.
- Sort each half recursively.
- Merge the two sorted halves back together.

When merging two sorted lists, the merge-sort algorithm compares the first element of each list and selects the smaller of the two elements. This element is added to the output list, and the process is repeated until all elements from both lists have been added to the output list.

## External Merge-Sort

External merge-sort is a modified version of the merge-sort algorithm designed specifically for external sorting. In external merge-sort, the data set is divided into smaller pieces that can fit into memory, and each piece is sorted using an in-memory sorting algorithm such as quicksort or heapsort. The sorted pieces are then merged back together using a multiway merge algorithm.

The basic idea behind external merge-sort is to break up the data set into smaller pieces that can fit into memory, sort these pieces, and then merge them back together in a series of steps until the entire data set is sorted. This process is similar to the merge-sort algorithm, but with the added complexity of handling data sets that cannot fit into memory.

The external merge-sort algorithm has several advantages over other external sorting algorithms. First, it is very efficient and can handle very large data sets. Second, it can handle data sets with varying record lengths, which makes it well-suited for sorting data sets that contain records of different sizes. Finally, it is a stable sorting algorithm, which means that the order of equal elements is preserved after sorting.

The external merge-sort algorithm consists of several steps:

- Divide the data set into smaller pieces that can fit into memory.
- Sort each piece using an in-memory sorting algorithm such as quicksort or heapsort.
- Merge the sorted pieces back together using a multiway merge algorithm.

The multiway merge algorithm used in external merge-sort is a modified version of the merge algorithm used in the merge-sort algorithm. In the multiway merge algorithm, the sorted pieces are merged together in a series of steps, with multiple pieces being merged at each step. This allows for more efficient merging of larger data sets.

## Multiway Merge-Sort

Multiway merge-sort is an extension of the external merge-sort algorithm that allows for more efficient sorting of larger data sets. In external merge-sort, the data set is divided into smaller pieces that can fit into memory, and each piece is sorted using an in-memory sorting algorithm such as quicksort or heapsort. The sorted pieces are then merged back together using a multiway merge algorithm.

The basic idea behind multiway merge-sort is to extend the external merge-sort algorithm to allow for more than two pieces to be merged at a time. In the multiway merge-sort algorithm, the data set is divided into k smaller pieces, where k is the maximum number of pieces that can fit into memory at once. These pieces are then sorted using an in-memory sorting algorithm, and the sorted pieces are merged back together using a multiway merge algorithm.

The multiway merge algorithm used in multiway merge-sort is similar to the merge algorithm used in the merge-sort algorithm, but with the added complexity of handling multiple input lists. In the multiway merge algorithm, the k sorted input lists are merged together in a series of steps, with multiple input lists being merged at each step. This allows for more efficient merging of larger data sets.

The time complexity of multiway merge-sort depends on the number of pieces that can fit into memory at once. In general, the larger the value of k, the more efficient the algorithm will be. However, larger values of k also require more memory to be available.

## Replacement Selection

Replacement selection is a technique used in external sorting to select the minimum element from a set of elements that cannot fit into memory at once. The basic idea behind replacement selection is to select a small number of elements from the input set, sort them, and then use them to replace some of the elements in memory. This process is repeated until all of the elements in the input set have been processed.

The replacement selection algorithm consists of several steps:

- Read a block of elements from the input set into memory.
- Sort the block of elements using an in-memory sorting algorithm such as quicksort or heapsort.
- Select the smallest element from the block.
- Replace one of the elements in memory with the smallest element.
- Repeat steps 1-4 until all of the elements in the input set have been processed.

The replacement selection algorithm requires a buffer of memory to hold the elements that are being replaced. The size of the buffer is determined by the size of the blocks that are being read from the input set. The larger the size of the buffer, the more efficient the algorithm will be, but also the more memory it will require.

One of the key advantages of the replacement selection algorithm is that it can handle data sets with varying record lengths, which makes it well-suited for sorting data sets that contain records of different sizes. However, it is not a stable sorting algorithm, which means that the order of equal elements is not preserved after sorting.