Bubble Sort in PHP With Example | How Bubble Sort Works in PHP

Blogs ❯❯ PHP

Image could not load

PHP Bubble Sort

PHP Sorting Algorithms

किसी भी programming में data को filter या sort करने के लिए use की गयी techniques / algorithms बहुत important होती है , इससे हमारे application की performance , accuracy और user experience depend करता है।

data को sort करने के लिए use की गयी algorithms जितनी अच्छी , सटीक
और कम time में result देने वाली होगी उतना user experience अच्छा होगा । बैसे तो data को sort करने के लिए कई तरह की techniques / algorithms जैसे Bubble Sort , Selection Sort , Insertion Sort available हैं।

इस blog में हम Bubble Sort के बारे में कुछ easy examples के साथ समझेंगे कि PHP में इसे किस तरह से use कर सकते हैं।

PHP Bubble Sort In Hindi

Bubble Sort एक simple sorting algorithm है जो List / Array के elements को repeatedly sort करता है। इसमें adjacent(नज़दीक) elements को compare किया जाता है , और अगर वो गलत order में हैं तो उनके swap किया जाता है। यह process हर pair के adjacent elements के लिए दोहराई जाती है जब तक complete list sort नहीं हो जाती।

sorting की इस technique को Sinking Sort भी कहते हैं।

How does Bubble Sort in PHP Work ?

PHP में Bubble Sort techniques को कई तरह से आप implement कर सकते हैं। पहला तरीका है for Loop का use करके और दूसरा तरीका है while Loop / do while Loop का use करके।

हालाँकि दोनों तरह के implementation में logic तो same रहेगा लेकिन syntax change हो जायगा।

For example

10

45

4

5

76

ऊपर दिए गए एक set में , सबसे पहला element 10 को इसके adjacent(नज़दीक) element 45 से compare किया जायगा , जो कि सही position पर है तो फिर next adjacent element 4 से compare किया जायगा अब 4 छोटा है 10 से , ये आपसे में swap हो जायेंगे। मतलन 10 की जगह 4 और 4 की जगह 10 .

इस तरह से ये process तब तक repeat होगी जब तक complete set sort नहीं हो जाता है।

PHP Bubble Sort Example In for Loop

// define functions. function bubble_sort($array) { $count = count($array); for($i=0; $i<$count; $i++) { // start iterating from next element as first element is already picked. for($j=$i+1; $j<$count; $j++) { // now compare them and swap. if($array[$i] > $array[$j]) { $temp = $array[$i]; $array[$i] = $array[$j]; $array[$j] = $temp; } } } return $array; } $array = [30, 20,1,2,0]; echo 'Before Soeting : '; print_r($array); echo 'After Soeting : '; print_r(bubble_sort($array));

Output

Before Soeting : Array
(
    [0] => 30
    [1] => 20
    [2] => 1
    [3] => 2
    [4] => 0
)
After Soeting : Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 20
    [4] => 30
)

PHP Bubble Sort Example In while Loop

अब एक example और देख लेते हैं जिसमे while Loop की help से bubble sorting करेंगे।

function bubble_sort($arr) { $n = count($arr); $swapped = true; $i = 0; while ($swapped) { $swapped = false; for ($j = 0; $j < $n - $i - 1; $j++) { if ($arr[$j] > $arr[$j + 1]) { // Swap the elements. $temp = $arr[$j]; $arr[$j] = $arr[$j + 1]; $arr[$j + 1] = $temp; $swapped = true; } } $i++; } return $arr; } $unsortedArray = [64, 34, 25, 12, 22, 11, 90]; $sortedArray = bubble_sort($unsortedArray); echo "Unsorted Array: " . implode(", ", $unsortedArray) . "<br/>"; echo "Sorted Array: " . implode(", ", $sortedArray);

Output

Unsorted Array: 64, 34, 25, 12, 22, 11, 90 
Sorted Array: 11, 12, 22, 25, 34, 64, 90 

I hope , ये blog आपके लिए helpful रहा होगा और आपको Bubble Sort के बारे में अच्छे से समझ आया होगा ।

Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook