Differentiate between internal sorting and external sorting.


Q.) Differentiate between internal sorting and external sorting.

Subject: data structures

Internal Sorting

  • Internal sorting algorithms operate entirely within the main memory of the computer.
  • The entire data set is loaded into the memory before the sorting process begins.
  • Internal sorting is efficient for small to medium-sized data sets that can fit entirely in the main memory.
  • Common internal sorting algorithms include:
    • Bubble Sort: Compares adjacent elements and swaps them if they are in the wrong order.
    • Selection Sort: Finds the minimum element from the unsorted portion and swaps it with the leftmost unsorted element.
    • Insertion Sort: Builds the sorted list one element at a time by inserting each unsorted element into its correct position in the sorted portion.
    • Merge Sort: Divides the list into smaller sublists, sorts them recursively, and then merges them back together.
    • Quick Sort: Selects a pivot element, partitions the list into two sublists based on the pivot, and recursively sorts the sublists.

External Sorting

  • External sorting algorithms are used for sorting large data sets that cannot fit entirely in the main memory.
  • The data set is divided into smaller chunks or blocks that can fit in the memory.
  • These blocks are sorted individually using an internal sorting algorithm.
  • The sorted blocks are then merged together to obtain the final sorted data set.
  • External sorting is slower than internal sorting due to the additional disk input/output operations required to access the data blocks.
  • Common external sorting algorithms include:
    • Merge Sort: Divides the data into blocks, sorts each block internally, and then merges them back together in sorted order.
    • Quicksort: Partitions the data into blocks, sorts each block internally, and then recursively sorts the blocks.
    • Polyphase Merge Sort: Divides the data into multiple sorted runs, merges them together, and repeats the process until the entire data set is sorted.

Comparison:

Feature Internal Sorting External Sorting
Data Size Small to medium Large
Memory Usage Entire data set fits in memory Data is divided into blocks that fit in memory
Efficiency Faster Slower
Common Algorithms Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, Quick Sort Merge Sort, Quicksort, Polyphase Merge Sort
Applications Sorting small to medium-sized data sets in-memory Sorting large data sets that cannot fit in memory