Program for bubble sort a given array of numbers using C/C++/Python/Java

Bubble sort–  In this, sorting begins from the first element and checks the first and second elements. If the first element is greater than the second element it swaps them. This process continues until the last element.

In C:

We ask the user to input the elements in an array and check if the first element is larger than the second element then swap them using the temp variable.

#include <stdio.h>

int main(){

    int arr[50], n, x, y, t;    

    printf("Enter the Number of Elements you want in the array: ");
    scanf("%d", &n);  
    printf(" Enter the Value of Elements : \n"); //press enter after entering the value
    for(x = 0; x < n; x++)
          scanf("%d", &arr[x]);

    for(x = 0; x < n - 1; x++){       

        for(y = 0; y < n - x - 1; y++){          

            if(arr[y] > arr[y + 1]){               

                t = arr[y];

                arr[y] = arr[y + 1];

                arr[y + 1] = t;

            }

        }

    }

    printf("Array after implementing bubble sort: ");

    for(x = 0; x < n; x++){

        printf("%d  ", arr[x]);

    }

    return 0;

}
The output:

Enter the Number of Elements you want in the array: 5
 Enter the Value of Elements : 
45
12
21
65
1
Array after implementing bubble sort: 1  12  21  45  65 

In  C++:

We ask the user to input the elements in an array and check if the first element is larger than the second element then swap them using the temp variable.

#include<iostream>
using namespace std;
int main()
{
    int n, i, arr[50], j, t;
    cout<<"Enter the Size : ";
    cin>>n;
    cout<<"Enter "<<n<<" elements: ";
    for(i=0; i<n; i++)
        cin>>arr[i];
    for(i=0; i<(n-1); i++)
    {
        for(j=0; j<(n-i-1); j++)
        {
            if(arr[j]>arr[j+1])
            {
                t = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = t;
            }
        }
    }
    cout<<"\nThe New Array is: \n";
    for(i=0; i<n; i++)
        cout<<arr[i]<<" ";
    cout<<endl;
    return 0;
}
The output:

Enter the Size : 4
Enter 4 elements: 1
2
9
0
The New Array is: 
0 1 2 9

In python:

We ask the user to input the elements in an array and check if the first element is larger than the second element then swap them using the temp variable.

def bubble_sort(list1):  
    for i in range(0,len(list1)-1):  
        for j in range(len(list1)-1):  
            if(list1[j]>list1[j+1]):  
                list1[j],list1[j+1]=list1[j+1],list1[j]
    return list1  
  
a=eval(input("enter a list"))
print("The unsorted list is: ", a)  
print("The sorted list is: ", bubble_sort(a))  
The output:

enter a list[2,7,8,1,0]
The unsorted list is:  [2, 7, 8, 1, 0]
The sorted list is:  [0, 1, 2, 7, 8]

In java:

We ask the user to input the elements in an array and check if the first element is larger than the second element then swap them using the temp variable.

class Bubble_Sort {
	void bubble_Sort(int arr[])
	{
		int n = arr.length;
		for (int i = 0; i < n - 1; i++)
			for (int j = 0; j < n - i - 1; j++)
				if (arr[j] > arr[j + 1]) {
					int t = arr[j];
					arr[j] = arr[j + 1];
					arr[j + 1] = t;
				}
	}

	
	void printArray(int arr[])
	{
		int n = arr.length;
		for (int i = 0; i < n; ++i)
			System.out.print(arr[i] + " ");
		System.out.println();
	}

		public static void main(String args[])
	{
		Bubble_Sort ob = new Bubble_Sort();
		int arr[] = { 646, 34, 2, 12, 22, 1, 89 };
		ob.bubble_Sort(arr);
		System.out.println("Sorted array");
		ob.printArray(arr);
	}
}
The output:

Sorted Array: 1 2 12 22 34 89 646

Time and Space Complexity

Time complexity- O(N) When the array is in descending order or not arranged in a proper manner then it takes n2 times.

Space Complexity -O(1) It is because we use extra variables to swap the numbers.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *