-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurryingTwo.js
More file actions
145 lines (120 loc) · 3.97 KB
/
curryingTwo.js
File metadata and controls
145 lines (120 loc) · 3.97 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
143
144
145
var toBeSortedArray = [44,22,69,55,66,34,21,34,56,76,43,22,45,12,3,5,6,7,8,9,0];
const pipe = function(...fns) {
return function(x) {
return fns.reduce(function(v, f) {
return f(v);
}, x);
}
};
function curry(fn,arity = fn.length) {
return (function nextCurried(prevArgs){
return function curried(nextArg){
var args = [ ...prevArgs, nextArg ];
//console.log("args are" + args);
if (args.length >= arity) {
return fn( ...args );
}
else {
return nextCurried( args );
}
};
})( [] );
}
function bubbleSortArray(initialArray){
var temp;
for (var j = 0;j<initialArray.length; j++){
for(var i=0; i <initialArray.length; i++){
if(initialArray[i] > initialArray[i+1]){
temp = initialArray[i+1];
initialArray[i+1] = initialArray[i];
initialArray[i] = temp;
}
}
}
return initialArray;
}
function quickSortArray(toSortArray, startIndex, endIndex){
var pivot = startIndex;
var upIncrement = startIndex + 1;
var downIncrement = endIndex;
var temp;
while((downIncrement > 0) && (downIncrement > pivot)){
if((toSortArray[upIncrement]) > toSortArray[pivot]){
if(toSortArray[downIncrement] < toSortArray[pivot]){
if(downIncrement < upIncrement){
temp = toSortArray[pivot];
toSortArray[pivot] = toSortArray[downIncrement];
toSortArray[downIncrement] = temp;
break;
} else{
temp = toSortArray[upIncrement];
toSortArray[upIncrement] = toSortArray[downIncrement];
toSortArray[downIncrement] = temp;
upIncrement++;
downIncrement--;
continue;
}
}else {
downIncrement--;
continue;
}
} else {
upIncrement++;
continue;
}
}
if(downIncrement > pivot){
quickSortArray(toSortArray, pivot, downIncrement);
}
if (upIncrement < endIndex){
quickSortArray(toSortArray, upIncrement, toSortArray.length -1);
}
}
function quickSort(toBeSortedArray){
toBeSortedArray.push(999);
console.log(toBeSortedArray);
quickSortArray(toBeSortedArray, 0, toBeSortedArray.length -1);
console.log(toBeSortedArray);
return toBeSortedArray;
}
//quickSort(toBeSortedArray);
function binarySearchInArray(number, arr, searchIteration = 0){
searchIteration++;
arrLength = arr.length;
if(arr.length > 1){
if(arrLength%2 === 0){
var lowerArray = arr.slice(0,arr.length/2);
var higherArray = arr.slice((arr.length/2)+1, arr.length-1);
} else{
var lowerArray = arr.slice(0,(arr.length-1)/2);
var higherArray = arr.slice(((arr.length-1)/2)+1, arr.length-1);
}
if((number === lowerArray[lowerArray.length-1]) ||
(number === higherArray[0])){
console.log("Number found = " + number + " and iteration " + searchIteration);
}
else if (number < lowerArray[lowerArray.length-1])
binarySearchInArray(number,lowerArray, searchIteration);
else if (number > higherArray[0])
binarySearchInArray(number,higherArray, searchIteration);
else {
console.log("sorry");
}
}
else {
console.log("Number Found");
}
}
// console.log(toBeSortedArray);
// binarySearchInArray(bubbleSortArray(toBeSortedArray),0);
/**
const pipingFunctions = pipe(
bubbleSortArray,
curry(binarySearchInArray)(0)
);
pipingFunctions(toBeSortedArray);*/
//console.log(toBeSortedArray);
const pipingFunctions = pipe(
quickSort,
curry(binarySearchInArray)(0));
pipingFunctions(toBeSortedArray);