forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WaveFormSort.js
98 lines (74 loc) · 2.39 KB
/
WaveFormSort.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
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
/*
Given an unsorted array, sort the array into a wave form.
Wave form - array[0] >= array[1] <= array[2] >= array[3] <= array[4] >= array[5] and so on
Algorithm - 1. Sort the given array
2. Swap all adjacent elements
Time Complexity - O(nLog n), if sorting algorithm like Merge sort is used
*/
// Prompt node package for taking user input
const prompt = require("prompt-sync")({ sigint: true });
function mergeSort(array) {
// Case of only one array element
if (array.length === 1) {
return array;
}
// get the mid point and divide the array in each half
const middle = Math.floor(array.length / 2);
const left = array.slice(0, middle);
const right = array.slice(middle);
// Call the algorithm to compare elements of each sub half
return merge(mergeSort(left), mergeSort(right));
}
function merge(left, right) {
let arr = [];
// Compare elements and add accordingly in arr
while (left.length && right.length) {
if (left[0] < right[0]) {
arr.push(left.shift());
} else {
arr.push(right.shift());
}
}
// return the arr & the left elements from leftArray + rightArray
return [...arr, ...left, ...right];
}
// Perform waveSort
function waveSort(array) {
// Call the merge sort algorithm
let sortedArray = mergeSort(array);
// Swap adjacent elements
for (let i = 0; i < sortedArray.length - 1; i += 2) {
[sortedArray[i], sortedArray[i + 1]] = [sortedArray[i + 1], sortedArray[i]];
}
return sortedArray;
}
/* Workflow of user input */
// Take array length as input
let arrayLength = +prompt("Enter array length - ");
// Check whether the entered value is number or not
if (isNaN(arrayLength)) return console.log("Only numbers are allowed");
// Globally declared array
let array = [];
// Take array items
for (let i = 1; i <= arrayLength; i++) {
array.push(+prompt(`Enter ${i} element - `));
// Check whether the entered value is number or not
if (array.includes(NaN)) return console.log("Only numbers are allowed");
}
console.log("Your array - ", array);
// Call the algorithm
let waveArray = waveSort(array);
console.log("Wave form - ", waveArray);
/*
> node WaveFormSort
Enter array length - 7
Enter 1 element - 62
Enter 2 element - 45
Enter 3 element - 21
Enter 4 element - 80
Enter 5 element - 30
Enter 6 element - 90
Enter 7 element - 11
Your array - [ 62, 45, 21, 80, 30, 90, 11 ]
Wave form - [ 21, 11, 45, 30, 80, 62, 90 ]
*/