بلبل ترتیب الگورتھم کا تعارف۔

بلبل ترتیب الگورتھم کا تعارف۔

ترتیب دینا سب سے بنیادی کاموں میں سے ایک ہے جسے آپ ڈیٹا پر لاگو کرسکتے ہیں۔ آپ مختلف ترتیب دینے والے الگورتھم جیسے کوئیک چھانٹ ، بلبلا چھانٹ ، ضم کرنے کی ترتیب ، اندراج کی ترتیب وغیرہ کا استعمال کرتے ہوئے مختلف پروگرامنگ زبانوں میں عناصر کو ترتیب دے سکتے ہیں۔





اس آرٹیکل میں ، آپ بلبل ترتیب الگورتھم کے کام کے بارے میں سیکھیں گے ، بلبل ترتیب الگورتھم کا سیڈوکوڈ ، اس کا وقت اور جگہ کی پیچیدگی ، اور مختلف پروگرامنگ زبانوں جیسے C ++ ، ازگر ، C ، اور جاوا اسکرپٹ میں اس کا نفاذ۔





بلبلہ ترتیب الگورتھم کیسے کام کرتا ہے؟

بلبلا چھانٹنا آسان ترین الگورتھم ہے جو بار بار فہرست میں قدم رکھتا ہے ، ملحقہ عناصر کا موازنہ کرتا ہے ، اور اگر وہ غلط ترتیب میں ہوں تو ان کا تبادلہ کرتا ہے۔ اس تصور کو ایک مثال کی مدد سے زیادہ موثر انداز میں سمجھایا جا سکتا ہے۔ مندرجہ ذیل عناصر کے ساتھ غیر ترتیب شدہ صف پر غور کریں: {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

وقت کی پیچیدگی اور بلبلہ ترتیب الگورتھم کی معاون جگہ۔

بلبل ترتیب الگورتھم کی بدترین وقت کی پیچیدگی O (n^2) ہے۔ یہ اس وقت ہوتا ہے جب صف نزولی ترتیب میں ہو اور آپ اسے بڑھتے ہوئے ترتیب میں ترتیب دینا چاہتے ہیں یا اس کے برعکس۔





ایڈوب میڈیا انکوڈر کا استعمال کیسے کریں

بلبل ترتیب الگورتھم کی بہترین کیس ٹائم پیچیدگی O (n) ہے۔ یہ اس وقت ہوتا ہے جب صف پہلے سے ترتیب دی گئی ہو۔

ونڈوز 10 کو فیکٹری ری سیٹ پر مجبور کرنے کا طریقہ

متعلقہ: بگ-او نوٹیشن کیا ہے؟

بلبل ترتیب الگورتھم کی اوسط کیس ٹائم پیچیدگی 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 عمل درآمد ہے:

// 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 مضامین شائع ہوئے)

یوراج دہلی یونیورسٹی ، انڈیا میں کمپیوٹر سائنس کے انڈر گریجویٹ طالب علم ہیں۔ وہ فل اسٹیک ویب ڈویلپمنٹ کے بارے میں پرجوش ہے۔ جب وہ نہیں لکھ رہا ہے ، وہ مختلف ٹیکنالوجیز کی گہرائی کو تلاش کر رہا ہے۔

کروم سے پاس ورڈ درآمد کرنے کا طریقہ
یوراج چندر سے مزید

ہماری نیوز لیٹر کو سبسکرائب کریں

ٹیک ٹپس ، جائزے ، مفت ای بکس ، اور خصوصی سودوں کے لیے ہمارے نیوز لیٹر میں شامل ہوں!

سبسکرائب کرنے کے لیے یہاں کلک کریں۔