Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Sort/Java/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path=""/>
<classpathentry kind="output" path=""/>
</classpath>
3 changes: 3 additions & 0 deletions Sort/Java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/InsertionSort.class
/RadixSort.class
/BubbleSort.class
17 changes: 17 additions & 0 deletions Sort/Java/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Java</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
48 changes: 48 additions & 0 deletions Sort/Java/BubbleSort.java
Original file line number Diff line number Diff line change
@@ -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;
}
}