Last active
June 6, 2016 09:55
-
-
Save KomanRudden/72b089cae157eca825fe to your computer and use it in GitHub Desktop.
Bubble sort example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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