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

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

Open
prasad-chavan1 wants to merge 3 commits into TheAlgorithms:master
base: master
Choose a base branch
Loading
from prasad-chavan1:master

Conversation

Copy link

@prasad-chavan1 prasad-chavan1 commented Sep 24, 2023

Open in Gitpod
know more

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new JavaScript files are placed inside an existing directory.
  • All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames.
    Example:UserProfile.js is allowed but userprofile.js,Userprofile.js,user-Profile.js,userProfile.js are not
  • All new algorithms have a URL in their comments that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Copy link
Author

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.

Copy link
Collaborator

@appgurueu appgurueu left a 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) {
Copy link
Collaborator

@appgurueu appgurueu Sep 24, 2023

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.

prasad-chavan1 reacted with heart emoji
// order, if order = 1
function bitonicMergeArr (a, low, cnt, dir) {
if (cnt > 1) {
const k = parseInt(cnt / 2)
Copy link
Collaborator

@appgurueu appgurueu Sep 24, 2023

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) }
Copy link
Collaborator

@appgurueu appgurueu Sep 24, 2023

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)
Copy link
Collaborator

@appgurueu appgurueu Sep 24, 2023

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
Copy link
Collaborator

@appgurueu appgurueu Sep 24, 2023

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) {
Copy link
Collaborator

@appgurueu appgurueu Sep 24, 2023

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
Copy link
Collaborator

@appgurueu appgurueu Sep 24, 2023

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'
Copy link
Collaborator

@appgurueu appgurueu Sep 24, 2023

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.

Copy link
Author

prasad-chavan1 commented Sep 24, 2023 via email

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: ***@***.***>
appgurueu reacted with thumbs up emoji

Copy link
Collaborator

@appgurueu appgurueu left a 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?

prasad-chavan1 reacted with confused emoji
Copy link
Author

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..

appgurueu reacted with thumbs up emoji

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@appgurueu appgurueu appgurueu requested changes

@raklaptudirm raklaptudirm Awaiting requested review from raklaptudirm raklaptudirm is a code owner

Requested changes must be addressed to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /