Showing posts with label Bubble Sort Algorithm. Show all posts
Showing posts with label Bubble Sort Algorithm. Show all posts

Wednesday, May 6, 2020

Bubble Sort Algorithm And sorting program in different language

This Chapter will help you to learn about  Bubble Sort Algorithm and bubble sort program examples in different languages. 


Bubble Sort Algorithm And sorting program in different language
Bubble Sort Algorithm And sorting program in different language

"Sorting Algorithms" ऐसी अवधारणाएं हैं जो प्रत्येक  competitive programmer को पता होनी चाहिए। "Sorting Algorithms" का उपयोग collections of numbers, strings, characters, or a structure या इनमें से किसी भी प्रकार की संरचना के संग्रह के लिए किया जा सकता है।

Bubble sort आसन्न तत्वों के जोड़े की बार-बार तुलना करने और फिर गलत क्रम में मौजूद होने पर उनकी स्थिति को स्वैप करने के विचार पर आधारित है।

मान लें कि A[] elements का एक unsorted array है। इस सरणी को sorted in ascending order किया जाना चाहिए।  pseudo code निम्नानुसार है:

Bubble Sort Program In C



void bubble_sort( int A[ ], int n ) {
    int temp;
    for(int k = 0; k< n-1; k++) {
        // (n-k-1) is for ignoring comparisons of elements which have already been compared in earlier iterations

        for(int i = 0; i < n-k-1; i++) {
            if(A[ i ] > A[ i+1] ) {
                // here swapping of positions is being done.
                temp = A[ i ];
                A[ i ] = A[ i+1 ];
                A[ i + 1] = temp;
            }
        }
    }
}

Now Understand  the pseudo code With example For "Bubble Sort in C" 


"Bubble Sort in C"
"Bubble Sort in C"
Step 1 में, 7 के साथ 4 तुलना की जाती है। चूंकि7>4, 7, 4 से  आगे बढ़ गया है। चूंकि अन्य सभी तत्व उससे कम मूल्य के हैं, इसलिए इसे सरणी के अंत में ले जाया जाता है।

अब सरणी है।

Step 2 में, 4 के साथ 5 तुलना की जाती है। चूंकि 5>4 और दोनों आरोही क्रम में हैं, इसलिए इन तत्वों की अदला-बदली नहीं की जाती है। हालांकि, जब 5 तुलना 2 की जाती है, 5>2और ये तत्व अवरोही क्रम में होते हैं। इसलिए,5 और2 बदली हैं।

Array According To Bubble Sort : A[] = {2,4,5,7} 

Step 3 में,4 की तुलना 2 के साथ की जाती है। चूंकि4>2, तत्व descending order में हैं, 4 और 2 स्वैप किए जाते हैं।

Sorted Array in "Bubble Sort" : A[] = {2,4,5,7}

Complexity In "Bubble Sort" In C programming

O(n^2) is bubble sort complexity in both worst and average case.

Bubble Sort Program In Python 



# Python program of Bubble Sort algorithm in hindi
  
def bubbleSort(arr): 
    n = len(arr) 
  
    # Traverse through all array components 
    for i in range(n-1): 
    # range(n) will work however outer loop can repeat just once quite required. 
  
        # Last i components are already at its place
        for j in range(0, n-i-1): 
  
            # traverse the array from zero to n-i-1 
            # Swap if the components found is greater 
            # than the following components 
            if arr[j] > arr[j+1] : 
                arr[j], arr[j+1] = arr[j+1], arr[j] 
  
# Driver code to check above 
arr = [64, 34, 25, 12, 22, 11, 90] 
  
bubbleSort(arr) 
  
print ("Sorted array:") 
for i in range(len(arr)): 
    print ("%d" %arr[i]), 

Bubble Sort Program In java

All the steps are same.

class BubbleSort
{
    void bubbleSort(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])
                {
                    // swap the temp and arr[i]
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
    }
 
/* Prints array in java */
    void printArray(int arr[])
    {
        int n = arr.length;
for(int i=0; i<n:++i) 
 System.out.print(arr[i] + " ");
        System.out.println();
    }
 
    // Driver technique to check above program
    public static void main(String args[])
    {
        BubbleSort ob = new BubbleSort();
        int arr[] = {65, 34, 25, 13, 22, 10, 91};
        ob.bubbleSort(arr);
        System.out.println("Sorted array");
        ob.printArray(arr);
    }
}

C++ Program For Bubble Sort

// Optimized implementation of Bubble sort algorithm 
#include
  
void swap(int *xp, int *yp) 
{ 
    int temp = *xp; 
    *xp = *yp; 
    *yp = temp; 
} 
  
// optimized of Bubble Sort algorithm in hindi 
void bubbleSort(int arr[], int n) 
{ 
   int i, j; 
   bool swapped; 
   for (i = 0; i < n-1; i++) 
   { 
     swapped = false; 
     for (j = 0; j < n-i-1; j++) 
     { 
        if (arr[j] > arr[j+1]) 
        { 
           swap(&arr[j], &arr[j+1]); 
           swapped = true; 
        } 
     } 
  
     // IF no 2 components in c++ program were swapped by inner loop, then break 
     if (swapped == false) 
        break; 
   } 
} 
  
/* Function for print the sorted array*/
void printArray(int arr[], int size) 
{ 
    int i; 
    for (i=0; i < size; i++) 
        printf("%d ", arr[i]); 
    printf("n"); 
} 
  
// Driver program to check above functions 
int main() 
{ 
    int arr[] = {64, 34, 25, 12, 22, 11, 90}; 
    int n = sizeof(arr)/sizeof(arr[0]); 
    bubbleSort(arr, n); 
    printf("Sorted array: n"); 
    printArray(arr, n); 
    return 0; 
}  

Sorting Algorithm : - 

Selection sort algorithm And Its Examples

bucket sort Algorithm In Hindi And Its Examples

Counting sort Algorithm in hindi and Its Example 

Heap Sort Algorithm In Hindi and its examples 

Insertion sort Algorithm And Its Examples

Merge Sort Algorithm In HIndi And Its Examples

Quick Sort Algorithm And Its Examples in hindi