forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shell_sort.js
28 lines (25 loc) · 901 Bytes
/
Shell_sort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//shellSortfunction
function shellSort(array) {
var gap;
//gap is taken of size half of array length
/*loop for reducing gap size one bye one to with minimum value of 1*/
for (gap =Math.floor(array.length/2); gap >0; gap=Math.floor(gap/2)) {
/*loops for changing the swaping values with gap size*/
for (var i = gap; i < array.length; i++) {
/*checks if elements needed to be swapped*/
/*swaps the digits using temproray variable*/
temp = array[i];
/*loop for swaping number with gap length in between */
for (j = i; j >= gap && array[j - gap] > temp; j = j - gap)
array[j] = array[j - gap];
array[j] = temp;
}
}
/*prints the sorted array*/
console.log('the sorted array is '+array);
}
/* psuedo data for array */
var arr=[ 2, 10, 8, 1, 4, 1];
/*calls to shell sort function*/
shellSort(arr);
/*1 1 2 4 8 10*/