Documente Academic
Documente Profesional
Documente Cultură
Insertion sort…………………………………………………..
Explanation of example………………………………......
Adventages………………………………………………………..
Referances………………………………………………………..
Teaching remarks…………………………………………….
Insertion Sort
Insertion sort is a simple sorting algorithm that builds the finalsorted array (or list)
one item at a time. It is much less efficient on large lists than more advanced
algorithms such as quicksort, heapsort, or merge sort..
We begin by assuming that a list with one item (position is already sorted. On each
pass, one for each item 1 through , the current item is checked against those in the
already sorted sublist. As we look back into the already sorted sublist, we shift those
items that are greater to the right. When we reach a smaller item or the end of the
sublist, the current item can be inserted.
shows the fifth pass in detail. At this point in the algorithm, a sorted sublist of five
items consisting of 17, 26, 54, 77, and 93 exists. We want to insert 31 back into the
already sorted items. The first comparison against 93 causes 93 to be shifted to the
right. 77 and 54 are also shifted. When the item 26 is encountered, the shifting
process stops and 31 is placed in the open position. Now we have a sorted sublist of
six items.
2
Insertion-Sort(A[])
3
For (j=2 to A.length)
4 Key = A[j]
6 i=j – 1
#include<iostream>
1
2 #include<conio.h>
main()
4
{
5
int Key;
6
int array[8];
7
cout<<"Enter 8 numbers: "<<endl;
8
for(int i=0; i<8; i++)
9 {
10 cin>>array[i];
11 }
cout<<endl;
12
cout<<"Orignally entered array by the user is: "<<endl;
13
for(int j=0; j<8; j++)
14
{
15
cout<<array[j];
16
cout<<endl;
17 }
18 cout<<endl;
20 { Key = array[j];
int i = j-1;
21
while(i >= 0 && array[i] > Key)
22
{
23
array[i + 1] = array[i];
24
i = i - 1;
25 }
26 array[i + 1] = Key;
27
cout<<"Sorted Array is: "<<endl;
29 {
30 cout<<array[i]<<endl;
}
31
getch();
Iteration 1:-
i > 0 and A[i] > key -> 1>0 and 2 > 3 -> condition false. Therefore loop will terminate
and the value of key will be assigned to A[i + 1]. In short there will be no change in
our array.
Second Pass:-
(j < A.length) -> True
Iteration 1:-
i > 0 and A[i] > key -> 2>0 and 3 > 1 -> True.
Therefore A[i + 1] assigned a[i].
Iteration 2:-
Here j=3, Key=1 and i=1.
i > 0 and A[i] > key -> 1>0 and 2 > 1 -> True.
Therefore A[i + 1] assigned a[i].
i > 0 and A[i] > key -> 0>0 and 0 > 1 -> False.
Therefore, the loop will terminate.
After the termination of a loop, the value of key will be assigned to A[i+1].
Third Pass:-
(j < A.length) -> True
Iteration 1:-
i > 0 and A[i] > key -> 3>0 and 3 > 8 -> False.
Therefore, a loop will terminate and the value of key will be assigned to A[i + 1]. So
there will be no change in our array.
Fourth Pass:-
(j < A.length) -> True
Iteration 1:-
i > 0 and A[i] > key -> 4 > 0 and 8 > 10 -> false.
Therefore, a loop will terminate and the value of key will be assigned to A[i + 1]. It
means our array will remains same.
Fifth Pass:-
(j < A.length) -> True
Iteration 1:-
i > 0 and A[i] > key -> 5 > 0 and 10 > 14 -> false.
Therefore, a loop will terminate and the value of key will be assigned to A[i + 1]. So
no change in our array.
/* */
Sixth Pass:-
(j < A.length) -> True
Iteration 1:-
i > 0 and A[i] > key -> 6 > 0 and 14 > 12 -> True.
Therefore A[i + 1] assigned a[i].
Iteration 2:-
Here j=7, Key=12 and i=5.
i > 0 and A[i] > key -> 5>0 and 10 > 12 -> False.
Therefore, a loop will terminate and the value of key will be assigned to A[i + 1].
Seventh Pass:-
(j < A.length) -> True
Iteration 1:-
i > 0 and A[i] > key -> 7 > 0 and 14 > 9 -> True.
Therefore A[i + 1] assigned a[i].
Iteration 2:-
Here j=8, Key=9 and i=6.
i > 0 and A[i] > key -> 6 > 0 and 12 > 9 -> True.
Therefore A[i + 1] assigned a[i].
i > 0 and A[i] > key -> 5 > 0 and 10 > 9 -> True.
Therefore A[i + 1] assigned a[i].
Iteration 4:-
Here j=8, Key=9 and i=4.
i > 0 and A[i] > key -> 4 > 0 and 8 > 9 -> False.
Therefore, a loop will terminate and the value of key will be assigned to A[i + 1].
Eighth Pass:-
Here (j= 9),
References
https://search.yahoo.com/search?ei=utf-
8&fr=tightropetb&p=inseration+sort+by+hell+hellgeek&type=103793_101717
http://www.geeksforgeeks.org/insertion-sort/
http://interactivepython.org/runestone/static/pythonds/index.html
https://www.tutorialspoint.com/data_structures_algorithms/insertion_sort_algorithm.h
tm
http://www.hellgeeks.com/insertion-sort/
https://www.google.com.pk/search?
dcr=0&source=hp&ei=PjkqWsKnFobSUZWCg5gN&q=linear+search+algorithm&oq=l
inear+search&gs_l=psy-
ab.1.1.0i67k1l4j0l6.1245.5597.0.14249.14.10.0.1.1.0.630.1255.5-
2.2.0....0...1c.1.64.psy-ab..11.3.1285.0..35i39k1j0i131i67k1.0.Z_Qf6FbZO9E
https://www.google.com.pk/search?dcr=0&source=hp&ei=o0kqWvH1Ls-
YsAe_85zQCQ&q=inseration+sort&oq=inseration+sort&gs_l=psy-
ab.3..0i10k1l10.21580.25511.0.26254.16.12.0.0.0.0.988.1689.6-
2.2.0....0...1c.1.64.psy-ab..14.2.1687.0...0.