Skip to content

Commit

Permalink
bubble sort: same as before
Browse files Browse the repository at this point in the history
  • Loading branch information
hammadsaedi committed Oct 16, 2023
1 parent 806abd1 commit 3a64d7a
Showing 1 changed file with 32 additions and 38 deletions.
70 changes: 32 additions & 38 deletions src/main/java/algorithm/BubbleSortSnippet.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* MIT License
*
* Copyright (c) 2017-2023 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* MIT License
*
* Copyright (c) 2017-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
@@ -22,35 +22,29 @@
* SOFTWARE.
*/

package algorithm;

/**
* BubbleSortSnippet.
*/
public class BubbleSortSnippet {

/**
* Sort an array with bubbleSort algorithm.
*
* @param arr array to sort
*/
public static void bubbleSort(int[] arr) {
int lastIndex = arr.length - 1;
boolean swapped = true;

while (swapped) {
swapped = false;
package algorithm;

for (int i = 0; i < lastIndex; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = true;
}
}

lastIndex--;
}
}
}
/**
* BubbleSortSnippet.
*/
public class BubbleSortSnippet {

/**
* Sort an array with bubbleSort algorithm.
*
* @param arr array to sort
*/
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;
}
}
}
}
}

0 comments on commit 3a64d7a

Please sign in to comment.