forked from pranjal36/SortingAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort1.js
More file actions
142 lines (110 loc) · 2.71 KB
/
sort1.js
File metadata and controls
142 lines (110 loc) · 2.71 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* This file contains a three implementations of Bubble sort algorithm in JavaScript (ES5)
* The complexity of Bubble sort is O(n^2)
* From: Ben's blog http://blog.benoitvallon.com/sorting-algorithms-in-javascript/the-bubble-sort-algorithm/
*/
var array = [9, 2, 5, 6, 4, 3, 7, 10, 1, 8];
var arrayOrdered = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var arrayReversed = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
/*
* Swap function helper
*/
function swap(array, i, j) {
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
/*
* Basic Bubble sort algorithm (worst case)
*/
function bubbleSortBasic(array) {
var temp;
for(var i = 0; i < array.length; i++) {
for(var j = 1; j < array.length; j++) {
if( array[j - 1] > array[j] ) {
//swap(array, j - 1, j);
temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
return array;
}
console.log( bubbleSortBasic(array.slice()) );
/*
* A little bit enhanced bubble sort
*/
function bubbleSortEnhanced(array) {
var swapped = true;
var temp;
do {
swapped = false;
for(var i = 0; i < array.length; i++) {
if( array[i] !== undefined && array[i + 1] !== undefined && array[i] > array[i + 1] ) {
//swap(array, i, i + 1);
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
swapped = true;
}
}
} while(swapped)
return array;
}
console.log( bubbleSortEnhanced(array.slice()) );
/*
* Another solution that reduces inner iterations and stops when array is actually sorted
*/
function bubbleSort(array) {
var swapped = true,
temp,
j = 0;
while(swapped) {
swapped = false;
j++;
for (var i = 0; i < array.length - j; i++) {
if(array[i] > array[i + 1]) {
//swap(array, i, i + 1);
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
swapped = true;
}
}
}
return array;
}
console.log( bubbleSort(array.slice()) );
/*
* The last solution but with counters
*/
function bubbleSortCount(array) {
var countOuter = 0;
var countInner = 0;
var countSwap = 0;
var swapped = true,
temp,
j = 0;
while(swapped) {
countOuter++;
swapped = false;
j++;
for (var i = 0; i < array.length - j; i++) {
countInner++;
if(array[i] > array[i + 1]) {
//swap(array, i, i + 1);
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
swapped = true;
countSwap++;
}
}
}
console.log('outer:', countOuter, 'inner:', countInner, 'swap:', countSwap);
return array;
}
bubbleSortCount(array.slice());
bubbleSortCount(arrayOrdered.slice());
bubbleSortCount(arrayReversed.slice());