Skip to content

Instantly share code, notes, and snippets.

@JohnnyFang
Created February 21, 2019 17:41
Show Gist options
  • Save JohnnyFang/54bbf169e41d1e3782fc16808473113e to your computer and use it in GitHub Desktop.
Save JohnnyFang/54bbf169e41d1e3782fc16808473113e to your computer and use it in GitHub Desktop.
BubbleSort
//*******************************************************************
// Welcome to CompileJava!
// If you experience any issues, please contact us ('More Info') -->
// Also, sorry that the "Paste" feature no longer works! GitHub broke
// this (so we'll switch to a new provider): https://blog.github.com\
// /2018-02-18-deprecation-notice-removing-anonymous-gist-creation/
//*******************************************************************
import java.lang.Math; // headers MUST be above the first class
// one class needs to have a main() method
public class HelloWorld
{
static void bubble_sort(int[] myarray)
{
int arr_length = myarray.length;
int temp = 0;
for(int a=0; a < arr_length ;a++){
for(int b=1; b < (arr_length - a); b++){
if(myarray[b-1] > myarray[b]){
// intercambiamos los elementos
temp = myarray[b-1];
myarray[b-1] = myarray[b];
myarray[b] = temp;
}
}
}
}
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
int [] numeros_x_ordenar = {3,1,14,2};
bubble_sort(numeros_x_ordenar);
for(int a=0; a<numeros_x_ordenar.length; a++){
System.out.print(numeros_x_ordenar[a] + " ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment