Welcome To The World Of Hacking


Learn Hacking|Teach Hacking|Learn To Secure|Learn To Code

Sunday

JAVA PROGRAMMING TUTORIAL 43 : BUBBLE SORT

//Bubble sort comapare each pair of adjacent items
//After comparison, it swaps them if they are in wrong order

import java.util.Scanner;

class HackDefence{

public static void main(String []args){

Scanner input = new Scanner(System.in);

System.out.println("Enter the number of integer:");

int i= input.nextInt();

int array[] = new int[i];

System.out.println("Enter "+ i + " integers");

int j;

for( j =0;j<i;j++)

array[j] = input.nextInt();

for(j=0;j<(i-1);j++){

for(int k=0;k<i-j-1;k++){

if(array[k]>array[k+1]){

int swap = array[k];

array[k] = array[k+1];

array[k+1] = swap;

}

}

}

System.out.println("Sorted list of numbers");

for(j=0;j<i;j++)

System.out.println(array[j]);

}

}
(my facebook profile link: https://www.facebook.com/dipankar.choudhury1)

OUTPUT:

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...