Skip to content

Instantly share code, notes, and snippets.

@yndx-handbook
Created September 1, 2022 16:26
Show Gist options
  • Save yndx-handbook/8c0004bf24af1262a03bbf30aa4436e7 to your computer and use it in GitHub Desktop.
Save yndx-handbook/8c0004bf24af1262a03bbf30aa4436e7 to your computer and use it in GitHub Desktop.
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;
}
}
@akuniutka
Copy link

Why there is left <= right in line 7, not left < right? Should we do a loop, when both left and right point at the same element?

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