-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Add the Stooge Sort Algorithm #998
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2a09635
Add stooge sort sorting algorithm with included tests
Madiena 0b2153e
Merge branch 'master' of https://github.com/Madiena/Javascript
Madiena a456b30
Add stooge sort sorting algorithm with included tests
Madiena 43ec87f
Add correct url for more information
Madiena e7a05ac
Add time complexity warning
Madiena File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Stooge Sort sorts an array based on divide and conquer principle | ||
* note the exceptionally bad time complexity | ||
* more information: https://en.wikipedia.org/wiki/Stooge_sort | ||
* | ||
*/ | ||
export function stoogeSort (items, leftEnd, rightEnd) { | ||
if (items[rightEnd - 1] < items[leftEnd]) { | ||
const temp = items[leftEnd] | ||
items[leftEnd] = items[rightEnd - 1] | ||
items[rightEnd - 1] = temp | ||
} | ||
const length = rightEnd - leftEnd | ||
if (length > 2) { | ||
const third = Math.floor(length / 3) | ||
stoogeSort(items, leftEnd, rightEnd - third) | ||
stoogeSort(items, leftEnd + third, rightEnd) | ||
stoogeSort(items, leftEnd, rightEnd - third) | ||
} | ||
return items | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { stoogeSort } from '../StoogeSort' | ||
|
||
test('The StoogeSort of the array [1, 6, 4, 7, 2] is [1, 2, 4, 6, 7]', () => { | ||
const arr = [1, 6, 4, 7, 2] | ||
const res = stoogeSort(arr, 0, arr.length) | ||
expect(res).toEqual([1, 2, 4, 6, 7]) | ||
}) | ||
|
||
test('The StoogeSort of the array [] is []', () => { | ||
const arr = [] | ||
const res = stoogeSort(arr, 0, arr.length) | ||
expect(res).toEqual([]) | ||
}) | ||
|
||
test('The StoogeSort of the array [46, 15, 49, 65, 23] is [15, 23, 46, 49, 65]', () => { | ||
const arr = [46, 15, 49, 65, 23] | ||
const res = stoogeSort(arr, 0, arr.length) | ||
expect(res).toEqual([15, 23, 46, 49, 65]) | ||
}) | ||
|
||
test('The StoogeSort of the array [136, 459, 132, 566, 465] is [132, 136, 459, 465, 566]', () => { | ||
const arr = [136, 459, 132, 566, 465] | ||
const res = stoogeSort(arr, 0, arr.length) | ||
expect(res).toEqual([132, 136, 459, 465, 566]) | ||
}) | ||
|
||
test('The StoogeSort of the array [45, 3, 156, 1, 56] is [1, 3, 45, 56, 156]', () => { | ||
const arr = [45, 3, 156, 1, 56] | ||
const res = stoogeSort(arr, 0, arr.length) | ||
expect(res).toEqual([1, 3, 45, 56, 156]) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.