Introduction To Bubble Sort:
- A bubble sort algorithm is a simple algorithm in which the elements are arranged by repeatedly comparing the adjacent elements, if they are in the wrong order then they are swapped.
- The bubble sort Algorithm is also called a sinking sort algorithm.
- The bubble sort algorithm is one of the simplest sorting algorithms and the length of the bubble sort algorithm is also small.
- It is an Inplace Sorting Algorithm.
- An algorithm that doesn't need an extra space except for a variable space for the completion of the sorting algorithm is called an Inplace sorting algorithm.
- Here, as the bubble sort algorithm works on the basis of comparison and swapping if they are in the wrong order, we didn't need any extra space more than a variable space. So, the bubble sort algorithm is an in-place sorting algorithm.
- Bubble sort Algorithm is a Stable sort Algorithm.
- In bubble sort, the order of the duplicate elements is not changed even after sorting.
- Time Complexity:
- Best Case: O(n)
- Average Case: O(n^2)
- Worst Case: O(n^2)
- Space Complexity: O(1)
Bubble Sort Implementation And Theory Clear Explanation In A Video:
Implementation Of Bubble Sort Algorithm In C:
// Bubble Sort Implementation In C#include<stdio.h>void bubbleSort(int A[],int n){int i,j,temp;for(i=0;i<n-1;i++){for(j=0;j<n-1-i;j++){if(A[j]>A[j+1]){temp = A[j];A[j] = A[j+1];A[j+1] = temp;}}}}int main() {int n,i;printf("Enter the Size of the Array: ");scanf("%d",&n);int A[n];for(i=0;i<n;i++){scanf("%d",&A[i]);}bubbleSort(A,n);for(i=0;i<n;i++){printf("%d ",A[i]);}return 0;}
Implementaiton Of Bubble Sort In C++ :
// Bubble Sort In C++#include<iostream>using namespace std;void bubbleSort(int A[],int n){int i,j,temp;for(i=1;i<n-1;i++){int flag = 0; // Initially flag = 0 indicates there is no swappingfor(j=0;j<n-1-i;j++){if(A[j]>A[j+1]){temp = A[j];A[j] = A[j+1];A[j+1] = temp;flag=1; // flag changes to 1, when elements are swapped}}if(flag==0){ // If flag=0 that means no swapping has been occurred, therefore they are already sortedbreak; // hence they are already sorted, we will break the process to save the time}}}int main(){int n;cout<<"Enter the size of the Array: ";cin>>n;int A[n];for(int i=0;i<n;i++){cin>>A[i];}bubbleSort(A,n);for(int i=0;i<n;i++){cout<<A[i]<<" ";}}
Implementation Of Bubble Sort In Python:
nums = list(map(int,input().split()))def bubbleSort(A):for i in range(len(A)):flag = 0for j in range(0,len(A)-1-i):if A[j]>A[j+1]:A[j],A[j+1] = A[j+1],A[j]flag=1if flag==0:breakbubbleSort(nums)for i in nums:print(i,end=" ")
Implementation Of Bubble Sort In Java:
class bubbleSort {void bSort(int A[]){int n = A.length;int i,j,flag,temp;for(i=0;i<n-1;i++){flag=0;for(j=0;j<n-1-i;j++){if(A[j]>A[j+1]){temp = A[j];A[j] = A[j+1];A[j+1] = temp;flag=1;}}if(flag==0) break;}}public static void main(String[] args) {int A[] = {3,23,43,22,3,332,5};bubbleSort ob = new bubbleSort();ob.bSort(A);for(int i=0;i<A.length;i++) System.out.print(A[i] + " ");}}
No comments:
Post a Comment