Sort algos: Bubblesort
This commit is contained in:
34
src/com/rkcsd/apps/demo/BubbleSort.java
Normal file
34
src/com/rkcsd/apps/demo/BubbleSort.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package com.rkcsd.apps.demo;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class BubbleSort {
|
||||
static int[] arr = {24, -7, -123456, -25, -64, 555, 3234};
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
|
||||
System.out.println(Arrays.toString(BubbleSort.sort(arr)));
|
||||
|
||||
}
|
||||
|
||||
public static int[] sort(int[] a){
|
||||
int tmp;
|
||||
|
||||
for (int i = a.length - 1; i >= 0; i--) {
|
||||
|
||||
for (int j = 0; j <= i - 1; j++) {
|
||||
|
||||
if (a[j] > a[j + 1]) {
|
||||
tmp = a[j];
|
||||
a[j] = a[j + 1];
|
||||
a[j + 1] = tmp;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user