- Bubble Sort in Data Structure in Hindi
- Algorithm for Bubble Sort in Hindi
- C program for Bubble Sort in Hindi
- C++ program for Bubble Sort in Hindi
Contents
show
Bubble Sort in Data Structure in Hindi
Bubble sort में, array के प्रत्येक तत्व की तुलना उसके adjacent तत्व से की जाती है। algorithm passes में सूची को process करता है। n तत्वों के साथ एक सूची को sorting के लिए n-1 passes की आवश्यकता होती है। array A के n तत्वों पर विचार करें जिनके तत्वों को bubble sort का उपयोग करके sort किया जाना है। एल्गोरिथ्म निम्नलिखित की तरह process करता है।
- Pass 1 में, A [0] की तुलना A [1] से की जाती है, A [1] की तुलना A [2] के साथ की जाती है, A [2] की तुलना A [3] और इसी तरह की जाती है। pass 1 के अंत में, सूची का सबसे बड़ा तत्व सूची के highest index में रखा जाता है।
- Pass 2 में, A [0] की तुलना A [1] से की जाती है, A [1] की तुलना A [2] और इसी तरह की जाती है। pass 2 के अंत में सूची का दूसरा सबसे बड़ा तत्व सूची के दूसरे highest index में रखा जाता है।
- Pass n-1 में, A [0] की तुलना A [1] के साथ की जाती है, A [1] की तुलना A [2] और इसी तरह की जाती है। इस pass के अंत में। सूची का सबसे छोटा तत्व सूची के पहले index में रखा जाता है।
Algorithm :
- Step 1: Repeat Step 2 For i = 0 to N-1
- Step 2: Repeat For J = i + 1 to N – I
- Step 3: IF A[J] > A[i]
SWAP A[J] and A[i]
[END OF INNER LOOP]
[END OF OUTER LOOP - Step 4: EXIT
Complexity
Scenario | Complexity |
---|---|
Space | O(1) |
Worst case running time | O(n2) |
Average case running time | O(n) |
Best case running time | O(n2) |
C Program
- #include<stdio.h>
- void main ()
- {
- int i, j,temp;
- int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
- for(i = 0; i<10; i++)
- {
- for(j = i+1; j<10; j++)
- {
- if(a[j] > a[i])
- {
- temp = a[i];
- a[i] = a[j];
- a[j] = temp;
- }
- }
- }
- printf(“Printing Sorted Element List …\n”);
- for(i = 0; i<10; i++)
- {
- printf(“%d\n”,a[i]);
- }
- }
Output:
Printing Sorted Element List . . . 7 9 10 12 23 34 34 44 78 101
C++ Program
- #include<iostream>
- using namespace std;
- int main ()
- {
- int i, j,temp;
- int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
- for(i = 0; i<10; i++)
- {
- for(j = i+1; j<10; j++)
- {
- if(a[j] < a[i])
- {
- temp = a[i];
- a[i] = a[j];
- a[j] = temp;
- }
- }
- }
- cout <<“Printing Sorted Element List …\n”;
- for(i = 0; i<10; i++)
- {
- cout <<a[i]<<“\n”;
- }
- return 0;
- }
Output:
Printing Sorted Element List ... 7 9 10 12 23 23 34 44 78 101