Selection Sort in PHP With Example | How does selection sort work in PHP ?

Blogs ❯❯ PHP

Image could not load

Selection 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 में हम Selection Sort के बारे में कुछ easy examples के साथ समझेंगे कि PHP में इसे किस तरह से use कर सकते हैं।

Selection Sort In Hindi

Selection Sort एक simple sorting algorithm है जिससे हम एक list या Array को sort करते हैं। ये step-by-step तरीके से काम करता है , जब तक कि Array के elements सही तरीके से arrange नहीं हो जाते हैं।

How does selection sort in PHP work ?

  1. सबसे पहले हम List में से minimum value को find करते हैं।

  2. जब हम minimum value को find कर लेते हैं तो उससे Array के पहले element से swap कर देते हैं , इससे minimum value वाले element को Array का पहला element बना देते हैं।

  3. फिर पहले element को छोड़कर 2nd element से Array में फिर से minimum value find करते हैं। Again अगर minimum value मिलती है तो फिर 2nd element से swap कर देते हैं।

  4. यह process हम array के हर एक element के लिए repeat करते हैं , और हर बार remaining unsorted part में से minimum value ढूंढकर उसे sorted part के बाद swap कर देते हैं।

Selection sort example

अब ऊपर दिए गए process को follow करके actually में समझ लेते हैं।

32

65

2

12

ऊपर दिए गए एक set में , सबसे छोटी value find करते है तो 2 मिलेगी , इसे set के पहले element 32 से swap कर देंगे।

2

65

32

12

अब 2nd element से सबसे छोटी value find करने पर 12 मिलेगी , इसे 2nd element 65 से swap कर देंगे।

2

12

32

64

अब 3rd number से सबसे छोटी value ढूंढने पर 32 मिलती है जो कि already सही जगह पर है। और इसी तरह से set का last element भी अपनी जगह पर सही है।

PHP Selection Sort Example with for Loop

अब Selection Sort algorithm से for Loop का use करके एक Array को sort करने का example देखते हैं।

function selection_sort($arr) { $n = count($arr); // we are looping n-1 because last element would be highest one. for ($i = 0; $i < $n - 1; $i++) { $minIndex = $i; // Find the index of the minimum element in the unsorted part of the array. for ($j = $i + 1; $j < $n; $j++) { if ($arr[$j] < $arr[$minIndex]) { $minIndex = $j; } } // Swap the minimum element with the first element of the unsorted part. $temp = $arr[$i]; $arr[$i] = $arr[$minIndex]; $arr[$minIndex] = $temp; } // return sorted array. return $arr; } $arr = [64, 25, 12, 22, 11]; echo "Original array : " . implode(", " , $arr) . "<br>"; $arr = selection_sort($arr); echo "Sorted array : " . implode(", " , $arr);

Output

Original array : 64, 25, 12, 22, 11
Sorted array : 11, 12, 22, 25, 64

अगर आप return statement use नहीं करना चाहते हैं तो , function parameter को as a reference (&$arr) handle करना पड़ेगा। ताकि array element के actual value को modify किया जा सके।

PHP Selection Sort example with while Loop

अब एक example और देख लेते हैं जिसमे while Loop की help से selection sorting करेंगे। हालाँकि while Loop implementation में भी logic तो same रहेगा बस syntax change हो जायगा।

function selection_sort($arr) { $n = count($arr); $i = 0; while ($i < $n - 1) { $minIndex = $i; $j = $i + 1; // Find the index of the minimum element in the unsorted part of the array. while ($j < $n) { if ($arr[$j] < $arr[$minIndex]) { $minIndex = $j; } $j++; } // Swap the minimum element with the first element of the unsorted part. $temp = $arr[$i]; $arr[$i] = $arr[$minIndex]; $arr[$minIndex] = $temp; $i++; } return $arr; } $arr = [231, 1, 2, 22, 5, 3411]; echo "Original array : " . implode(", " , $arr) . "<br>"; $arr = selection_sort($arr); echo "Sorted array : " . implode(", " , $arr);

Output

Original array : 231, 1, 2, 22, 5, 3411
Sorted array : 1, 2, 5, 22, 231, 3411

I hope , ये blog आपके लिए helpful रहा होगा और आपको Selection 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