You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// First write the swap function, which is just a helper function to swap values of the array.
2
+
functionswap(array,i,j){
3
+
vartemp=array[i];
4
+
array[i]=array[j];
5
+
array[j]=temp;
6
+
}
7
+
8
+
functionquicksortHoare(array,left,right){
9
+
// left-pointer would be the index of the first element which is 0 and right-pointer would be the index of the last element which would be (length -1).
10
+
left=left||0;
11
+
right=right||array.length-1;
12
+
13
+
varpivot=partitionHoare(array,left,right);
14
+
15
+
if(left<pivot-1){
16
+
quicksortHoare(array,left,pivot-1);
17
+
}
18
+
19
+
if(right>pivot){
20
+
quicksortHoare(array,pivot,right)
21
+
}
22
+
23
+
returnarray;
24
+
25
+
}
26
+
27
+
/* Two indices that start at the ends of the array being partitioned, then move toward each other, until they detect an inversion: a pair of elements, one greater than the pivot, one smaller, that are in the wrong order relative to each other. The inverted elements are then swapped.
28
+
Here the numerical values of left and right is continually getting updated with each inner while loop. But only if the while loop condition gets satisfied. That is, when the while loop condition is unsatisfied, e.g. for the first inner while loop, when array[left] > array[pivot] which means we have found a misplaced pair.
29
+
30
+
That is, although the left <= right (which is being made sure by the outer while loop) the actual elements are not sorted. Meaning a left side element is larger in value than the right side element. So, the code execution then jumps out of the inner while loop and goes right in to execute the swap function.
// By adding 1, I am making the maximum inclusive ( the minimum is inclusive anyway). Because, the Math.random() function returns a floating-point, pseudo-random number in the range from 0 inclusive up to but not including 1
59
+
}
60
+
61
+
vararr=[];
62
+
63
+
for(vari=0;i<10;i++){//initialize a random integer unsorted array
// First write the swap function, which is just a helper function to swap values of the array.
2
+
functionswap(array,i,j){
3
+
vartemp=array[i];
4
+
array[i]=array[j];
5
+
array[j]=temp;
6
+
}
7
+
8
+
functionquicksortLomuto(array,left,right){
9
+
// left-pointer would be the index of the first element which is 0 and right-pointer would be the index of the last element which would be (length -1).
10
+
left=left||0;
11
+
right=right||array.length-1;
12
+
13
+
varpivot=partitionLomuto(array,left,right);
14
+
15
+
if(left<pivot-1){
16
+
quicksortLomuto(array,left,pivot-1);
17
+
}
18
+
19
+
if(right>pivot){
20
+
quicksortLomuto(array,pivot-1,right)
21
+
}
22
+
23
+
returnarray;
24
+
}
25
+
26
+
functionpartitionLomuto(array,left,right){
27
+
// Lomuto algorithm always uses the last element, array[right], for the pivot.
28
+
varpivot=right;
29
+
vari=left;
30
+
31
+
/*The logic under Lomuto is, we start from the leftmost element and keep track of index of smaller (or equal to) elements as j. While traversing, if we find a smaller element, we swap current element with arr[j]. Otherwise we ignore current element.*/
// By adding 1, I am making the maximum inclusive ( the minimum is inclusive anyway). Because, the Math.random() function returns a floating-point, pseudo-random number in the range from 0 inclusive up to but not including 1
47
+
}
48
+
49
+
vararr=[];
50
+
51
+
for(vari=0;i<10;i++){//initialize a random integer unsorted array
// Returns a random integer between min (inclusive) and max (inclusive). Using Math.round() will give a non-uniform distribution, which we dont want in this case.
38
+
39
+
functiongetRandomInt(min,max){
40
+
returnMath.floor(Math.random()*(max-min+1))+min;
41
+
// By adding 1, I am making the maximum inclusive ( the minimum is inclusive anyway). Because, the Math.random() function returns a floating-point, pseudo-random number in the range from 0 inclusive up to but not including 1
42
+
}
43
+
44
+
vararr=[];
45
+
46
+
for(vari=0;i<10;i++){//initialize a random integer unsorted array
0 commit comments