Created
September 1, 2022 16:26
-
-
Save yndx-handbook/8c0004bf24af1262a03bbf30aa4436e7 to your computer and use it in GitHub Desktop.
This file contains 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
void ShakerSort(vector<int>& values) { | |
if (values.empty()) { | |
return; | |
} | |
int left = 0; | |
int right = values.size() - 1; | |
while (left <= right) { | |
for (int i = right; i > left; --i) { | |
if (values[i - 1] > values[i]) { | |
swap(values[i - 1], values[i]); | |
} | |
} | |
++left; | |
for (int i = left; i < right; ++i) { | |
if (values[i] > values[i + 1]) { | |
swap(values[i], values[i + 1]); | |
} | |
} | |
--right; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why there is
left <= right
in line 7, notleft < right
? Should we do a loop, when bothleft
andright
point at the same element?