javascript-algorithms icon indicating copy to clipboard operation
javascript-algorithms copied to clipboard

📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings

Results 281 javascript-algorithms issues
Sort by recently updated
recently updated
newest added

Fix for issue: Wrong array representation in Heap README.md #640

[{"_id":"62e716fb3f02dc4dab2ad1c8","body":"looks like they fixed it, see: https:\/\/upload.wikimedia.org\/wikipedia\/commons\/3\/38\/Max-Heap.svg You can close this one :)","issue_id":1659297582713,"origin_id":991418294,"user_origin_id":13461315,"create_time":1639190829,"update_time":1639190829,"id":1659311867336,"updated_at":"2022-07-31T23:57:47.335000Z","created_at":"2022-07-31T23:57:47.335000Z"}] comment

I have fixed the [issue](https://github.com/trekhleb/javascript-algorithms/issues/640) regarding the array representation of HEAP. Kindly review. Thanks

In the `README.es-ES.md` file to explain the linked list, there were many typos in the description of the algorithm.

ran lint and test with no issues

Inserting, removing, accessing, modifying, by index, on linked list

[{"_id":"62e716fb3f02dc4dab2ad1ca","body":"Hi @willow-iam, I decided to work on the Linked-Lists functions you asked for.\r\n\r\nI created this pull request to learn more about open source for a Intro to software Engineering course I am taking. This is my first open source contribution so I hope it gets accepted! \r\n\r\nI created functions for remove (deleteAtIndex), get (findByIndex) and set (setAtIndex). I did not create the insert function as that is already an existing function for linked-lists. \r\n\r\nI also created test cases for all of these functions to ensure they work optimally and correctly!","issue_id":1659297582722,"origin_id":1024948306,"user_origin_id":72417965,"create_time":1643475831,"update_time":1643475846,"id":1659311867633,"updated_at":"2022-07-31T23:57:47.632000Z","created_at":"2022-07-31T23:57:47.632000Z"},{"_id":"65946ff75799de9564123d55","body":"can i contribute to this issue ??","issue_id":1659297582722,"origin_id":1509846499,"user_origin_id":58464900,"create_time":1681567525,"update_time":1681567525,"id":1704226807304,"updated_at":"2024-01-02T20:20:07.304000Z","created_at":"2024-01-02T20:20:07.304000Z"}] comment

``` Insert(head, index, value) Pre: head is the head node in the list Post: the list at the new index is the value; all items in the list are shifted...

Index methods ll

[{"_id":"65946e7b5799de9564123d3e","body":"can i contribute to this issue ??\r\n","issue_id":1659297582724,"origin_id":1509846273,"user_origin_id":58464900,"create_time":1681567489,"update_time":1681567489,"id":1704226427650,"updated_at":"2024-01-02T20:13:47.650000Z","created_at":"2024-01-02T20:13:47.650000Z"}] comment

Hi, I created the algorithms for removing, accessing, and modifying at an specific linked-list indexes for the issue #835 . I tried my best to replicate the code strucutre of...

Adding of a method that get the successor of a node inside the birany search tree

[{"_id":"62e716fc3f02dc4dab2ad1cb","body":"# [Codecov](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/589?src=pr&el=h1) Report\n> Merging [#589](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/589?src=pr&el=desc) (4917139) into [master](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/commit\/1b0e27ab86dbae97b99620db3ecacffafd17d98a?el=desc) (1b0e27a) will **not change** coverage.\n> The diff coverage is `100.00%`.\n\n[![Impacted file tree graph](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/589\/graphs\/tree.svg?width=650&height=150&src=pr&token=2jOOUBhvZL)](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/589?src=pr&el=tree)\n\n```diff\n@@ Coverage Diff @@\n## master #589 +\/- ##\n=========================================\n Coverage 100.00% 100.00% \n=========================================\n Files 153 154 +1 \n Lines 2645 2649 +4 \n Branches 438 439 +1 \n=========================================\n+ Hits 2645 2649 +4 \n```\n\n\n| [Impacted Files](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/589?src=pr&el=tree) | Coverage \u0394 | |\n|---|---|---|\n| [...greater-than-number\/powerOfTwoGreaterThanNumber.js](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/589\/diff?src=pr&el=tree#diff-c3JjL2FsZ29yaXRobXMvbWF0aC9wb3dlci1vZi10d28tZ3JlYXRlci10aGFuLW51bWJlci9wb3dlck9mVHdvR3JlYXRlclRoYW5OdW1iZXIuanM=) | `100.00% <100.00%> (\u00f8)` | |\n\n------\n\n[Continue to review full report at Codecov](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/589?src=pr&el=continue).\n> **Legend** - [Click here to learn more](https:\/\/docs.codecov.io\/docs\/codecov-delta)\n> `\u0394 = absolute <relative> (impact)`, `\u00f8 = not affected`, `? = missing data`\n> Powered by [Codecov](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/589?src=pr&el=footer). Last update [1b0e27a...4917139](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/589?src=pr&el=lastupdated). Read the [comment docs](https:\/\/docs.codecov.io\/docs\/pull-request-comments).\n","issue_id":1659297582727,"origin_id":743787013,"user_origin_id":22429695,"create_time":1607793806,"update_time":1607866396,"id":1659311868186,"updated_at":"2022-07-31T23:57:48.185000Z","created_at":"2022-07-31T23:57:48.185000Z"},{"_id":"62e716fc3f02dc4dab2ad1cd","body":"How can I complete my checks ?, I remark that I need to do the **continuous integration** but how can I do it ?\r\nThanks","issue_id":1659297582727,"origin_id":743975703,"user_origin_id":68383483,"create_time":1607849816,"update_time":1607849816,"id":1659311868190,"updated_at":"2022-07-31T23:57:48.189000Z","created_at":"2022-07-31T23:57:48.189000Z"},{"_id":"62e716fc3f02dc4dab2ad1ce","body":"To be honest, this looks more like basic logarithm operations for a math class and less like a CS algorithm for this repo. Maybe if the code found the first `1` in the binary representation and cleared bits on the right?","issue_id":1659297582727,"origin_id":743997141,"user_origin_id":7661457,"create_time":1607860747,"update_time":1607860747,"id":1659311868192,"updated_at":"2022-07-31T23:57:48.192000Z","created_at":"2022-07-31T23:57:48.192000Z"},{"_id":"62e716fc3f02dc4dab2ad1cf","body":"For you I should not use the logarithm operation ? because this looks more like the math class ? ","issue_id":1659297582727,"origin_id":744008896,"user_origin_id":68383483,"create_time":1607866505,"update_time":1607866505,"id":1659311868195,"updated_at":"2022-07-31T23:57:48.195000Z","created_at":"2022-07-31T23:57:48.195000Z"},{"_id":"62e716fc3f02dc4dab2ad1d0","body":"I'm not commenting on the code itself, I'm commenting on its inclusion in the project which has the usual bunch of CS algorithms. Calculating the \"next power of two\" is just applying the well-known logarithm function. In my opinion, it doesn't fit here. But I'm not the maintainer, just giving my two cents.","issue_id":1659297582727,"origin_id":744030972,"user_origin_id":7661457,"create_time":1607876176,"update_time":1607876176,"id":1659311868198,"updated_at":"2022-07-31T23:57:48.197000Z","created_at":"2022-07-31T23:57:48.197000Z"},{"_id":"62e716fc3f02dc4dab2ad1d1","body":"Ok. I understand that you say. But I wanted to do it because I have another contribution for this repository and which use it, then I have seen better to separate it.\r\n\r\nThis is my first pull request. I am a beginner, I would like to learn about git GitHub and JavaScript.","issue_id":1659297582727,"origin_id":744032958,"user_origin_id":68383483,"create_time":1607877087,"update_time":1607877087,"id":1659311868201,"updated_at":"2022-07-31T23:57:48.200000Z","created_at":"2022-07-31T23:57:48.200000Z"}] comment

I have added one file to your repository, this file contains a function which take a number and returns the power of two which is directly greater than the given...

Bit Reversal missing?

[{"_id":"62e716fc3f02dc4dab2ad1d2","body":"Hi i am a fresher looking for some opensource contribution can u help me out i want to contribute to your project and can u assign me this issue:)","issue_id":1659297582730,"origin_id":1019067000,"user_origin_id":72294477,"create_time":1642829738,"update_time":1642829738,"id":1659311868495,"updated_at":"2022-07-31T23:57:48.495000Z","created_at":"2022-07-31T23:57:48.495000Z"}] comment

Something along the lines of: ``` function bitReverse(num){ var result = 0; for (var i = 0; i < 8; i++) { result = result * 2 + (num %...

traverse linked list is easy but uninteresting. You must add new variable and use while.So why not add a method to return a iterator.Then we can use for of to...

Provide possible paths from A to B in a directed graph

[{"_id":"62e716fc3f02dc4dab2ad1d3","body":"The list could easily be infinite for a simple `A <-> B`. You'd need to define the problem better.","issue_id":1659297582736,"origin_id":1010037655,"user_origin_id":7661457,"create_time":1641912596,"update_time":1641912596,"id":1659311868815,"updated_at":"2022-07-31T23:57:48.815000Z","created_at":"2022-07-31T23:57:48.815000Z"},{"_id":"62e716fc3f02dc4dab2ad1d4","body":"A first step would be to find all possible cycles in a graph. How: A first approach is to depth-search from an origin node: If one of the neighbors to current node belongs to the current route, persist the cycle in a list and backtrack it, removing from LocalPathList;\r\n\r\nA second step is to go along a route with inbetween cycles: every cycle may be traversed only once along the same route.\r\n\r\nExample: Given a graph with adjacency matrix given by object {'A': ['B'], 'B\": ['C'], 'C': ['D', 'E', 'F'], 'D': [], 'E': ['B'], 'F': ['B']}, the possible paths from A to D must be ['ABCD', 'ABCEBD', 'ABCFBD'].\r\n\r\n\r\n\r\n","issue_id":1659297582736,"origin_id":1010079306,"user_origin_id":13961685,"create_time":1641915119,"update_time":1641915119,"id":1659311868817,"updated_at":"2022-07-31T23:57:48.817000Z","created_at":"2022-07-31T23:57:48.817000Z"}] comment

Hi, I would like to know if you have an algorithm to find all possible paths (cyclic or acyclic) in a directed graph from certain node A to B. Although...

choose pivot from median of three elements for quicksort

[{"_id":"62e716fd3f02dc4dab2ad1d5","body":"Test failures seem to be unrelated? Not sure what's going on there.","issue_id":1659297582739,"origin_id":413908008,"user_origin_id":579233,"create_time":1534520965,"update_time":1534520965,"id":1659311869187,"updated_at":"2022-07-31T23:57:49.186000Z","created_at":"2022-07-31T23:57:49.186000Z"},{"_id":"62e716fd3f02dc4dab2ad1d7","body":"I think it is critical to choose appropriate pivot. If you fix the conflict, it would be really helpful!","issue_id":1659297582739,"origin_id":445129518,"user_origin_id":29746520,"create_time":1544161499,"update_time":1544161499,"id":1659311869190,"updated_at":"2022-07-31T23:57:49.190000Z","created_at":"2022-07-31T23:57:49.190000Z"},{"_id":"62e716fd3f02dc4dab2ad1d9","body":"![image](https:\/\/user-images.githubusercontent.com\/29746520\/49629830-cbd56680-fa2e-11e8-8920-1aee32960acb.png)\r\nThe code bot says that you exceeded the maximum size of array. I hope it would be helpful!","issue_id":1659297582739,"origin_id":445129776,"user_origin_id":29746520,"create_time":1544161597,"update_time":1544161597,"id":1659311869193,"updated_at":"2022-07-31T23:57:49.193000Z","created_at":"2022-07-31T23:57:49.193000Z"},{"_id":"62e716fd3f02dc4dab2ad1da","body":"# [Codecov](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171?src=pr&el=h1) Report\n> Merging [#171](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171?src=pr&el=desc) into [master](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/commit\/d9946c12497bc5f28ac5788cdd7f46bae777f2a1?src=pr&el=desc) will **not change** coverage.\n> The diff coverage is `100%`.\n\n[![Impacted file tree graph](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171\/graphs\/tree.svg?width=650&token=2jOOUBhvZL&height=150&src=pr)](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171?src=pr&el=tree)\n\n```diff\n@@ Coverage Diff @@\n## master #171 +\/- ##\n=====================================\n Coverage 100% 100% \n=====================================\n Files 147 147 \n Lines 2590 2595 +5 \n Branches 432 434 +2 \n=====================================\n+ Hits 2590 2595 +5\n```\n\n\n| [Impacted Files](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171?src=pr&el=tree) | Coverage \u0394 | |\n|---|---|---|\n| [src\/algorithms\/sorting\/quick-sort\/QuickSort.js](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171\/diff?src=pr&el=tree#diff-c3JjL2FsZ29yaXRobXMvc29ydGluZy9xdWljay1zb3J0L1F1aWNrU29ydC5qcw==) | `100% <100%> (\u00f8)` | :arrow_up: |\n| [src\/algorithms\/graph\/kruskal\/kruskal.js](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171\/diff?src=pr&el=tree#diff-c3JjL2FsZ29yaXRobXMvZ3JhcGgva3J1c2thbC9rcnVza2FsLmpz) | `100% <100%> (\u00f8)` | :arrow_up: |\n\n------\n\n[Continue to review full report at Codecov](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171?src=pr&el=continue).\n> **Legend** - [Click here to learn more](https:\/\/docs.codecov.io\/docs\/codecov-delta)\n> `\u0394 = absolute <relative> (impact)`, `\u00f8 = not affected`, `? = missing data`\n> Powered by [Codecov](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171?src=pr&el=footer). Last update [d9946c1...478abba](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171?src=pr&el=lastupdated). Read the [comment docs](https:\/\/docs.codecov.io\/docs\/pull-request-comments).\n","issue_id":1659297582739,"origin_id":446272685,"user_origin_id":22429695,"create_time":1544546612,"update_time":1584514284,"id":1659311869196,"updated_at":"2022-07-31T23:57:49.195000Z","created_at":"2022-07-31T23:57:49.195000Z"},{"_id":"62e716fd3f02dc4dab2ad1db","body":"# [Codecov](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171?src=pr&el=h1) Report\n> Merging [#171](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171?src=pr&el=desc) into [master](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/commit\/d9946c12497bc5f28ac5788cdd7f46bae777f2a1?src=pr&el=desc) will **decrease** coverage by `0.41%`.\n> The diff coverage is `100%`.\n\n[![Impacted file tree graph](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171\/graphs\/tree.svg?width=650&token=2jOOUBhvZL&height=150&src=pr)](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171?src=pr&el=tree)\n\n```diff\n@@ Coverage Diff @@\n## master #171 +\/- ##\n==========================================\n- Coverage 100% 99.58% -0.42% \n==========================================\n Files 147 128 -19 \n Lines 2590 2429 -161 \n Branches 432 413 -19 \n==========================================\n- Hits 2590 2419 -171 \n- Misses 0 9 +9 \n- Partials 0 1 +1\n```\n\n\n| [Impacted Files](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171?src=pr&el=tree) | Coverage \u0394 | |\n|---|---|---|\n| [src\/algorithms\/sorting\/quick-sort\/QuickSort.js](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171\/diff?src=pr&el=tree#diff-c3JjL2FsZ29yaXRobXMvc29ydGluZy9xdWljay1zb3J0L1F1aWNrU29ydC5qcw==) | `100% <100%> (\u00f8)` | :arrow_up: |\n| [src\/algorithms\/graph\/kruskal\/kruskal.js](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171\/diff?src=pr&el=tree#diff-c3JjL2FsZ29yaXRobXMvZ3JhcGgva3J1c2thbC9rcnVza2FsLmpz) | `44.44% <0%> (-55.56%)` | :arrow_down: |\n| [...algorithms\/sorting\/selection-sort\/SelectionSort.js](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171\/diff?src=pr&el=tree#diff-c3JjL2FsZ29yaXRobXMvc29ydGluZy9zZWxlY3Rpb24tc29ydC9TZWxlY3Rpb25Tb3J0Lmpz) | `100% <0%> (\u00f8)` | :arrow_up: |\n| [...hms\/math\/euclidean-algorithm\/euclideanAlgorithm.js](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171\/diff?src=pr&el=tree#diff-c3JjL2FsZ29yaXRobXMvbWF0aC9ldWNsaWRlYW4tYWxnb3JpdGhtL2V1Y2xpZGVhbkFsZ29yaXRobS5qcw==) | `100% <0%> (\u00f8)` | :arrow_up: |\n| [src\/data-structures\/stack\/Stack.js](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171\/diff?src=pr&el=tree#diff-c3JjL2RhdGEtc3RydWN0dXJlcy9zdGFjay9TdGFjay5qcw==) | `100% <0%> (\u00f8)` | :arrow_up: |\n| [src\/utils\/comparator\/Comparator.js](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171\/diff?src=pr&el=tree#diff-c3JjL3V0aWxzL2NvbXBhcmF0b3IvQ29tcGFyYXRvci5qcw==) | `100% <0%> (\u00f8)` | :arrow_up: |\n| [src\/algorithms\/sorting\/bubble-sort\/BubbleSort.js](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171\/diff?src=pr&el=tree#diff-c3JjL2FsZ29yaXRobXMvc29ydGluZy9idWJibGUtc29ydC9CdWJibGVTb3J0Lmpz) | `100% <0%> (\u00f8)` | :arrow_up: |\n| [...rc\/algorithms\/search\/binary-search\/binarySearch.js](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171\/diff?src=pr&el=tree#diff-c3JjL2FsZ29yaXRobXMvc2VhcmNoL2JpbmFyeS1zZWFyY2gvYmluYXJ5U2VhcmNoLmpz) | `100% <0%> (\u00f8)` | :arrow_up: |\n| [src\/data-structures\/trie\/Trie.js](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171\/diff?src=pr&el=tree#diff-c3JjL2RhdGEtc3RydWN0dXJlcy90cmllL1RyaWUuanM=) | `100% <0%> (\u00f8)` | :arrow_up: |\n| [src\/data-structures\/heap\/MinHeap.js](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171\/diff?src=pr&el=tree#diff-c3JjL2RhdGEtc3RydWN0dXJlcy9oZWFwL01pbkhlYXAuanM=) | `100% <0%> (\u00f8)` | :arrow_up: |\n| ... and [32 more](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171\/diff?src=pr&el=tree-more) | |\n\n------\n\n[Continue to review full report at Codecov](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171?src=pr&el=continue).\n> **Legend** - [Click here to learn more](https:\/\/docs.codecov.io\/docs\/codecov-delta)\n> `\u0394 = absolute <relative> (impact)`, `\u00f8 = not affected`, `? = missing data`\n> Powered by [Codecov](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171?src=pr&el=footer). Last update [d9946c1...f4668db](https:\/\/codecov.io\/gh\/trekhleb\/javascript-algorithms\/pull\/171?src=pr&el=lastupdated). Read the [comment docs](https:\/\/docs.codecov.io\/docs\/pull-request-comments).\n","issue_id":1659297582739,"origin_id":446272686,"user_origin_id":22429695,"create_time":1544546612,"update_time":1544546612,"id":1659311869198,"updated_at":"2022-07-31T23:57:49.198000Z","created_at":"2022-07-31T23:57:49.198000Z"},{"_id":"62e716fd3f02dc4dab2ad1dc","body":"Should be fixed -- I wasn't choosing pivots the right way, and it turned out Kruskal's algorithm was broken...","issue_id":1659297582739,"origin_id":446296853,"user_origin_id":579233,"create_time":1544550705,"update_time":1544550705,"id":1659311869201,"updated_at":"2022-07-31T23:57:49.201000Z","created_at":"2022-07-31T23:57:49.201000Z"},{"_id":"62e716fd3f02dc4dab2ad1dd","body":"@trekhleb Could you have a look please? This makes quicksort better and fixes a bug in Kruskal's algorithm that breaks it in some cases.","issue_id":1659297582739,"origin_id":446308946,"user_origin_id":579233,"create_time":1544552862,"update_time":1544552862,"id":1659311869203,"updated_at":"2022-07-31T23:57:49.203000Z","created_at":"2022-07-31T23:57:49.203000Z"},{"_id":"62e716fd3f02dc4dab2ad1df","body":"> The method for choosing the pivot as implemented at the moment results in degenerate behaviour when the array is already sorted or reverse sorted. A better way is to choose the median of three elements (first, mid, last) as the pivot. See Robert Sedgewick. Implementing Quicksort Programs. Commun. ACM, 1(10):847\u2013857, October 1978.\r\n\r\nWhat a nice improvement. @larskotthoff Thanks a lot.\r\n\r\nThe fact that this PR is not merged in is a loss for the repository.","issue_id":1659297582739,"origin_id":997572174,"user_origin_id":22052875,"create_time":1639972758,"update_time":1639972758,"id":1659311869206,"updated_at":"2022-07-31T23:57:49.206000Z","created_at":"2022-07-31T23:57:49.206000Z"},{"_id":"62e716fd3f02dc4dab2ad1e1","body":"The repo is mostly dead.","issue_id":1659297582739,"origin_id":1010065651,"user_origin_id":7661457,"create_time":1641914291,"update_time":1641914291,"id":1659311869209,"updated_at":"2022-07-31T23:57:49.209000Z","created_at":"2022-07-31T23:57:49.209000Z"}] comment

The method for choosing the pivot as implemented at the moment results in degenerate behaviour when the array is already sorted or reverse sorted. A better way is to choose...