Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit912813b

Browse files
committed
writing expalanatory notes on quick sort Hoare implementation
1 parent25d21eb commit912813b

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

‎Sort/QuickSort/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#Quick Sort
22

3+
The quicksort algorithm basically works by partitioning the entire array, and then recursively partitioning the left and right parts of the array until the entire array is sorted. The left and right parts of the array are determined by the index returns after each partition operation. That index effectively becomes the boundary between the left and right parts of the array.
4+
35
The algorithm for Quicksort is:
46

57
1. Pick a pivot element that divides the list into two sublists.

‎Sort/QuickSort/quick-sort-Hoare.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
// 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-
}
2+
3+
// function swap(array, i, j) {
4+
// var temp = array[i];
5+
// array[i] = array[j];
6+
// array[j] = temp;
7+
// }
8+
9+
constswap=(arr,i,j)=>[arr[i],arr[j]]=[arr[j],arr[i]]
10+
11+
/* In the below quickSortHoare() function, I am invoking the function recursively with the below condition. So once this condtion is no more applicable, that will mean the terminal condition has been reached to stop the recursion.
12+
13+
If left is less than the returned index minus 1 then there are still items on the left to be sorted and quickSort() is called recursively on those items. Likewise, if index is less than the right pointer then there are still items on the right to sort. Once all this is done, the array is returned as the result. */
714

815
functionquicksortHoare(array,left,right){
16+
17+
// To optimize for performance, the array isn’t sorted if it has zero or one items. If there are two or more items in the array then it is partitioned.
18+
if(arr.length<2)returnarr;
19+
920
// 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).
1021
left=left||0;
1122
right=right||array.length-1;
@@ -24,12 +35,20 @@
2435

2536
}
2637

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.
38+
/* A> 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.
39+
40+
B> 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.
41+
42+
C> That is, although the left <= right (which is being made sure by the outer while loop's condition) the actual pair of the array-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.
43+
44+
45+
D> The entire algorithm is just a loop of loops. The outer loop determines when all of the items in the array range have been processed. The two inner loops control movement of the left and right pointers. When both of the inner loops complete, then the pointers are compared to determine if the swap is necessary. After the swap, both pointers are shifted so that the outer loop continues in the right spot.
46+
47+
This partitionHoare function returns the value of the left pointer because this is used to determine where to start partitioning the next time. Keep in mind that the partitioning is happening in place, without creating any additional arrays.
48+
*/
2949

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.
31-
*/
3250
functionpartitionHoare(array,left,right){
51+
3352
varpivot=Math.floor((left+right)/2);
3453

3554
while(left<right){
@@ -49,10 +68,13 @@
4968
returnleft;
5069
}
5170

52-
71+
/* The pivot value is determined by adding together the left and right values and then dividing by 2. Since this value could potentially be a floating-point number, it’s necessary to perform some rounding. In this case, I chose to use the floor function, but I could have just as well use the ceiling function or round function with some slightly different logic. */
5372

5473
/******************* Testing Hoare algorithm *********************/
5574

75+
// First 2 test cases to check the output value of partitionHoare()
76+
console.log(partitionHoare([2,8,6,0,1],0,4))// => 3
77+
console.log(partitionHoare([0,1,2,3,4],0,4))// => 3
5678

5779
letarr=Array.from({length :20},()=>Math.floor(Math.random()*20));
5880
console.log(arr);//printing unsorted array

‎Sort/QuickSort/quick-sort-basic-Recursive-FUTURE-REFERENCE.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
1-
/*The algorithm for Quicksort is:
2-
3-
1. Pick a pivot element that divides the list into two sublists.
4-
5-
2. Reorder the list so that all elements less than the pivot element are placed before
6-
the pivot and all elements greater than the pivot are placed after it.
7-
8-
3. After this partitioning, the pivot is in its final position. This is called the partition operation.
9-
10-
3. Repeat steps 1 and 2 on both the list with smaller elements and the list of larger
11-
elements.
12-
13-
4. Quicksort is a divide and conquer algorithm in the style of merge sort. The basic idea is to find a “pivot” item in the array to compare all other items against, then shift items such that all of the items before the pivot are less than the pivot value and all the items after the pivot are greater than the pivot value. After that, recursively perform the same operation on the items before and after the pivot. */
1+
/* */
142

153
// basic recursive implementation, where pivot is the first element, without using swap function and partition function.
164

175
functionquickSortBasic(array){
6+
7+
// To optimize for performance, the array isn’t sorted if it has zero or one items. If there are two or more items in the array then it is partitioned.
188
if(array.length<2){
199
returnarray;
2010
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp