Analysis & Design of Algorithms: Introduction To Sorting
Analysis & Design of Algorithms: Introduction To Sorting
I nt roduc t i on to
Sorting • Prepared by:-
Sagar Virani
Assistant Professor
Computer Engineering
Department
VVP Engineering College
Introduction to Sorting
• Sorting is the fundamental algorithmic problem in mathematics and
computer science.
• It puts elements in a certain order.
• The most common used orders are numerical and lexicographical
(alphabetical) order.
Introduction to Sorting
• Efficient sorting is important to optimize the use of other algorithms,
as it is the first step in most of them.
• There are many sorting algorithms but knowing which one to use
depends on the specific problem at hand.
Introduction to Sorting
Factors that help decide the sort to use are:
• How many elements needed to sort?
Will there be duplicate elements in the data?
If yes, does their order need to be maintained after sorting?
• What do we know about the distribution of elements?
Partially sorted
Totally random
Almost sorted
• What resources are available for executing sorts?
More memory
More processors
Introduction to Sorting
Sorting algorithms are often classified using different metrics:
• Computational complexity
Based on worst, average and best-case behavior
Acceptable behavior – O(n lgn)
Unacceptable behavior – O(n2)
• Memory usage
In-place
Not in-place
• Recursion
Recursive
Non-recursive
• Stability
Stable
Not stable
Introduction to Sorting
In-place sorting
• In-place means that the algorithm does not use extra space for manipulating the
input but may require a small though non-constant extra space for its operation.
• Usually, this space is O(log n), though sometimes anything in o(n) (Smaller
9 2 7 5 2 4 3 6
Introduction to Sorting
Stable sorting
• Stable sorting algorithms maintain the relative order of records with equal keys
(i.e., values).
• That is, a sorting algorithm is stable if whenever there are two records R and S
with the same key and with R appearing before S in the original list, R will
9 2 7 5 2 4 3 6 2 2 3 4 5 6 7 9
R S R S
Sorting Algorithms
Covered in the Syllabus:
• Selection Sort
• Bubble Sort
• Insertion Sort
• Shell Sort
• Heap Sort
• Counting Sort
• Radix Sort
• Bucket Sort
• Merge Sort
• Quick Sort
Any Questions?