2. #Presentation submitted to:
Sohrab Hossain, Assistant professor of CSE
FSET,USTC
#Presentation submitted by:
Abdul Kuddus
Department of CSE
ID:15010102
Batch:25th
#Presentation Name:
Sort the element of an array using insertion sort.
3. #Sorting:
Let A be a list of m elements A1 , A 2 , …. An in memory. Sorting A
refers to the operation of rearranging the contents of A. So that they
are increasing in order(numerically or alphabetically) that is ,so that
A1 <=A2 <=A3 <= A4 <= ……..<=An
#Insertion sort:
Suppose an array with n elements A[1],A[2]……A[n] in the
memory. The insertion sort algorithm scans A from A[1] to A[n]
inserting each element A[k] into its position in the previously sorted
sub array A[1],A[2]…..A[k-1].
4. #Algorithm:
Insertion (A,N)
This algorithm sort the array A with N element
1.Set A[0]=1 [initial sentinel element]
2.Repeat steps 3 and 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]
(a) Set PTR:=PTR-1.
[End of loop]
5.Set A[PTR+1]:=TEMP. [insert element at proper place]
[End of step loop 2]
6. Return.
6. *Pass1: using sorting we compare 1st element with its previous nearest large
element
*pass2: using sorting we compare 2nd element with its previous nearest large
element
After comparison result of array
*pass3: : using sorting we compare 3rd element with its previous large element
77 44 22 6633 11
77
33 44 11 22 6677
33 44 11 22 66
33 77 11 22 66
33
At 77 has no previous large item , so result
77 44 11 22 66
44
7. After comparison result of array
*pass4: using sorting we compare 4th element with its previous nearest large
element
After comparison result of array
*pass5: using sorting we compare 4th element with its previous nearest large
element
44
4433
33
11
11 33 44
11 33 44 2277 66
77 6622
77 22 66
77 11 22 66
8. After comparison result of array
*pass6: using sorting we compare 4th element with its previous nearest large
element
After comparison result of array
11
664422 55
22 33 77
22 44 55 66 77
11 77
44 55
The array is sorted
11