Insertion Sort
Insertion Sort
Sorting
with Insertion
Sort Algorithm:
Introd uction
I n this prese ntation, w e will d i s c u s s t h e
i n s e r t i o n so r t a l g o r i t h m a n d its
efficiency in sorting data. We will
e x p l o r e t h e v a r i o u s a s p e c t s o f this
a l g o r i t h m a n d h o w it c a n b e u s e d t o
optimize the sorting process.
What is Insertion Sort?
Inse r t i o n so r t is a s i m p l e sorting
a l g o r i t h m t h a t so r t s a n a r r a y b y
repeatedly shifting elements to
their correct position. It w o r k s b y
iterating through the array a n d
comparing each element with the
o n e b e f o r e it, s w a p p i n g t h e m if
n e c e ssa r y. T h i s p r o c e s s is r e p e a t e d
until t h e entire a r r a y is sorte d.
Efficiency of Insertion
Sort
A l t h o u g h i nse r t i o n so r t is a s i m p l e
algorithm, it is efficient for sm a ll
arrays or lists. It ha s a t i m e c om pl e xi t y
of O(n^2), w h i c h m a k e s it less efficient
than other sorting algorithms for
larger data sets. H o w e v e r, it is still
w i d e l y u s e d i n p r a c t i c e d u e t o its
simplicity a n d efficiency for sm a l l da ta
set s.
Algorithm
void insertionSort(int arr[], int size) {
for (int i = 1; i < size; i++) {
int key = arr[i];
int j = i - 1;
arr[j + 1] = key;
}
}
Advantages of Insertion Sort