You are on page 1of 6

Data Structures

Unit 5 Part 1
BCA, NGMC
Sorting
• Sorting refers to the operation or technique of
arranging and rearranging sets of data in some
specific order.
• The importance of sorting lies in the fact that
data searching can be optimized to a very high
level, if data is stored in a sorted manner.
• Sorting is also used to represent data in more
readable formats.
• Example: Telephone Directory, Dictionary
Insertion Sort
• Insertion sorting algorithm can be best
thought of as a sorting scheme which can be
compared to that of sorting a hand of playing
cards.
• Take one card and then look at the rest with
the intent of building up an ordered set of
cards in your hand.
Advantages
• Stable algorithm
• Simple to implement
• Efficient for small data sets.
• Adaptive
• Need constant amount memory space.
Algorithm
i←1
while i < length(A)
j←i
while j > 0 and A[j-1] > A[j]
swap A[j] and A[j-1]
j←j-1
end while
i←i+1
end while

You might also like