1. SUBJECT CODE: 366 IT ELECTIVE 4 (ANALYSIS OF ALGORITM)
Submitted to:
Benedict D. Sy, MIT
Professor Submitted by:
Neil Soliven
Red Agustin
Jovany Princesa
2. Jon Bentley shows a three-line C version, and a five-
line optimized version.
Insertion sort is a sorting algorithm in which the elements are
transferred one at a time to the right position. In other words, an
insertion sort helps in building the final sorted list, one item at a
time, with the movement of higher-ranked elements. An insertion
sort has the benefits of simplicity and low overhead.
In an insertion sort, the first element in the array is considered as
sorted, even if it is an unsorted array. In an insertion sort, each
element in the array is checked with the previous elements, resulting
in a growing sorted output list. With each iteration, the sorting
algorithm removes one element at a time and finds the appropriate
location within the sorted array and inserts it there. The iteration
continues until the whole list is sorted.
3. It is simple to implement and is quite efficient for small
sets of data, especially if it is substantially sorted.
It has low overhead and can sort the list as it receives
data
It needs only a constant amount of memory space for
the whole operation. It is more efficient than other
similar algorithms such as bubble sort or selection sort.
It is better than Selection Sort and Bubble Sort
algorithms.
It is a stable sorting, as it does not change the relative
order of elements with equal keys
4. An insertion sort is less efficient on larger
data sets and less efficient than the heap sort
or quick sort algorithms.
As the number of elements increases the
performance of the program would be slow.
Insertion sort is that it does not perform as
well as other, better sorting algorithms
5.
6. The insertion sort works in a slightly different
way. It always maintains a sorted sub list in
the lower positions of the list. Each new item
is then “inserted” back into the previous sub
list such that the sorted sub list is one item
larger. The table below shows the insertion
sorting process. The shaded items represent
the ordered sub lists as the algorithm makes
each pass.
9. As we can see at the table, we pick up a
number and compare it with the number
ahead of it, and put the key in the right place.
5 have nothing before it.
1 is compared to 5 and is inserted before 5.
6 is greater than 5 and 1.
2 is smaller than 6 and 5, but greater than 1,
so it is inserted after 1.
4 is smaller than 6 and 5, but greater than 1
and 2, so it is inserted after 2.
3 is smaller than 6,5 and 4, but greater than
1and 2, so it is inserted after 2.
10. #include<stdio.h>
int main()
{
inti,j,n,temp,a[30];
printf("Enter the number of elements:");
scanf("%d",&n);
printf("nEnter the elementsn");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n-1;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j]; //moves element forward
j=j-1;
}
a[j+1]=temp; //insert element in proper place
}
printf("nSorted list is as followsn");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}
11. The selection sort is a combination of searching and sorting.
During each pass, the unsorted element with the smallest (or
largest) value is moved to its proper position in the array.
The number of times the sort passes through the array is one
less than the number of items in the array. In the selection sort,
the inner loop finds the next smallest (or largest) value and the
outer loop places that value into its proper location.
Selection sort algorithm starts by comparing first two elements
of an array and swapping if necessary, i.e., if you want to sort the
elements of array in ascending order and if the first element is
greater than second then, you need to swap the elements but, if
the first element is smaller than second, leave the elements as it
is. Then, again first element and third element are compared and
swapped if necessary. This process goes on until first and last
element of an array is compared. This completes the first step of
selection sort.
12. It performs well on a small list.
It is an in-place sorting algorithm; no
additional temporary storage is required
beyond what is needed to hold the original
list.
Its performance is easily influenced by the
initial ordering of the items before the sorting
process.
13. It is poor efficiency when dealing with a huge
list of items.
Selection sort requires n-squared number of
steps for sorting n elements.
Quick Sort is much more efficient than
selection sort
14. Beginning: 84 69 76 86 94 91
PASS 1 84 91 76 86 94 69
PASS 2 84 91 94 86 76 69
PASS 3 86 91 94 84 76 69
PASS 4 94 91 86 84 76 69
PASS 5 94 91 86 84 76 69
Beginning: 20 8 5 10 7
PASS 1 5 8 20 10 7
PASS 2 5 7 20 10 8
PASS 3 5 7 8 10 20
PASS 4 5 7 8 10 20
NOTE:
While being an easy sort, the selection sort is one of the least efficient. The
algorithm offers no way to end the sort early, even if it begins with an already
sorted list.
15. ASCENDING ORDER
#include<iostream>
#include<conio.h>
using namespace std;
template<class T>
voids_sort(T a[],int n)
{
inti,j,t;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j]<a[i]) //for descending
order use if(a[j]>a[i])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}
int main()
{
int a[100],i,n;
cout<<"Enter The number of
Element:n";
cin>>n;
cout<<"nEnter
Numbers:n";
for(i=0;i<n;i++)
{
cout<<"nEnter:";
cin>>a[i];
}
s_sort(a,n);
cout<<"nSort in Ascending
ordern";
for(i=0;i<n;i++)
{
cout<<a[i]<<"t";
}
getch();
return 0;
}