Skip to content

Snippets-Bubblesort need to adjust boundaries. #75

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
Suiyueemperor opened this issue Nov 10, 2023 · 0 comments
Open

Snippets-Bubblesort need to adjust boundaries. #75

Suiyueemperor opened this issue Nov 10, 2023 · 0 comments
Labels
epic: snippet info: help wanted Extra attention is needed resolution: won't fix This will not be worked on type: bug Something isn't working

Comments

@Suiyueemperor
Copy link

 public static void bubbleSort(int[] arr) {
    var lastIndex = arr.length - 1;
    for(var j = 0; j < lastIndex; j++) {
      for(var i = 0; i < lastIndex - j; i++) {
        if(arr[i] > arr[i + 1]) {
          var tmp = arr[i];
          arr[i] = arr[i + 1];
          arr[i + 1] = tmp;
        }
      }
    }
  }

The outermost loop can be run at most n-1 times, so the changes are as follows

for(var j = 0; j < lastIndex - 1; j++) {
      for(var i = 0; i < lastIndex - j - 1; i++) {
@iluwatar iluwatar added type: bug Something isn't working info: help wanted Extra attention is needed epic: snippet labels Nov 25, 2023
@iluwatar iluwatar added the resolution: won't fix This will not be worked on label Mar 24, 2024
@iluwatar iluwatar moved this from Todo to Done in Java Design Patterns project Mar 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
epic: snippet info: help wanted Extra attention is needed resolution: won't fix This will not be worked on type: bug Something isn't working
Projects
Development

No branches or pull requests

2 participants