Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Analysis & Design of Algorithms

I nt r o d uc t i o n t o
• Prepared by:-
Sorting
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.
• 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
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
Stable Sort
• 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 appear before S in the sorted list.

Input List Sorted List

9 2 7 5 2 4 3 6 2 2 3 4 5 6 7 9

R S R S
In-place Sort
• An in-place algorithm is an algorithm that does not need an extra
space and produces an output in the same memory that contains the
data by transforming the input ‘in-place’.
• However, a small constant extra space used for variables is allowed.
• This space is O(log n), though sometimes anything in O(n).
Any Questions?

You might also like