Last active
March 25, 2017 09:44
-
-
Save niteshpsit1/d4bf85e84912ab1ede4cc6300c36fed0 to your computer and use it in GitHub Desktop.
sorting programs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let arrayToBeSort = [12,45,75,35,55,54,2,21] | |
// ## Bubble Sort | |
/** | |
* | |
* Bubble sort is a simple sorting algorithm. | |
* This sorting algorithm is comparison-based algorithm | |
* in which each pair of adjacent elements is compared | |
* and the elements are swapped if they are not in order. | |
* This algorithm is not suitable for large data sets as | |
* its average and worst case complexity are of Ο(n2) | |
* where n is the number of items. | |
*/ | |
function bubbleSort(arrayToBeSort){ | |
// take the length of array | |
let arrayLength = arrayToBeSort.length | |
for(let i = 1; i <= arrayLength; i++){ | |
for(let j = 0; j < arrayLength - i ; j++){ | |
if(arrayToBeSort[j] > arrayToBeSort[j+1]){ | |
let temp = arrayToBeSort[j] | |
arrayToBeSort[j] = arrayToBeSort[j+1] | |
arrayToBeSort[j+1] = temp | |
} | |
} | |
} | |
} | |
// ## Insertion Sort | |
/** | |
* | |
* This is an in-place comparison-based sorting algorithm. Here, | |
* a sub-list is maintained which is always sorted. For example, | |
* the lower part of an array is maintained to be sorted. | |
* An element which is to be 'insert'ed in this sorted sub-list, | |
* has to find its appropriate place and then it has to be inserted there. Hence the name, insertion sort. | |
* The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in the same array). | |
* This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2), | |
* where n is the number of items. | |
*/ | |
function insertionSort(arrayToBeSort){ | |
let arrayLength = arrayToBeSort.length | |
for(let i = 0; i < arrayLength; i++){ | |
for(let j = i; j > 0; j --){ | |
if(arrayToBeSort[j-1] > arrayToBeSort[j]){ | |
let temp = arrayToBeSort[j-1] | |
arrayToBeSort[j-1] = arrayToBeSort[j] | |
arrayToBeSort[j] = temp | |
} | |
else | |
break; | |
} | |
} | |
} | |
//## | |
/** | |
* | |
* Selection sort is a simple sorting algorithm. | |
* This sorting algorithm is an in-place comparison-based algorithm | |
* in which the list is divided into two parts, the sorted part at | |
* the left end and the unsorted part at the right end. Initially, | |
* the sorted part is empty and the unsorted part is the entire list. | |
* | |
* The smallest element is selected from the unsorted array and swapped | |
* with the leftmost element, and that element becomes a part of the sorted array. | |
* Thisprocess continues moving unsorted array boundary by one element to the right. | |
* | |
* This algorithm is not suitable for large data sets as its average and worst case | |
* complexities are of Ο(n2), where n is the number of items. | |
*/ | |
function selectionSort(arrayToBeSort){ | |
let arrayLength = arrayToBeSort.length | |
for(let i = 0; i < arrayLength -1; i++){ | |
let minIndex = i; | |
for(let j = minIndex + 1; j < arrayLength; j++ ){ | |
if(arrayToBeSort[minIndex] > arrayToBeSort[j]) | |
minIndex = j | |
} | |
if(minIndex !== i){ | |
let temp = arrayToBeSort[i] | |
arrayToBeSort[i] = arrayToBeSort[minIndex] | |
arrayToBeSort[minIndex] = temp | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment