Skip to content

Instantly share code, notes, and snippets.

@sunmeat
Last active December 12, 2021 17:30
Show Gist options
  • Save sunmeat/f92e28fc4a48faf3b2fb to your computer and use it in GitHub Desktop.
Save sunmeat/f92e28fc4a48faf3b2fb to your computer and use it in GitHub Desktop.
bubble sort example for android 8
package com.alex.sorting;
public class JavaApplication1 {
public static void main(String[] args) {
int size = 10;
int ar[] = new int[size];
// before sort
for (int i = 0; i < size; i++) {
ar[i] = (int) (Math.random() * 100);
System.out.print(ar[i] + " ");
}
System.out.print("\n\n");
// start sort
for (int pr = 0; pr < size - 1; pr++) {
for (int i = size - 1; i > 0; i--) {
if (ar[i - 1] > ar[i]) {
int temp = ar[i - 1];
ar[i - 1] = ar[i];
ar[i] = temp;
}
}
}
// after sort
for (int i = 0; i < size; i++) {
System.out.print(ar[i] + " ");
}
System.out.print("\n\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment