Created
February 5, 2018 03:12
-
-
Save Parithi/8ee804b980b2cdd2e33897063478a2ca to your computer and use it in GitHub Desktop.
Bubble Sort
This file contains 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.parithi; | |
import java.util.Arrays; | |
public class BubbleSort { | |
public static void main(String[] args) { | |
int[] array = {9,4,1,2,7,5,6,3,8}; | |
System.out.println(Arrays.toString(array)); | |
for(int j=0;j<array.length-1;j++) { | |
for (int i = array.length - 1; i > j; i--) { | |
int value1 = array[i]; | |
int value2 = array[i - 1]; | |
if (value1 < value2) { | |
array[i - 1] = value1; | |
array[i] = value2; | |
} | |
} | |
} | |
System.out.println(Arrays.toString(array)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment