బబుల్ సార్ట్ అల్గోరిథం పరిచయం

బబుల్ సార్ట్ అల్గోరిథం పరిచయం

మీరు డేటాకు వర్తించే అత్యంత ప్రాథమిక కార్యకలాపాలలో సార్టింగ్ ఒకటి. మీరు క్విక్ సార్ట్, బబుల్ సార్ట్, మెర్జ్ సార్ట్, ఇన్సర్షన్ సార్ట్ వంటి వివిధ సార్టింగ్ అల్గారిథమ్‌లను ఉపయోగించి ఎలిమెంట్‌లను వివిధ ప్రోగ్రామింగ్ లాంగ్వేజ్‌లలో క్రమబద్ధీకరించవచ్చు.





ఈ వ్యాసంలో, బబుల్ సార్ట్ అల్గోరిథం, బబుల్ సార్ట్ అల్గోరిథం యొక్క సూడోకోడ్, దాని సమయం మరియు స్థల సంక్లిష్టత మరియు 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). శ్రేణి ఇప్పటికే క్రమబద్ధీకరించబడినప్పుడు ఇది సంభవిస్తుంది.

ఫోటోషాప్‌లో వెక్టర్ లోగోను ఎలా తయారు చేయాలి

సంబంధిత: బిగ్- O సంజ్ఞామానం అంటే ఏమిటి?

బబుల్ సార్ట్ అల్గోరిథం యొక్క సగటు-కేస్ సమయ సంక్లిష్టత 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 కథనాలు ప్రచురించబడ్డాయి)

యువరాజ్ భారతదేశంలోని ఢిల్లీ విశ్వవిద్యాలయంలో కంప్యూటర్ సైన్స్ అండర్ గ్రాడ్యుయేట్ విద్యార్థి. అతను పూర్తి స్టాక్ వెబ్ డెవలప్‌మెంట్ పట్ల మక్కువ కలిగి ఉన్నాడు. అతను వ్రాయనప్పుడు, అతను వివిధ సాంకేతికతల లోతును అన్వేషిస్తున్నాడు.

విండోస్ 10 లో బ్లూ స్క్రీన్‌ను ఎలా పరిష్కరించాలి
యువరాజ్ చంద్ర నుండి మరిన్ని

మా వార్తాలేఖకు సభ్యత్వాన్ని పొందండి

టెక్ చిట్కాలు, సమీక్షలు, ఉచిత ఈబుక్‌లు మరియు ప్రత్యేకమైన డీల్స్ కోసం మా వార్తాలేఖలో చేరండి!

సభ్యత్వం పొందడానికి ఇక్కడ క్లిక్ చేయండి