diff --git a/Sort/Java/.classpath b/Sort/Java/.classpath new file mode 100644 index 0000000..3f3893a --- /dev/null +++ b/Sort/Java/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/Sort/Java/.gitignore b/Sort/Java/.gitignore new file mode 100644 index 0000000..0b77c91 --- /dev/null +++ b/Sort/Java/.gitignore @@ -0,0 +1,3 @@ +/InsertionSort.class +/RadixSort.class +/BubbleSort.class diff --git a/Sort/Java/.project b/Sort/Java/.project new file mode 100644 index 0000000..ea54703 --- /dev/null +++ b/Sort/Java/.project @@ -0,0 +1,17 @@ + + + Java + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Sort/Java/BubbleSort.java b/Sort/Java/BubbleSort.java new file mode 100644 index 0000000..06c4b7d --- /dev/null +++ b/Sort/Java/BubbleSort.java @@ -0,0 +1,48 @@ +public class BubbleSort { + + public static void main(String a[]) { + int[] arr1 = { 9, 14, 3, 2, 43, 11, 58, 22 }; + System.out.println("Before Sort"); + for (int i : arr1) { + System.out.print(i + " "); + } + System.out.println(); + + sort(arr1, 0, arr1.length-1);// sorting array using insertion sort + + System.out.println("After Sort"); + for (int i : arr1) { + System.out.print(i + " "); + } + } + + public static void sort(int[] array, int leftIndex, int rightIndex) { + if (array != null && leftIndex < rightIndex && leftIndex >= 0 && rightIndex >= 0 && array.length > 0 + && array.length >= rightIndex) { + + int i = leftIndex; + while (i < rightIndex) { + boolean temTroca = false; + for (int y = leftIndex; y < rightIndex - i; y++) { + if (array[y] > (array[y + 1])) { + swap(array, y, y + 1); + temTroca = true; + } + } + if (!temTroca) { + break; + } + i++; + } + } + } + + public static void swap(int[] array, int i, int j) { + if (array == null) + throw new IllegalArgumentException(); + + int temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } +}