# Selection Sort Vs Bubble Sort

Bubble sort takes an order of n time whereas selection sort consumes an order of n 2 time. Bubble sort is a stable algorithm, in contrast, selection sort is unstable. Selection sort algorithm is fast and efficient as compared to bubble sort which is very slow and inefficient. Before the stats, You must already know what is Merge sort, Selection Sort, Insertion Sort, Bubble Sort, Quick Sort, Arrays, how to get current time. What is Stable Sorting? A sorting algorithm is said to be stable if and only if two records R and S with the same key and with R appearing before S in the original list, R must appear before S in.

Contents: Insertion Sort vs. Selection Sort.Difference Insertion and Selection SortThe key difference between insertion sort and selection sort is that in insertion sort in which is data is sorted by inserting it in already sorted list whereas, in selection sort, data is sorted by putting consecutive elements in sorted locations. Insertion Sort vs. Selection SortSorting is a very important concept in programming; there are many algorithms for sorting in programming. Two most famous algorithms for sorting are insertion sort and selection sort.

Both these algorithms are different from each other. In insertion sort in which is data is sorted by inserting it into the already sorted list. Only one element is inserted in a sorted array at a time.

The main objective of insertion sort is to insert the element at the right place with the right order. After these insertions whole list is sorted. Advertisement - Continue Reading BelowFor the better performance, the insertion sort algorithm is not used, but the algorithm that is used in place of bubble sort is the selection sort algorithm. Selection sort is a sorting algorithm that selects the largest number and swap with the last number. In selection sort, we select one number, and that number is selected on demand for selection whether it is in ascending order or descending order. Comparison Chart BasisInsertion SortSelection SortMeaningIn insertion sort in which is data is sorted by inserting it in already sorted listIn selection sort, data is sorted by putting consecutive elements in sorted locations.AlgorithmInsertion sort is a stable algorithm.Selection sort is an unstable algorithm.Immediate dataInsertion sort cannot deal with immediate dataInsertion sort cannot deal with immediate.Time complexityIn case of insertion sort time, complexity is 0 (n)In case of selection sort time, complexity is 0 (n^2).

Advertisement - Continue Reading Below Insertion SortIn insertion sort in which is data is sorted by inserting it in the already sorted list. Only one element is inserted in a sorted array at a time. The main objective of insertion sort is to insert the element at the right place with the right order. After these insertions whole list is sorted.

In insertion sort, we need two sets of arrays, among these two arrays one is sorted, and other is not sorted. There is a loop that keeps on working until the array is sorted.

## Bubble Sort Example

Selection SortFor the better performance, bubble sort algorithm is not used, but the algorithm that is used in place of bubble sort is the selection sort algorithm. Selection sort is a sorting algorithm that selects the largest number and swap with the last number.

In selection sort, we select one number, and that number is selected on demand for selection whether it is in ascending order or descending order.

Bubble sort does it's work by bubbling the lowest value to the top or sinking the highest to the bottom (depending on how it is written). It's slow for large numbers of items. But even with large number of elements it can be reasonable if the items are almostsorted.Quicksort does its work by getting a pivot, then Quicksorting elements to the left and Quicksorting elements to the right. It's usually fast but can be slow if the items are already sorted (depending on how it is written).In the good old days, some system sorts would do a bubble sort then if there were a large number of swaps switch to Quicksort (or some other one).The links Marco posted should add more to that simplistic explanation.PS Some time ago I came across animated sorts on the web. Can't remebmer the URL but a google for something with sort bubble animation might turn up something.Regards David R-The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.Object-oriented programming offers a sustainable way to write spaghetti code.

Paul Graham.Every program eventually becomes rococo, and then rubble. Alan PerlisThe only valid measurement of code quality: WTFs/minute. Bubble sort does it's work by bubbling the lowest value to the top or sinking the highest to the bottom (depending on how it is written).

It's slow for large numbers of items. But even with large number of elements it can be reasonable if the items are almostsorted.Quicksort does its work by getting a pivot, then Quicksorting elements to the left and Quicksorting elements to the right. It's usually fast but can be slow if the items are already sorted (depending on how it is written).In the good old days, some system sorts would do a bubble sort then if there were a large number of swaps switch to Quicksort (or some other one).The links Marco posted should add more to that simplistic explanation.PS Some time ago I came across animated sorts on the web. Can't remebmer the URL but a google for something with sort bubble animation might turn up something.Regards David R-The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.Object-oriented programming offers a sustainable way to write spaghetti code. Paul Graham.Every program eventually becomes rococo, and then rubble. Alan PerlisThe only valid measurement of code quality: WTFs/minute.

'In the good old days, some system sorts would do a bubble sort then if there were a large number of swaps switch to Quicksort (or some other one).' According to wikipedia the default library sorting of collections in Python and Java uses a sorting algorithm that uses Insertion sort (the slightly less trivial and twice as fast version of bubble sort) for small sets of data and a more complex modificationof mergesort for larger sets of data. It also has a number of added checks for special cases (i.e. Already sorted, reverse sorted, large subsets of sorted data).