0% found this document useful (0 votes)
35 views24 pages

Sorting Algorithms Overview and Methods

The document provides an overview of sorting algorithms, including definitions and classifications such as internal and external sorting. It details specific algorithms like Merge Sort, Insertion Sort, and Shell Sort, explaining their mechanisms and implementations. Additionally, it includes links for further resources on sorting algorithms.

Uploaded by

advika.sagar
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

Topics covered

  • Sorting Criteria,
  • File Sorting,
  • Sorting Insights,
  • Merge Sort,
  • Sorting Order,
  • Sorting Principles,
  • Sorting Applications,
  • Sorting,
  • Shell Sort,
  • External Sorting
0% found this document useful (0 votes)
35 views24 pages

Sorting Algorithms Overview and Methods

The document provides an overview of sorting algorithms, including definitions and classifications such as internal and external sorting. It details specific algorithms like Merge Sort, Insertion Sort, and Shell Sort, explaining their mechanisms and implementations. Additionally, it includes links for further resources on sorting algorithms.

Uploaded by

advika.sagar
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

Topics covered

  • Sorting Criteria,
  • File Sorting,
  • Sorting Insights,
  • Merge Sort,
  • Sorting Order,
  • Sorting Principles,
  • Sorting Applications,
  • Sorting,
  • Shell Sort,
  • External Sorting

Data Structures

Div B
SY EXCP
Sem III
Dr. Rajashree Daryapurkar
2023-24
Sorting Algorithms
Sorting
• Sorting means arranging the elements of an
array so that they are placed in some relevant
order which may be either ascending or
descending
• A sorting algorithm is defined as an algorithm
that puts the elements of a list in a certain
order, which can be either numerical order,
alphabetical order, or any user-defined order.
Sorting
• Internal sorting which deals with sorting the
data stored in the computer’s memory
• External sorting which deals with sorting the
data stored in files.
• External sorting is applied when there is
voluminous data that cannot be stored in the
memory.
Sorting
• Merge Sort is a Divide and Conquer algorithm.
• It divides input array in two halves, calls itself for
the two halves and then merges the two sorted
halves.
• The merge() function is used for merging two
halves. The merge(arr, l, m, r) is key process that
assumes that arr[l..m] and arr[m+1..r] are sorted
and merges the two sorted sub-arrays into one.
Merge Sort
Merge Sort
Merge Sort
Merge Sort
Merge Sort
Merge Sort
Merge sort implementation
MergeSort(arr[], l, r)
If l < r
1. Find the middle point to divide the array into two
halves: middle m = l+(r-l)/2
2. Call mergeSort for first half: Call mergeSort(arr, l,
m)
3. Call mergeSort for second half: Call mergeSort(arr,
m+1, r)
4. Merge the two halves sorted in step 2 and 3: Call
merge(arr, l, m, r)
Insertion Sort
Insertion sort is a simple sorting algorithm that
works the way we sort playing cards in our hands.
Algorithm
// Sort an arr[] of size n
insertionSort(arr, n)
Loop from i = 1 to n-1.
……a) Pick element arr[i] and insert it into sorted
sequence arr[0…i-1]
Algorithm for insertion sort
• INSERTION-SORT (ARR, N)
Step 1: Repeat Steps 2 to 5 for K = 1 to N – 1
Step 2: SET TEMP = ARR[K]
Step 3: SET J = K - 1
Step 4: Repeat while TEMP <= ARR[J]
SET ARR[J + 1] = ARR[J]
SET J = J - 1
[END OF INNER LOOP]
Step 5: SET ARR[J + 1] = TEMP
[END OF LOOP]
Step 6: EXIT
Shell Sort
• ShellSort is mainly a variation of Insertion
Sort. In insertion sort, we move elements only
one position ahead.
• When an element has to be moved far ahead,
many movements are involved.
• The idea of shellSort is to allow exchange of
far items.
Shell Sort
Shell Sort
Shell Sort
Shell Sort
Shell Sort
Links for sorting algorithms
• [Link]

Common questions

Powered by AI

