-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathBubble_Sort.js
More file actions
43 lines (40 loc) · 1004 Bytes
/
Bubble_Sort.js
File metadata and controls
43 lines (40 loc) · 1004 Bytes
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function swap(arr, first_Index, second_Index){
var temp = arr[first_Index];
arr[first_Index] = arr[second_Index];
arr[second_Index] = temp;
}
function bubble_Sort(arr){
var len = arr.length,
i, j, stop;
for (i=0; i < len; i++){
for (j=0, stop=len-i; j < stop; j++){
if (arr[j] > arr[j+1]){
swap(arr, j, j+1);
}
}
}
return arr;
}
console.log(bubble_Sort([10, 5, 2, 5, -6, 4, -1])); //array that is to be sorted
/*
algorithm:
function swap( array, first_Element_index, second_element_index){
swap the variables using temporary variable
}
function bubble_Sort(array)
{
len=array.length
for i = 0 to len :
swapped = false
for j = 0 to len-i:
(compare the adjacent elements)
if array[j] > array[j+1] then
/* swap them */
swap( array,j,j+1 )
swapped = true
end if
end for
return arr
}
pass the argument array
*/