बबल सॉर्ट एल्गोरिथम का परिचय

बबल सॉर्ट एल्गोरिथम का परिचय

छँटाई सबसे बुनियादी कार्यों में से एक है जिसे आप डेटा पर लागू कर सकते हैं। आप क्विक सॉर्ट, बबल सॉर्ट, मर्ज सॉर्ट, इंसर्शन सॉर्ट इत्यादि जैसे विभिन्न सॉर्टिंग एल्गोरिदम का उपयोग करके विभिन्न प्रोग्रामिंग भाषाओं में तत्वों को सॉर्ट कर सकते हैं। इन सभी में बबल सॉर्ट सबसे सरल एल्गोरिदम है।





इस लेख में, आप बबल सॉर्ट एल्गोरिथम की कार्यप्रणाली, बबल सॉर्ट एल्गोरिथम के स्यूडोकोड, इसके समय और स्थान की जटिलता, और विभिन्न प्रोग्रामिंग भाषाओं जैसे C++, Python, C, और JavaScript में इसके कार्यान्वयन के बारे में जानेंगे।





बबल सॉर्ट एल्गोरिथम कैसे काम करता है?

बबल सॉर्ट सबसे सरल सॉर्टिंग एल्गोरिदम है जो बार-बार सूची के माध्यम से कदम उठाता है, आसन्न तत्वों की तुलना करता है, और यदि वे गलत क्रम में हैं तो उन्हें स्वैप कर देता है। इस अवधारणा को एक उदाहरण की सहायता से अधिक कुशलता से समझाया जा सकता है। निम्नलिखित तत्वों के साथ एक क्रमबद्ध सरणी पर विचार करें: {16, 12, 15, 13, 19}।





उदाहरण:

यहां आसन्न तत्वों की तुलना की जाती है और यदि वे आरोही क्रम में नहीं हैं, तो उनकी अदला-बदली की जाती है।



बबल सॉर्ट एल्गोरिथम का स्यूडोकोड

स्यूडोकोड में, बबल सॉर्ट एल्गोरिथ्म को इस प्रकार व्यक्त किया जा सकता है:

bubbleSort(Arr[], size)
// loop to access each array element
for i=0 to size-1 do:
// loop to compare array elements
for j=0 to size-i-1 do:
// compare the adjacent elements
if Arr[j] > Arr[j+1] then
// swap them
swap(Arr[j], Arr[j+1])
end if
end for
end for
end

उपरोक्त एल्गोरिथ्म सभी तुलनाओं को संसाधित करता है, भले ही सरणी पहले से ही क्रमबद्ध हो। यदि आंतरिक लूप किसी भी स्वैप का कारण नहीं बनता है तो इसे एल्गोरिदम को रोककर और अनुकूलित किया जा सकता है। यह एल्गोरिथम के निष्पादन समय को कम करेगा।





इस प्रकार, अनुकूलित बबल सॉर्ट एल्गोरिथ्म का छद्म कोड इस प्रकार व्यक्त किया जा सकता है:

bubbleSort(Arr[], size)
// loop to access each array element
for i=0 to size-1 do:
// check if swapping occurs
swapped = false
// loop to compare array elements
for j=0 to size-i-1 do:
// compare the adjacent elements
if Arr[j] > Arr[j+1] then
// swap them
swap(Arr[j], Arr[j+1])
swapped = true
end if
end for
// if no elements were swapped that means the array is sorted now, then break the loop.
if(not swapped) then
break
end if
end for
end

बबल सॉर्ट एल्गोरिथम का समय जटिलता और सहायक स्थान

बबल सॉर्ट एल्गोरिथम की सबसे खराब स्थिति समय जटिलता ओ (एन ^ 2) है। यह तब होता है जब सरणी अवरोही क्रम में होती है और आप इसे आरोही क्रम में या इसके विपरीत क्रमबद्ध करना चाहते हैं।





अपना ps4 नाम कैसे बदलें

बबल सॉर्ट एल्गोरिथम की सर्वोत्तम-केस समय जटिलता O(n) है। यह तब होता है जब सरणी पहले से ही क्रमबद्ध होती है।

मेरा मैक स्टार्ट नहीं होगा

सम्बंधित: बिग-ओ नोटेशन क्या है?

बबल सॉर्ट एल्गोरिथम की औसत-केस समय जटिलता O(n^2) है। यह तब होता है जब सरणी के तत्व अव्यवस्थित क्रम में होते हैं।

