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

Insertion Sort

Uploaded by

aamirshahad3321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Insertion Sort

Uploaded by

aamirshahad3321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

E ff i c ient

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;

// Move elements of arr[0..i-1]


that are greater than key to one
position ahead of their current
position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}

arr[j + 1] = key;
}
}
Advantages of Insertion Sort

O n e of the key advantages of insertion


s o r t is that it is a n in-pla c e so r t i n g
a l g o r i t h m , m e a n i n g it d o e s n o t r e q u i r e
a n y extra m e m o r y. It is also a stable
s o r t i n g a l g o r i t h m , m e a n i n g it p r e s e r v e s
t h e re la tive o r d e r o f e q u a l e l e m e n t s .
Additionally, it is e a s y to i m p l e m e n t a n d
understand.
Disadvantages of Insertion Sort

The main disadvantage of insertion


sort is its t i m e c o m p l e x i t y for larger
d a t a sets. It c a n a l s o b e s l o w w h e n
so r t i n g d a t a that is a l r e a d y partially
s o r t e d . F u r t h e r m o r e , it m a y n o t b e
suitable for sorting c ompl e x data types
such as objects or structures.
Conclusion
In c onc lusi on, i nse r ti on so r t is a s i m p l e a n d efficient
a l g o r i t h m f o r so r t i n g s m a l l d a t a sets. W h i l e it m a y n o t b e
suitable for larger d a t a sets o r c o m p l e x d a t a types, it is still
w i d e l y u s e d in pra c ti c e d u e t o its si m pl i c i t y a n d e a s e o f
i m p l e m e n t a t i o n . W e h o p e t hi s p r e s e n t a t i o n h a s p r o v i d e d
you
w i t h a c o m p r e h e n s i v e g u i d e t o i n s e r t i o n sort.

You might also like