Bubble Sort in Java
Example:
Bubble Sort is the Sort Alogrithm.In Bubble Sort first we take the Unsorted Array and then Sort the Array by using Bubble Sort Alogrithm.
- Bubble sort starts with very first two elements, comparing them to check which one is greater.we have two values first values is the
x[j]
Position and Second value is in thex[j+1]
Position - When the Values are swapped then the Second value 5 at the consider as
x[j]
Position and third value 2 isx[j+1]
- The third value 5 is compare with the fourth value 1. 5 is in
x[j]
poisition and 1 isx[j+1]
position. The condition is Checkedx[j] > x[j+1]
then value are swapped. - After values are swapped then the Fourth value is 5 and Six value is 3. The 5 value at the
x[j]
poisition and 3 isx[j+1]
position.The condition is Checkedx[j] > x[j+1]
then value are swapped. - After Swapping the value 5 and 3 then you can See that the Bubble Sort First Iteration is Completed the value five is arrived on their path.
- In Second Iteration You can Compare the first two values if the first value is greater then second value then values are swapped otherwise values are not swapped. the first value 5 position is
x[j]
and Secodn value 2 isx[j+1]
position.The condition is Checkedx[j] > x[j+1]
- After Swapping the Values then the second condition is checked. The second value 4 is compare with third value 1. the second value 4 is in the
x[j]
Position and the third value 1 is atx[j+1]
Position.The condition is Checkedx[j] > x[j+1]
- After Swapping the values the the third condition is checked. The third value 4 is compare with fourth value 3. the third value 4 is in the
x[j]
Position and the fourth value 3 is atx[j+1]
Position.The condition is Checkedx[j] > x[j+1]
- At the End the Fourth Condition is checked.The Four position value are 4 then five position value is 5. The condition is again checked again. Then the condition is false the values at the Second Iteration is Completed are sorted.Then again the third iteratioin is start.
- After completed the second iteration then Third iteration is start.In this iteration is the outer loop is start and the Four time inner loop are executed.The First Value 2 is Compare with the Second Value is 1. After Checked the condition again the first value 2 is in
x[j]
Position and the second value 1 is atx[j+1]
Position.The condition is Checkedx[j] > x[j+1]
- After Swapping the value the second value is the 2 and third value is 3.the second value 2 is in
x[j]
Position and the third value 3 is atx[j+1]
Position.The condition is Checkedx[j] > x[j+1]
- Then you see your values are swapped.
In Case the First Value is Greater then Second Value then the Values are Swapped.In this array 5 is greater then 4 then values are swapped.
In this case checked the value if the 5 is greater then 2 then value are swapped.
In this case the 5 is greater then 2 the value are swapped.
In this case the 4 is greater then 1 the value are swapped.
In this case the 4 is greater then 3 the value are swapped.
In this case the 2 is not greater then 3 the value are not swapped.
Final Result :
import java.util.*;
class Sort{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
int i,j,temp;
System.out.println("Enter Total Value");
int n=input.nextInt(); //Input Size of the Array
int[] x = new int[n];
System.out.println("Enter "+ n+" Value");
for(i=0;i<n;i++){
int a=input.nextInt();
x[i]=a;
}
for(i=0;i<n; i++){
for(j=0;j<n-1; j++){
if(x[j]>x[j+1]){ //Swap Values
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
}
}
System.out.println("Array Sorting");
for(i=0;i<n; i++){
System.out.println(x[i]);
}
}
}
Bubble Sort in Java (2nd Method)
class secondMethod{
public static void main(String [] args){
int x[] = {5,3,1,2,4};
int i,j,temp,n;
n = x.length; // Calculate the length of the Array
for(i=0;i<n; i++){
for(j=1;j<n; j++){
if(x[j]<x[j-1]){
temp=x[j];
x[j]=x[j-1];
x[j-1]=temp;
}
}
}
System.out.println("Array Sorting");
for(i=0;i<n; i++){
System.out.println(x[i]); //Print Swap Values
}
}
}
Bubble Sort in C++
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
int main(){
system ("color 2F"); //Change the background in the Output Layout
int number,size,array,i,j;
cout<<"Enter the Size of Array =";
cin>>size;
int x[size];
cout<h;<h;"Enter the "<<size<<" Number \n";
for(i=0; i<size; i++){
cout<<"x["<<i<<"] = ";
cin>>array;
x[i]= array;
}
for(i=0; i<size; i++){
for(j=0; j<size-1; j++){
if(x[j]>x[j+1]){
int temp;
temp = x[j];
x[j] = x[j+1];
x[j+1] = temp;
}
}
}
cout<<"Sorted Array\n";
for(i=0; i<size; i++){
cout<<x[i]<<",";
}
getch();
}
Post a Comment