Write a program in java to arrange numbers in ascending order using bubble sort technique
Answers
Answered by
1
public class Sample
{
public static void main(String[] args)
{
int demo[] = new int[]{100,200,50,100,80,77};
for(int a=0; a < demo.length; a++)
{ }
bubbleSort(demo);
System.out.println("Output is : ");
for(int a=0; a < demo.length; a++)
{
System.out.print(demo[a] + " ");
}
}
private static void bubbleSort(int[] demo)
{
int n = demo.length;
int b = 0;
for(int a=0; a < n; a++)
{
for(int j=1; j < (n-a); j++)
{
if(demo[j-1] > demo[j])
{
b = demo[j-1];
demo[j-1] = demo[j];
demo[j] = b;
} } } } }
Hope this helps!
{
public static void main(String[] args)
{
int demo[] = new int[]{100,200,50,100,80,77};
for(int a=0; a < demo.length; a++)
{ }
bubbleSort(demo);
System.out.println("Output is : ");
for(int a=0; a < demo.length; a++)
{
System.out.print(demo[a] + " ");
}
}
private static void bubbleSort(int[] demo)
{
int n = demo.length;
int b = 0;
for(int a=0; a < n; a++)
{
for(int j=1; j < (n-a); j++)
{
if(demo[j-1] > demo[j])
{
b = demo[j-1];
demo[j-1] = demo[j];
demo[j] = b;
} } } } }
Hope this helps!
Answered by
2
//bubble sort 1
class BubbleSort{
public static void main(String args[]){
int num[] = {4,2,7,8,5,9,1,6,11,45};
int tempo;
for(int i = 0; i < 10 - 1; i++){
for(int j = i + 1; j < 10; j++){
if(num[i]>num[j]){
tempo = num[i];
num[i] = num[j];
num[j] = tempo;
}
}
}
for(int i = 0; i < 10; i++){
System.out.println(num[i]);
}
}
}
Hope this helps,
ira.
Similar questions
Social Sciences,
8 months ago
Physics,
1 year ago
Math,
1 year ago
Physics,
1 year ago
Physics,
1 year ago