-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathctci-bubble-sort.js
More file actions
57 lines (44 loc) · 1.28 KB
/
ctci-bubble-sort.js
File metadata and controls
57 lines (44 loc) · 1.28 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
#!/usr/bin/env node
// Performs a bubble sort, counting the number of swaps made.
//
// https://www.hackerrank.com/challenges/ctci-bubble-sort
'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.replace(/\s*$/, '')
.split('\n')
.map(str => str.replace(/\s*$/, ''));
main();
});
function readLine() {
return inputString[currentLine++];
}
// Complete the countSwaps function below.
function countSwaps(a) {
let swapCount = 0;
for (let i = 0; i < a.length; i += 1) {
for (let j = 0; j < a.length - 1; j += 1) {
const numberA = a[j];
const numberB = a[j + 1];
if (numberA > numberB) {
a[j] = numberB;
a[j + 1] = numberA;
swapCount += 1;
}
}
}
console.log(`Array is sorted in ${swapCount} swaps.`);
console.log('First Element:', a[0]);
console.log('Last Element:', a[a.length - 1]);
}
function main() {
const n = parseInt(readLine(), 10);
const a = readLine().split(' ').map(aTemp => parseInt(aTemp, 10));
countSwaps(a);
}