Shell Sort improves upon Insertion Sort by allowing the exchange of elements that are far apart, reducing the total number of movements needed. It does this by initially sorting the array with a large gap and then reducing the gap size incrementally until it becomes 1, at which point it becomes a standard Insertion Sort. This gradual reduction of the gap helps to provide a more efficiently sorted dataset before the final pass, greatly improving performance over standard Insertion Sort for larger datasets .

The primary computational difference between Merge Sort and Insertion Sort lies in their time complexities. Merge Sort has a time complexity of O(n log n) due to its divide-and-conquer approach that repeatedly divides the array and merges sorted subarrays. In contrast, Insertion Sort typically has a worst-case time complexity of O(n^2) because each insertion can potentially move each element to the beginning of the list. However, Insertion Sort can perform better (O(n)) on nearly sorted arrays, unlike Merge Sort, which maintains consistent performance irrespective of initial order .

Internal sorting algorithms are used for sorting data that can fit entirely into the computer’s memory, whereas external sorting algorithms are applied to data that is too large to fit into memory and is instead stored in external files. Internal sorting is generally faster as it relies solely on memory operations, whereas external sorting involves I/O operations, making it slower but necessary for large datasets .

Shell Sort is generally more efficient than straightforward algorithms like Bubble Sort and even basic Insertion Sort for large datasets due to its initial sorting with large gaps that reduce disorder faster. When handling datasets with fewer initially sorted elements, Shell Sort also outperforms due to these gap-based preliminary sorting passes. However, it might still be less efficient than advanced algorithms like Quick Sort or Merge Sort, although it uses less memory than Merge Sort and has comparable or better cache performance, benefiting certain data configurations .

Insertion Sort would be more advantageous in scenarios where the list is already mostly sorted, as it runs in linear time in such cases. It is also useful for sorting small datasets due to its simplicity and low overhead. Additionally, Insertion Sort is adaptive, meaning it reduces the total number of comparisons needed when the array is partially sorted .

The insertion method of Insertion Sort resembles sorting playing cards by hand because both involve selecting an item and placing it in its correct position relative to already sorted items. As in card sorting, where each card is picked and inserted in the correct place amongst already sorted cards, Insertion Sort picks each element from the unsorted portion of the array and inserts it into the correct position within the sorted portion. This comparison highlights the adaptive, item-by-item insertion approach that is intuitive yet effective in certain scenarios .

Potential drawbacks of using Merge Sort for large datasets include its high space complexity due to the use of temporary arrays for merging. This issue can lead to increased memory usage, which can be problematic in memory-constrained environments. These drawbacks can be mitigated by implementing an in-place variant of Merge Sort, or by optimizing the merge process to use less additional memory when feasible. However, this comes at the cost of increased algorithmic complexity .

Merge Sort uses the divide and conquer strategy by recursively dividing the array into two halves until each sub-array contains a single element. The significance of the merge function is that it takes two sorted sub-arrays and combines them into one sorted array. This merging process is crucial as it reconstructs the sorted order from divided parts. The merge function ensures that at each merge step, the sub-arrays are combined in sorted order, making the entire array sorted when the recursion unravels .

The space complexity of Merge Sort, which is O(n) due to the auxiliary arrays used for merging, can affect its practicality for applications where memory is limited, such as embedded systems or devices with constrained memory. To alleviate these concerns, alternative strategies include using iterative or in-place versions of Merge Sort, which reduce additional space usage, or opting for other algorithms like Quick Sort or Heap Sort that generally use less auxiliary space. These alternative strategies come with trade-offs in terms of implementation complexity or time complexity under specific conditions .

The main challenges of implementing the merge function in Merge Sort include efficiently combining the two sorted sub-arrays in a manner that preserves their order without using additional space excessively. The design of the merge function solves these challenges by maintaining pointers for the current index of each sub-array and comparing elements to arrange them correctly in a temporary array, which minimizes movement and space usage. This systematic merging ensures that the function remains efficient, which is critical for the overall performance of the Merge Sort algorithm .

You might also like