-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path4-apr.js
54 lines (49 loc) · 982 Bytes
/
4-apr.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
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
// // second maximum salary.
let employees = [
{
name: 'babu',
salary: 25000
},
{
name: 'bahubali',
salary: 86000
},
{
name: 'puspaa',
salary: 100000
},
{
name: 'Nobita',
salary: 1000
}
]
console.log(bubbleSort(employees, employees.length, 3))
function bubbleSort(arr, n, k) {
for(let i=0;i<k;i++) {
for(let j=0;j<n-i-1;j++) {
if(arr[j].salary > arr[j+1].salary) {
swap(arr, j, j+1);
}
}
}
return arr[n-k];
}
function swap(arr, i, j) {
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// insertion sort
function insertionSort(arr, n) {
for(let i=1;i<n;i++) {
let key = arr[i];
let j = i-1;
while(j>=0 && arr[j] > key) {
arr[j+1] = arr[j];
j--;
}
arr[j+1] = key;
}
return arr;
}
console.log(insertionSort([1,4,7,8,2], 5))