-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Added Bitonic Sort Algorithm #1370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Respected @appgurueu & @raklaptudirm ,I apologize for any inconvenience caused by my recent pull request. It includes code indentation fixes that I'm addressing promptly. Please consider merging the request, and I'll ensure it aligns perfectly with project standards in the next update. Your guidance is appreciated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also add a proper JSDoc comment. Try to reuse the tests of existing sorting algorithms (but don't just copy and paste; simply execute the same tests for multiple sorting algorithms that offer the same API).
* more information: https://en.wikipedia.org/wiki/Bitonic_sorter | ||
|
||
*/ | ||
function compAndSwap (a, i, j, order) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than taking an order
parameter, this should take a comparison function.
// order, if order = 1 | ||
function bitonicMergeArr (a, low, cnt, dir) { | ||
if (cnt > 1) { | ||
const k = parseInt(cnt / 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why use parseInt
instead of cnt >> 1
for floor division by 2?
function bitonicMergeArr (a, low, cnt, dir) { | ||
if (cnt > 1) { | ||
const k = parseInt(cnt / 2) | ||
for (let i = low; i < low + k; i++) { compAndSwap(a, i, i + k, dir) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not inline compAndSwap
?
|
||
function bitonicSort (a, low, cnt, order) { // (arr 0 arrLen AS-Ds-order) | ||
if (cnt > 1) { | ||
const k = parseInt(cnt / 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here (use cnt >> 1
)
|
||
// Calling of bitonicSort func for sorting the entire array | ||
// of length N in ASCENDING order | ||
// here up=1 for ASCENDING & up=0 for DESCENDING |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, I'd prefer a comparator.
} | ||
|
||
// displaying array | ||
function logArray (arr) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function doesn't belong here. It should be removed.
|
||
export { sort } | ||
|
||
// Test Case method |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't belong here.
@@ -0,0 +1,36 @@ | |||
import {sort} from '../BitonicSort' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should use proper Jest tests.
Okay sir it will be ready soon as per your instructions till tomorrow
❤️Have a great evening
…On Sun, Sep 24, 2023, 4:45 PM Lars Müller ***@***.***> wrote:
***@***.**** requested changes on this pull request.
Please also add a proper JSDoc comment. Try to reuse the tests of existing
sorting algorithms (but don't just copy and paste; simply execute the same
tests for multiple sorting algorithms that offer the same API).
------------------------------
In Sorts/BitonicSort.js
<#1370 (comment)>
:
> @@ -0,0 +1,74 @@
+/*
+
+ * JS program for bitonic sort
+ * Bitonic Sort is a parallel sorting algorithm that works by dividing the
+ array into two parts, recursively sorting them, and then merging them in a
+ specific way.
+ * more information: https://en.wikipedia.org/wiki/Bitonic_sorter
+
+ */
+function compAndSwap (a, i, j, order) {
Rather than taking an order parameter, this should take a comparison
function.
------------------------------
In Sorts/BitonicSort.js
<#1370 (comment)>
:
> +
+ */
+function compAndSwap (a, i, j, order) {
+ if ((a[i] > a[j] && order === 1) || (a[i] < a[j] && order === 0)) {
+ // Swapping elements
+ const temp = a[i]
+ a[i] = a[j]
+ a[j] = temp
+ }
+}
+
+// It recursively sorts a bitonic sequence in ascending
+// order, if order = 1
+function bitonicMergeArr (a, low, cnt, dir) {
+ if (cnt > 1) {
+ const k = parseInt(cnt / 2)
Why use parseInt instead of cnt >> 1 for floor division by 2?
------------------------------
In Sorts/BitonicSort.js
<#1370 (comment)>
:
> + */
+function compAndSwap (a, i, j, order) {
+ if ((a[i] > a[j] && order === 1) || (a[i] < a[j] && order === 0)) {
+ // Swapping elements
+ const temp = a[i]
+ a[i] = a[j]
+ a[j] = temp
+ }
+}
+
+// It recursively sorts a bitonic sequence in ascending
+// order, if order = 1
+function bitonicMergeArr (a, low, cnt, dir) {
+ if (cnt > 1) {
+ const k = parseInt(cnt / 2)
+ for (let i = low; i < low + k; i++) { compAndSwap(a, i, i + k, dir) }
Why not inline compAndSwap?
------------------------------
In Sorts/BitonicSort.js
<#1370 (comment)>
:
> +}
+
+// It recursively sorts a bitonic sequence in ascending
+// order, if order = 1
+function bitonicMergeArr (a, low, cnt, dir) {
+ if (cnt > 1) {
+ const k = parseInt(cnt / 2)
+ for (let i = low; i < low + k; i++) { compAndSwap(a, i, i + k, dir) }
+ bitonicMergeArr(a, low, k, dir)
+ bitonicMergeArr(a, low + k, k, dir)
+ }
+}
+
+function bitonicSort (a, low, cnt, order) { // (arr 0 arrLen AS-Ds-order)
+ if (cnt > 1) {
+ const k = parseInt(cnt / 2)
Same here (use cnt >> 1)
------------------------------
In Sorts/BitonicSort.js
<#1370 (comment)>
:
> +
+ // sort in ascending order since order here is 1
+ bitonicSort(a, low, k, 1)
+
+ // sort in descending order since order here is 0
+ bitonicSort(a, low + k, k, 0)
+
+ // Will merge whole sequence in ascending order
+ // since dir=1.
+ bitonicMergeArr(a, low, cnt, order)
+ }
+}
+
+// Calling of bitonicSort func for sorting the entire array
+// of length N in ASCENDING order
+// here up=1 for ASCENDING & up=0 for DESCENDING
Again, I'd prefer a comparator.
------------------------------
In Sorts/BitonicSort.js
<#1370 (comment)>
:
> +
+ // Will merge whole sequence in ascending order
+ // since dir=1.
+ bitonicMergeArr(a, low, cnt, order)
+ }
+}
+
+// Calling of bitonicSort func for sorting the entire array
+// of length N in ASCENDING order
+// here up=1 for ASCENDING & up=0 for DESCENDING
+function sort (a, N, up) {
+ bitonicSort(a, 0, N, up)
+}
+
+// displaying array
+function logArray (arr) {
This function doesn't belong here. It should be removed.
------------------------------
In Sorts/BitonicSort.js
<#1370 (comment)>
:
> +
+// Calling of bitonicSort func for sorting the entire array
+// of length N in ASCENDING order
+// here up=1 for ASCENDING & up=0 for DESCENDING
+function sort (a, N, up) {
+ bitonicSort(a, 0, N, up)
+}
+
+// displaying array
+function logArray (arr) {
+ for (let i = 0; i < arr.length; ++i) { console.log(arr[i] + ' ') }
+}
+
+export { sort }
+
+// Test Case method
This doesn't belong here.
------------------------------
In Sorts/test/BitonicSort.test.js
<#1370 (comment)>
:
> @@ -0,0 +1,36 @@
+import {sort} from '../BitonicSort'
This should use proper Jest tests.
—
Reply to this email directly, view it on GitHub
<#1370 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AXFPHSQARWGOBP7473RJ6KTX4AI3RANCNFSM6AAAAAA5EULERY>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did this PR just change from Bitonic Sort to Polynomial Hash?
Sir actually am too busy...Today is my final theory paper so i didnt made any changes to that algo but instead of that i added another hash algo with proper format..so sorry for that....i'll do it by today or tommorow.. |
know more
Describe your change:
Checklist:
Example:
UserProfile.js
is allowed butuserprofile.js
,Userprofile.js
,user-Profile.js
,userProfile.js
are notFixes: #{$ISSUE_NO}
.