Skip to content

Instantly share code, notes, and snippets.

@KomanRudden
Last active June 6, 2016 09:55
Show Gist options
  • Save KomanRudden/72b089cae157eca825fe to your computer and use it in GitHub Desktop.
Save KomanRudden/72b089cae157eca825fe to your computer and use it in GitHub Desktop.
Bubble sort example
package com.permgen;
import java.util.Arrays;
public class PermgenBubbleSortAsce {
public static void main(String[] args) {
int arrayList[] = { 5,3,9,7,1,8 };
System.out.println("\nFinal result:"+Arrays.toString(PermgenBubbleSortAsceMethod(arrayList)));
}
public static int[] PermgenBubbleSortAsceMethod(int[] arr){
int temp;
for(int i=0; i < arr.length; i++){
for(int j=1; j < arr.length; j++){
if(arr[j-1] > arr[j]){
temp=arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
System.out.println((i+1)+"th iteration result: "+Arrays.toString(arr));
}
return arr;
}
}
package com.permgen;
import java.util.Arrays;
public class PermgenBubbleSortDesc {
public static void main(String[] args) {
int arrayList[] = { 3,5,7,9,1,8 };
System.out.println("\nFinal result:"+Arrays.toString(PermgenBubbleSortDescMethod(arrayList)));
}
public static int[] PermgenBubbleSortDescMethod(int[] arr){
int temp;
for(int i=0; i < arr.length; i++){
for(int j=1; j < arr.length; j++){
if(arr[j-1] < arr[j]){
temp=arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
System.out.println((i+1)+"th iteration result: "+Arrays.toString(arr));
}
return arr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment