0% found this document useful (0 votes)
5 views

5.1.sorting_Theory

The document provides a critical comparison of various sorting algorithms, including bubble sort, selection sort, insertion sort, merge sort, quicksort, and heap sort, highlighting their advantages, disadvantages, time complexity, and space complexity. It emphasizes the importance of understanding sorting algorithms for problem-solving and complexity analysis, noting that quicksort is generally preferred for large datasets due to its efficiency. Additionally, it discusses the concepts of in-place sorting, stability, and adaptability in sorting algorithms.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

5.1.sorting_Theory

The document provides a critical comparison of various sorting algorithms, including bubble sort, selection sort, insertion sort, merge sort, quicksort, and heap sort, highlighting their advantages, disadvantages, time complexity, and space complexity. It emphasizes the importance of understanding sorting algorithms for problem-solving and complexity analysis, noting that quicksort is generally preferred for large datasets due to its efficiency. Additionally, it discusses the concepts of in-place sorting, stability, and adaptability in sorting algorithms.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Critical Comparison of Sorting Algorithms

A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders
are numerical order and lexicographical order. Sorting algorithms provide an introduction to a variety of core
algorithm concepts, such as big O notation, divide and conquer algorithms, data structures, best-, worst- and
average-case analysis, time-space tradeoffs, and lower bounds.

Here we will see some sorting methods. There are 200+ sorting techniques. We will discuss few of
them. Some sorting techniques are comparison based sort, some are non-comparison based sorting technique.
Here we will discuss on Comparison based sorting techniques.

Comparison Based Sorting techniques: Which are bubble sort, selection sort, insertion sort, Merge
sort, quicksort, heap sort etc. These techniques are considered as comparison based sort because in these
techniques the values are compared, and placed into sorted position in ifferent phases. Here we will see time
complexity of these techniques.

Bubble Sort:
Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be sorted,
comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the
list is repeated until no swaps are needed, which indicates that the list is sorted. Because it only uses
comparisons to operate on elements, it is a comparison sort.

Advantages Disadvantages
The primary advantage of the bubble sort is that it is The main disadvantage of the bubble sort is the fact
popular and easy to implement. that it does not deal well with a list containing a
huge number of items.
In the bubble sort, elements are swapped in place The bubble sort requires n-squared processing steps
without using additional temporary storage. for every n number of elements to be sorted.
The space requirement is at a minimum The bubble sort is mostly suitable for academic
teaching but not for real-life applications.

Selection Sort:
Selection sort is an in-place comparison sort. It has O(n2) complexity, making it inefficient on large lists, and
generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and also has
performance advantages over more complicated algorithms in certain situations.

Advantages Disadvantages
The main advantage of the selection sort is that it The primary disadvantage of the selection sort is its poor
performs well on a small list. efficiency when dealing with a huge list of items.

Because it is an in-place sorting algorithm, no additional The selection sort requires n-squared number of steps for
temporary storage is required beyond what is needed to sorting n elements.
hold the original list.
Its performance is easily influenced by the initial Quick Sort is much more efficient than selection
ordering of the items before the sorting process. sort

Insertion Sort:
Insertion sort is a comparison sort in which the sorted array (or list) is built one entry at a time. It is much
less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.
However, insertion sort provides several advantages:
Advantages Disadvantages

The main advantage of the insertion sort is its The disadvantage of the insertion sort is that it does
simplicity. not perform as well as other, better sorting
algorithms
It also exhibits a good performance when dealing With n-squared steps required for every n element
with a small list. to be sorted, the insertion sort does not deal well
with a huge list.
The insertion sort is an in-place sorting algorithm so The insertion sort is particularly useful only when
the space requirement is minimal. sorting a list of few items.

Merge Sort:
Merge sort is an O(n log n) comparison-based sorting algorithm. It is an example of the divide and conquer
algorithmic paradigm.
Advantages Disadvantages
It can be applied to files of any size. Requires extra space »N
Reading of the input during the run-creation step is Merge Sort requires more space than other sort.
sequential ==> Not much seeking.
If heap sort is used for the in-memory part of the merge, Merge sort is less efficient than other sort
its operation can be overlapped with I/O

Quick Sort:
Quicksort is a well-known sorting algorithm that, on average, makes O(n log n) comparisons to sort n items.
However, in the worst case, it makes O(n2) comparisons. Typically, quicksort is significantly faster than
other O(n log n) algorithms, because its inner loop can be efficiently implemented on most architectures, and
in most real-world data, it is possible to make design choices which minimize the probability of requiring
quadratic time.

Advantages Disadvantages
The quick sort is regarded as the best sorting The slight disadvantage of quick sort is that its
algorithm. worst-case performance is similar to average
performances of the bubble, insertion or selections
sorts.
It is able to deal well with a huge list of items. If the list is already sorted than bubble sort is much
more efficient than quick sort
Because it sorts in place, no additional storage is If the sorting element is integers than radix sort is
required as well more efficient than quick sort.

Heap Sort:
Heapsort is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat
slower in practice on most machines than a good implementation of quicksort, it has the advantage of a worst-
case O(n log n) runtime. Heapsort combines the time efficiency of merge sort and the storage efficiency of
quicksort.
Advantages Disadvantages
The Heap sort algorithm is widely used because Heap sort requires more space for sorting
of its efficiency.
The Heap sort algorithm can be implemented as Quick sort is much more efficient than Heap in
an in-place sorting algorithm many cases
its memory usage is minimal Heap sort make a tree of sorting elements.
Performance:
Sorting Time Complexity Space Complexity
Algorithms
Best case Average case Worst case Worst case
2 2
Bubble Sort O(n) O(n ) O(n ) O(1)
Selection Sort O(n2) O(n2) O(n2) O(1)
Insertion Sort O(n) O(n2) O(n2) O(1)
Merge Sort O(n log n) typical O(n log n) O(n log n) O(n)
Quick Sort O(n log n) O(n log n) O(n2) O(n)
Heap Sort O(n log n) O(n log n) O(n log n) O(1)

In-place, Stable and Adaptability:


 A sorting algorithm is In-place if the algorithm does not use extra space for manipulating the input but may
require a small though nonconstant extra space for its operation. Or we can say, a sorting algorithm sorts in-
place if only a constant number of elements of the input array are ever stored outside the array.
 A sorting algorithm is stable if it does not change the order of elements with the same value.
 Adaptability: Whether or not the presortedness of the input affects the running time. Algorithms that take this
into account are known to be adaptive.

Sorting Algorithms In-Place Stable Adaptability


Bubble Sort Yes Yes Generally not, but we
can make it adaptive
Selection Sort Yes No No
Insertion Sort yes Yes Yes
Quick Sort Yes No No
Merge Sort No* Yes No
Heap Sort Yes No
*Because it requires an extra array to merge the sorted subarrays
Critical Ideas to Think:

 Understanding the sorting algorithms are the best way to learn problem solving and complexity
analysis in the algorithms. In some cases, We often use sorting as a key routine to solve several
coding problems.
 Knowing which algorithm is best possible depends heavily on details of the application and
implementation. In most practical situations, quicksort is a popular algorithm for sorting large input
arrays because its expected running time is O(nlogn). It also outperforms heap sort in practice.
 If stability is important and space is available, the merge sort might be the best choice for the
implementation.
 Like insertion sort, quick sort has tight code and hidden constant factor in its running time is small.
 When the input array is almost sorted or input size is small, then the insertion sort can be preferred.
 We can prefer merge sort for sorting a linked list.
 Bubble sort is not a practical sorting algorithm when n is large.

You might also like