// Bubble Sort for (int outer = a.length - 1; outer > 0; outer--) { for (int inner = 0; inner < outer; inner++) { if (a[inner] > a[inner + 1]) { int temp = a[inner + 1]; a[inner + 1] = a[inner]; a[inner] = temp; } } }
// Selection Sort for (int outer = a.length - 1; outer > 0; outer--) { int max = outer; for (int inner = 0; inner < outer; inner++) { if (a[inner] > a[max]) { max = inner; } } int temp = a[outer]; a[outer] = a[max]; a[max] = temp; }
// Insertion Sort for (int outer = 1; outer < a.length; outer++) { int temp = a[outer]; int inner = outer; while ((inner > 0) && a[inner - 1] > temp) { a[inner] = a[inner - 1]; inner--; } a[inner] = temp; }
No comments:
Post a Comment