Q. Write a program to sort an array elements in ascending order using Bubble Sort.



Bubble Sort :- Bubble sort is also known as sinking sort. Bubble sort is a simple sorting algorithm that compares adjacent elements and swaps them if order is wrong.

Ascending Order :- Numbers are said to be in ascending order when they are arranged from smallest to largest number. Such as 5, 9, 13, 17 and 21 are arranged in ascending order.

Bubble Sort Algorithm

START
Step 1: Repeat Step 2 For i = 0 to N
Step 2: Repeat For J = 0 to N - I
Step 3: IF A[J] > A[J+1]
SWAP A[J] and A[J+1]
[END OF INNER LOOP]
[END OF OUTER LOOP
Step 4: EXIT
STOP


Bubble Sort Program

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • #include <stdio.h>
    int main(){
    int temp, i, j, number[5]={6,7,2,1,4};
    /* This is the main logic of bubble sort algorithm 
    */
    for(i = 0 ;i < 5; i++){
    for(j = 0; j < 5-i-1 ; ++j){
    if(number[j] > number[j+1]){
    temp = number[j];
    number[j] = number[j+1];
    number[j+1] = temp;
    }
    }
    }
    printf("Sorted Array: ");
    for(i=0; i < 5; i++)
    printf(" %d",number[i]);
    return 0;
    }
    
    // Bubble sort in C++
    #include <iostream>
    using namespace std;
    void bubble_Sort(int array[], int size)
    {
    for (int i = 0; i < size - 1; ++i)
    {
    for (int i = 0; i < i - i - 1; ++i)
    {
    // To sort in descending order, change > to < in this line.
    if (array[i] > array[i + 1])
    {
    int temp = array[i];
    array[i] = array[i + 1];
    array[i + 1] = temp;
    }
    }
    }
    }
    void print_Array(int array[], int size)
    {
    for (int i = 0; i < size; ++i)
    {
    cout << "  " << array[i];
    }
    cout << "\n";
    }
    int main()
    {
    int number[] = {6, 7, 2, 1, 4};
    int size = sizeof(number) / sizeof(number[0]);
    bubble_Sort(number, size);
    cout << "Sorted Array :\n";
    print_Array(number, size);
    }
    
    // Bubble sort in Java
    import java.util.Arrays;
    class LFC {
    void bubble_Sort(int array[]) {
    int size = array.length;
    for (int i = 0; i < size - 1; i++)
    for (int j = 0; j < size - i - 1; j++)
    // To sort in descending order, change > to < in this line.
    if (array[j] > array[j + 1]) {
    int temp = array[j];
    array[j] = array[j + 1];
    array[j + 1] = temp;
    }
    }
    public static void main(String args[]) {
    int[] number = { 6, 7, 2, 1, 4 };
    LFC bs = new LFC();
    bs.bubble_Sort(number);
    System.out.println("Sorted Array :");
    System.out.println(Arrays.toString(number));
    }
    }
    
    def bubble_Sort(array):
    for i in range(len(array)):
    for j in range(0, len(array) - i - 1):
    # To sort in descending order, change > to < in this line.
    if array[j] > array[j + 1]:
    (array[j], array[j + 1]) = (array[j + 1], array[j])
    number = [ 6, 7, 2, 1, 4 ];
    bubble_Sort(number)
    print('Sorted Array: ')
    print(number)
    
    using System;
    namespace LFC {
    class MySort {
    static void Main(string[] args) {
    int[] arr = { 6, 7, 2, 1, 4 };
    int temp;
    for (int j = 0; j <= arr.Length - 2; j++) {
    for (int i = 0; i <= arr.Length - 2; i++) {
    if (arr[i] > arr[i + 1]) {
    temp= arr[i + 1];
    arr[i + 1] = arr[i];
    arr[i] = temp;
    }
    }
    }
    Console.WriteLine("Sorted Array:");
    foreach (int p in arr)
    Console.Write(p + " ");
    Console.Read();
    }
    }
    }
    
    function bubble_Sort(&$number) 
    { 
    $n = sizeof($number); 
    // Traverse through all array elements 
    for($i = 0; $i < $n; $i++) 
    { 
    $temp = False; 
    // Last i elements are already 
    // in place 
    for ($j = 0; $j < $n - $i - 1; $j++) 
    { 
    if ($number[$j] > $number[$j+1]) 
    { 
    $t = $number[$j]; 
    $number[$j] = $number[$j+1]; 
    $number[$j+1] = $t; 
    $temp = True; 
    } 
    } 
    // IF no two elements were swapped 
    // by inner loop, then break 
    if ($temp == False) 
    break; 
    } 
    } 
    // Driver code to test above 
    $number = array(6, 7, 2, 1, 4);  
    $len = sizeof($number); 
    bubble_Sort($number); 
    echo "Sorted Array : \n"; 
    for($i = 0; $i < $len; $i++) 
    echo $number[$i]." "; 
    
    #include <stdio.h>
    int main(){
    int temp, i, j, number[5]={6,7,2,1,4};
    /* This is the main logic of bubble sort algorithm 
    */
    for(i = 0 ;i < 5; i++){
    for(j = 0; j < 5-i-1 ; ++j){
    if(number[j] > number[j+1]){
    temp = number[j];
    number[j] = number[j+1];
    number[j+1] = temp;
    }
    }
    }
    printf("Sorted Array: ");
    for(i=0;i<5;i++)
    printf(" %d",number[i]);
    return 0;
    }
    

    Output

    Sorted Array: 1 2 4 6 7
    

    Recommended Programs

       Bubble sort program (Descending Order)
       Program to find length of a string