answersLogoWhite

0


Best Answer

Before we examine the differences, let us examine the implementations. The code that follows is written in C++, but can be easily adapted to other languages.

Firstly, let us assume we have an array of num elements (we'll use integers here, but typically you will use pointers to maintain efficiency):

int array[num];

A selection sort has the following nested loop structure:

for( int outer=0; outer < num-1; ++outer )

{

int min = outer;

for( inner=outer+1; inner < num; ++inner )

{

if( array[inner] < array[min] )

min = inner;

}

if( min != outer )

swap( array[min], array[outer] );

}

Whereas an insertion sort has the following nested loop structure:

for( index=1; index < num; ++index )

{

value = array[index];

hole = index;

while( hole > 0 && value < array[hole-1] )

array[hole] = array[--hole];

array[hole] = value;

}

Although both algorithms split an array into two subsets (a sorted subset to the left and an unsorted subset to the right) and both move one element from one set to the other on each iteration of the outer loop, the selection sort begins with an empty sorted subset while the insertion sort begins with a sorted subset of one element (any array with only one element can always be regarded as being sorted).

In both cases, the outer loop keeps track of the initial index of the current unsorted subset and both perform a complete traversal of the unsorted subset. However selection sort stops when there is only one unsorted element left, which automatically becomes the largest value in the sorted set and therefore doesn't need to move, whereas insertion sort traverses the entire unsorted subset.

The key difference is in the inner loops. With selection sort, the inner loop skips the first unsorted element and traverses the remainder of the unsorted subset locating the index of the lowest value (recorded by min). At the end of the inner traversal, if min is not the same as the outer loop index then the values are swapped. Thus the sorted subset gains a new element containing the next largest value on each iteration, beginning with the lowest value.

With insertion sort, the outer loop extracts the first unsorted element's value and stores its index (hole). There isn't really a hole in the array, it's simply a marker to determine where the extracted value will be inserted. The inner while loop then traverses the sorted subset. On each iteration, if the hole marker is non-zero and the value is less than the value of the element to the left of the hole marker, then that element's value is copied into the hole to its right and the hole marker moves left. When the inner loop ends, the hole marker denotes where the extracted value should be placed. Thus if the extracted value is greater than or equal to the largest sorted value, then the value doesn't move (it was already sorted), otherwise it is inserted into its correct position within the sorted subset.

It can be proved that although selection sort only requires one swap at most for every iteration of the outer loop, it must perform (num-1)! comparisons in total (that is, for num=6 there are 5+4+3+2+1 comparisons in total). It should be noted that every swap incurs three copy processes, however a single copy takes less time than a single comparison.

By contrast, insertion sort stops comparing when the insert point is located, thus, on average, there will be fewer comparisons than (num-1)!. However, while every iteration of the outer loop incurs two copy processes even if the current element doesn't need to move, and additional copy processes for each movement of the hole marker, it's wrong to assume selection sort is always faster. On average, it is actually slower.

Looking at the worst case example, let us suppose the array is in reverse order. Selection sort requires (num-1)! comparisons no matter what and will also incur n-1 swaps in total (which is 3(num-1) copies in total). By contrast, insertion sort also requires (num-1)! comparisons but requires 2(num-1)+(num-1)! copy processes. The end result is that selection sort performs slightly better, and will improve as the array size increases.

Another worst case example is the already-sorted array. Again, selection sort requires (num-1)! comparisons but no swaps at all. However, insertion sort only requires num-1 comparisons and 2(num-1) copies. In this case, insertion sort performs slightly better, and will improve as the array size increases.

While both algorithms are fairly efficient when dealing with small arrays (typically up to 20 elements), with insertion sort performing better on average, they are highly inefficient when dealing with larger arrays. For this you need recursive, logarithmic, divide-and-conquer algorithms, such as quicksort, which are capable of sorting num elements with only (log num) comparisons. However, these algorithms don't perform well with smaller subsets, so it pays to combine the two into a hybrid algorithm. That is, when a logarithmic algorithm reduces a subset to 20 elements or less, then that subset can be sorted far more efficiently with an insertion sort.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

what is the difference between selection sort and insertion sort?

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between insertion and selection sort?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the difference between insertion sort and selection sort?

Both bubble sort and selection sort are in-place sorts, which means they require no additional space to sort. Both are O(n). Both also share worst/average case time complexities of O(n2). Selection sort also has O(n2) for a best case scenario, while an intelligent bubble sort implementation will have O(n) for a best case (already sorted) scenario. Note that while looking at the numbers above seem to show that bubble sort has a slight edge over selection sort, in practice you should choose selection over bubble. It will very nearly always perform better in real-time tests.


Who invented insertion sort?

There are no records of when insertion sort was invented because people have been sorting things using the insertion sort and selection sort algorithms since before records began; they are ancient algorithms. You cannot be credited for creating an algorithm that already exists. Shell sort, which is a refinement of insertion sort, was developed much later, in 1959 by Donald Shell. His algorithm can be credited because it takes advantage of a computer's processing abilities, whereas insertion sort and selection sort rely purely on a human's processing abilities.


Different types of sorting techniques in c language?

types of sorting in c language are: insertion sort selection sort bubble sort merge sort two way merge sort heap sort quick sort


What is the difference between bubble sort and selection sort in pascal?

The traditional bubble sort moves any number of elements at most one position per iteration, while selection sort moves exactly one element per iteration. Both sorts require an exponential amount of time to produce their results.


Using doublelinked list insertion sort in c language?

using doublelinked list insertion sort in c language

Related questions

What are the types of sort algorithms?

insertion,bubble,quick, quick3, merge, shell,heap, selection sorting


What is the difference between insertion sort and selection sort?

Both bubble sort and selection sort are in-place sorts, which means they require no additional space to sort. Both are O(n). Both also share worst/average case time complexities of O(n2). Selection sort also has O(n2) for a best case scenario, while an intelligent bubble sort implementation will have O(n) for a best case (already sorted) scenario. Note that while looking at the numbers above seem to show that bubble sort has a slight edge over selection sort, in practice you should choose selection over bubble. It will very nearly always perform better in real-time tests.


Who invented insertion sort?

There are no records of when insertion sort was invented because people have been sorting things using the insertion sort and selection sort algorithms since before records began; they are ancient algorithms. You cannot be credited for creating an algorithm that already exists. Shell sort, which is a refinement of insertion sort, was developed much later, in 1959 by Donald Shell. His algorithm can be credited because it takes advantage of a computer's processing abilities, whereas insertion sort and selection sort rely purely on a human's processing abilities.


Different types of sorting techniques in c language?

types of sorting in c language are: insertion sort selection sort bubble sort merge sort two way merge sort heap sort quick sort


What is the difference between bubble sort and selection sort in pascal?

The traditional bubble sort moves any number of elements at most one position per iteration, while selection sort moves exactly one element per iteration. Both sorts require an exponential amount of time to produce their results.


Who is best merge sort or insertion sort?

Merge sort is good for large data sets, while insertion sort is good for small data sets.


Why comparisons are less in merge sort than insertion sort?

the main reason is: Merge sort is non-adoptive while insertion sort is adoptive the main reason is: Merge sort is non-adoptive while insertion sort is adoptive


Using doublelinked list insertion sort in c language?

using doublelinked list insertion sort in c language


What are the applications of selection sort?

None. Selection sort can only be used on small sets of unsorted data and although it generally performs better than bubble sort, it is unstable and is less efficient than insert sort. This is primarily because insert sort only needs to scan as far back as required to perform an insertion whereas selection sort must scan the entire set to find the lowest value in the set. And although selection sort generally performs fewer writes than insert sort, it cannot perform fewer writes than cycle sort, which is important in applications where write speed greatly exceeds read speed.


How do you improve insertion sort algorithm?

If there was a way, it would be the new insertion sort! Theoretically you could reduce the time by using a linked list and searching to the position it needs to be inserted and inserting it. In practice however you would be better off simply using a different sort, especially if you don't want your data in a linked list. Selection sort is better when writing is expensive. Quicksort and Mergesort are faster on large data sets.


What is the difference between sort of and kind of?

&quot;Sort of&quot; is used to indicate a small degree or extent, while &quot;kind of&quot; is used to suggest a category or type. For example, &quot;I sort of like ice cream&quot; implies a mild preference, whereas &quot;I'm kind of hungry&quot; suggests a general feeling of hunger.


What are the solutions for MCA sem 3 solved assignments?

sort the follwing list of numbers in descending 187,62,155,343,184,958,365,427,78,94,121,388 using each of the follwing methods: 1)Insertion sort 2)selection sort 3)heap sort 4)merge sort 5)quick sort further count the number of operations, by each sorting method