You are on page 1of 16

Sorting

Sorting is a process that organizes a collection of data into either


ascending or descending order.
Sorting Algorithms
• There are many sorting algorithms, such as:
• Selection Sort
• Insertion Sort
• Bubble Sort
• Merge Sort
• Quick Sort

CENG 213 Data Structures


Insertion Sort
• Idea: like sorting a hand of playing cards
• Start with an empty left hand and the cards facing down on the table.
• Remove one card at a time from the table, and insert it into the correct
position in the left hand.
• compare it with each of the cards already in the hand, from right to
left.
• The cards held in the left hand are sorted.
• these cards were originally the top cards of the pile on the table

9/27/21 3
Insertion Sort
Remove one card at a time from the table, and
insert it into the correct position in the left hand.

77 33 44 11 88 22 66 55

9/27/21 4
Insertion Sort
Pass 1: 77 by itself is trivially sorted

-α 77

33 44 11 88 22 66 55

9/27/21 5
Insertion Sort
Pass 2: 33 is inserted either before or
after 77 so that: 33, 77 is sorted.

77
-α 33

44 11 88 22 66 55

9/27/21 6
Insertion Sort
Pass 3: 44 is inserted into its proper place
in 33, 77, that is , before 33, between 33
and 77, or after 77, so that: 33, 44, 77 is
sorted.
44 77
-α 33

11 88 22 66 55

9/27/21 7
Insertion Sort

Pass 4: 11 is inserted into its


proper place in 33,44,77. so that:
11, 33, 44, 77 is sorted.

44 77
33
-α 11

88 22 66 55

9/27/21 8
Insertion Sort
Pass 5: 88 is inserted into its
proper place in 11,33,44,77. so
that: 11, 33, 44, 77, 88 is sorted.

44 77 88
33
-α 11

22 66 55

9/27/21 9
Insertion Sort
Pass 6: 22 is inserted into its proper
place in 11,33,44,77,88. so that: 11,
22, 33, 44, 77, 88 is sorted.

33 44 77 88
22
-α 11

66 55

9/27/21 10
Insertion Sort

Pass 7: 66is inserted into its proper


place in 11, 22, 33,44,77,88. so that:
11, 22, 33, 44, 66, 77, 88 is sorted.

33 44 66 77
22 88
-α 11

55

9/27/21 11
Insertion Sort

Pass 8: 55 is inserted into its proper place in 11,


22, 33,44,66,77,88. so that: 11, 22, 33, 44, 55, 66,
77, 88 is sorted.

33 44 55 66
22 77
-α 11 88

9/27/21 12
Insertion Sort
Suppose an array A with n elements A[1], A[2],……,A[N] is in memory.
The insertion sort algorithm scans A from A[1] to A[N], inserting each
element A[K] into its proper position in the previously sorted subarray
A[1], A[2],…..A[K-1]. That is:
Pass 1: A[1] by itself is trivially sorted.
Pass 2: A[2] is inserted either before or after A[1] so that: A[1], A[2] is sorted.
Pass 3: A[3] is inserted into its proper place in A[1], A[2], that is , before
A[1], between A[1] and A[2], or after A[2], so that: A[1], A[2], A[3] is sorted.
Pass 4: A[4] is inserted into its proper place in A[1], A[2],A[3] so that:
………………….A[1], A[2], A[3], A[4] is sorted.
…………………………………………………………………………………
…………………………………………………………………………………
Pass N: A[N] is inserted into its proper place in A[1], A[2],……,A[N-1] so
that: ………………….A[1], A[2],……., A[N] is sorted.
Pass A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8]

K=1, -α 77 33 44 11 88 22 66 55
K=2 -α 77 33 44 11 88 22 66 55
K=3 -α 33 77 44 11 88 22 66 55
K=4 -α 33 44 77 11 88 22 66 55
K=5 -α 11 33 44 77 88 22 66 55
K=6 -α 11 33 44 77 88 22 66 55
K=7 -α 11 22 33 44 77 88 66 55
K=8 -α 11 22 33 44 66 77 88 55
Sorted -α 11 22 33 44 55 66 77 88
Algorithm 9.1
(Insertion sort) INSERTION (A,N)
1. Set A[0]:=-α
2. Repeat Steps 3 to 5 for K=2,3,……,N:
3. Set TEMP:=A[K] and PTR:=K-1.
4. Repeat while TEMP<A[PTR]:
(a) Set A[PTR+1]:=A[PTR]: [Move element forward.]
(b). Set PTR:=PTR-1.
[End of loop]
5. Set A[PTR+1]:=TEMP. [Insert element in proper place]
[End of step 2 loop]
6. Return.

You might also like