बबल सॉर्ट एल्गोरिथ्म के लिए आवश्यक सहायक स्थान O(1) है।

C++ बबल सॉर्ट एल्गोरिथम का कार्यान्वयन

बबल सॉर्ट एल्गोरिथम का C++ कार्यान्वयन नीचे दिया गया है:

// C++ implementation of the
// optimised Bubble Sort algorithm
#include
using namespace std;
// Function to perform Bubble Sort
void bubbleSort(int arr[], int size) {
// Loop to access each element of the array
for (int i=0; i<(size-1); i++) {
// Variable to check if swapping occurs
bool swapped = false;
// loop to compare two adjacent elements of the array
for (int j = 0; j <(size-i-1); j++) {
// Comparing two adjacent array elements
if (arr[j] > arr[j + 1]) {
// Swap both elements if they're
// not in correct order
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
// If no elements were swapped that means the array is sorted now,
// then break the loop.
if (swapped == false) {
break;
}
}
}
// Prints the elements of the array
void printArray(int arr[], int size) {
for (int i = 0; i cout << arr[i] << ' ';
}
cout << endl;
}
int main() {
int arr[] = {16, 12, 15, 13, 19};
// Finding the length of the array
int size = sizeof(arr) / sizeof(arr[0]);
// Printing the given unsorted array
cout << 'Unsorted Array: ' << endl;
printArray(arr, size);
// Calling bubbleSort() function
bubbleSort(arr, size);
// Printing the sorted array
cout << 'Sorted Array in Ascending Order:' << endl;
printArray(arr, size);
return 0;
}

आउटपुट:

Unsorted Array:
16 12 15 13 19
Sorted Array in Ascending Order:
12 13 15 16 19

बबल सॉर्ट एल्गोरिथम का पायथन कार्यान्वयन

बबल सॉर्ट एल्गोरिथम का पायथन कार्यान्वयन नीचे दिया गया है:

# Python implementation of the
# optimised Bubble Sort algorithm

# Function to perform Bubble Sort
def bubbleSort(arr, size):
# Loop to access each element of the list
for i in range (size-1):
# Variable to check if swapping occurs
swapped = False
# loop to compare two adjacent elements of the list
for j in range(size-i-1):
# Comparing two adjacent list elements
if arr[j] > arr[j+1]:
temp = arr[j]
arr[j] = arr[j+1]
arr[j+1] = temp
swapped = True
# If no elements were swapped that means the list is sorted now,
# then break the loop.
if swapped == False:
break
# Prints the elements of the list
def printArray(arr):
for element in arr:
print(element, end=' ')
print('')

arr = [16, 12, 15, 13, 19]
# Finding the length of the list
size = len(arr)
# Printing the given unsorted list
print('Unsorted List:')
printArray(arr)
# Calling bubbleSort() function
bubbleSort(arr, size)
# Printing the sorted list
print('Sorted List in Ascending Order:')
printArray(arr)

आउटपुट:

Unsorted List:
16 12 15 13 19
Sorted List in Ascending Order:
12 13 15 16 19

सम्बंधित: पायथन में फॉर लूप्स का उपयोग कैसे करें

सी बबल सॉर्ट एल्गोरिदम का कार्यान्वयन

बबल सॉर्ट एल्गोरिथम का C कार्यान्वयन नीचे दिया गया है:

// C implementation of the
// optimised Bubble Sort algorithm
#include
#include
// Function to perform Bubble Sort
void bubbleSort(int arr[], int size) {
// Loop to access each element of the array
for (int i=0; i<(size-1); i++) {
// Variable to check if swapping occurs
bool swapped = false;
// loop to compare two adjacent elements of the array
for (int j = 0; j <(size-i-1); j++) {
// Comparing two adjacent array elements
if (arr[j] > arr[j + 1]) {
// Swap both elements if they're
// not in correct order
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
// If no elements were swapped that means the array is sorted now,
// then break the loop.
if (swapped == false) {
break;
}
}
}
// Prints the elements of the array
void printArray(int arr[], int size) {
for (int i = 0; i printf('%d ', arr[i]);
}
printf(' ⁠n ');
}
int main() {
int arr[] = {16, 12, 15, 13, 19};
// Finding the length of the array
int size = sizeof(arr) / sizeof(arr[0]);
// Printing the given unsorted array
printf('Unsorted Array: ⁠n');
printArray(arr, size);
// Calling bubbleSort() function
bubbleSort(arr, size);
// Printing the sorted array
printf('Sorted Array in Ascending Order: ⁠n');
printArray(arr, size);
return 0;
}

आउटपुट:

Unsorted Array:
16 12 15 13 19
Sorted Array in Ascending Order:
12 13 15 16 19

बबल सॉर्ट एल्गोरिथम का जावास्क्रिप्ट कार्यान्वयन

बबल सॉर्ट एल्गोरिथम का जावास्क्रिप्ट कार्यान्वयन नीचे दिया गया है:

// JavaScript implementation of the
// optimised Bubble Sort algorithm
// Function to perform Bubble Sort
function bubbleSort(arr, size) {
// Loop to access each element of the array
for(let i=0; i // Variable to check if swapping occurs
var swapped = false;
// loop to compare two adjacent elements of the array
for(let j=0; j // Comparing two adjacent array elements
if(arr[j] > arr[j+1]) {
// Swap both elements if they're
// not in correct order
let temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
swapped = true;
}
// If no elements were swapped that means the array is sorted now,
// then break the loop.
if (swapped == false) {
break;
}
}
}
}
// Prints the elements of the array
function printArray(arr, size) {
for (let i=0; i document.write(arr[i] + ' ');
}
document.write('
')
}

var arr = [16, 12, 15, 13, 19];
// Finding the length of the array
var size = arr.length;
// Printing the given unsorted array
document.write('Unsorted Array:
');
printArray(arr, size);
// Calling bubbleSort() function
bubbleSort(arr, size);
// Printing the sorted array
document.write('Sorted Array in Ascending Order:
');
printArray(arr, size);

आउटपुट:

Unsorted Array:
16 12 15 13 19
Sorted Array in Ascending Order:
12 15 13 16 19

अब आप बबल सॉर्ट एल्गोरिथम के कार्य को समझते हैं

बबल सॉर्ट सबसे सरल सॉर्टिंग एल्गोरिथम है और इसका उपयोग मुख्य रूप से सॉर्टिंग की नींव को समझने के लिए किया जाता है। बबल सॉर्ट को पुनरावर्ती रूप से भी लागू किया जा सकता है, लेकिन यह ऐसा करने के लिए कोई अतिरिक्त लाभ प्रदान नहीं करता है।

पायथन का उपयोग करके, आप बबल सॉर्ट एल्गोरिथ्म को आसानी से लागू कर सकते हैं। यदि आप पायथन से अपरिचित हैं और अपनी यात्रा की शुरुआत करना चाहते हैं, तो 'हैलो वर्ल्ड' स्क्रिप्ट के साथ शुरुआत करना एक बढ़िया विकल्प है।

साझा करना साझा करना कलरव ईमेल 'हैलो वर्ल्ड' स्क्रिप्ट का उपयोग करके पायथन के साथ शुरुआत कैसे करें

पायथन आज उपयोग की जाने वाली सबसे लोकप्रिय प्रोग्रामिंग भाषाओं में से एक है। अपनी पहली पायथन लिपि के साथ आरंभ करने के लिए इस ट्यूटोरियल का अनुसरण करें।

आगे पढ़िए
संबंधित विषय
  • प्रोग्रामिंग
  • जावा
  • अजगर
  • कोडिंग ट्यूटोरियल
लेखक के बारे में युवराज चंद्र(60 लेख प्रकाशित)

युवराज दिल्ली विश्वविद्यालय, भारत में कंप्यूटर विज्ञान के स्नातक छात्र हैं। उन्हें फुल स्टैक वेब डेवलपमेंट का शौक है। जब वह नहीं लिख रहा होता है, तो वह विभिन्न तकनीकों की गहराई की खोज कर रहा होता है।

ट्रैकिंग (वाणिज्यिक एयरलाइन उड़ान)
युवराज चंद्र की अन्य फ़िल्में-टीवी शो

हमारे न्यूज़लेटर की सदस्यता लें

तकनीकी युक्तियों, समीक्षाओं, निःशुल्क ई-पुस्तकों और अनन्य सौदों के लिए हमारे न्यूज़लेटर से जुड़ें!

सब्सक्राइब करने के लिए यहां क्लिक करें