PHP Form validation | Validate User Input Data In PHP In Hindi
PHP null In Hindi | PHP is null Function
PHP com port communication In Hindi | Write on com port in PHP
PHP Null Coalescing Operator In Hindi , Null Coalescing Operator kya hai ?
Variables In Snowflake In Hindi | Snowflake Variables In Hindi
What is Hub? and How Hub works?
If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
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 में हम Insertion Sort
के बारे में कुछ easy examples के साथ समझेंगे कि PHP में इसे किस तरह से use कर सकते हैं।
●●●
Insertion sort एक simple sorting algorithm है जो एक list या Array को sort करने के लिए use की जाती है। इसका basic idea ये कि आप एक element को लीजिये और उस subset में insert insert कीजिये जो already sorted है। यह process ऐसे ही हर element के लिए repeat होती है जब तक पूरी list sorted नहीं हो जाती।
Example के लिए हमारे पास एक Array है जिसमे 5 elements हैं।
4 | 2 | 7 | 1 | 3 |
Step 1 : पहला element (4) already sorted subset में insert किया जाता है। क्योंकि यह पहला element है , तो कोई comparison या shifting नहीं होती। तो अभी elements की position भी same ही होगी।
Step 2 : दूसरा element (2) , 4 से छोटा है तो 4 की जगह पर shift कर देंगे। और 4 को 2 की जगह पर।
2 | 4 | 7 | 1 | 3 |
Step 3 : तीसरा element (7) already 4 बड़ा है तो इस बार दोनों elements की position में कोई change नहीं आएगा।
Step 4 : अब 4th element (1) को insert करेंगे। 1 sorted subset में available पहले तीनों elements से छोटा है तो इसे सबसे पहली position पर insert कर देंगे।
1 | 2 | 4 | 7 | 3 |
Step 5 : अब 5th element (3) को सबसे छोटा मानकर sorted subset के 1st position पर insert करेंगे। और उसे 1 के आगे shift करेंगे फिर उसे 2 के आगे shift करेंगे। इस तरह से 3 अपनी सही जगह पर आ जायजा
1 | 2 | 3 | 4 | 7 |
●●●
अब same example को हम for Loop का use करके Insertion Sort करेंगे।
function insertionSort($arr) {
$n = count($arr);
for ($i = 1; $i < $n; $i++) {
$key = $arr[$i];
/*
* Move elements of arr[0..i-1], that are greater than key,
* to one position ahead of their current position
*/
for ($j = $i - 1; $j >= 0 && $arr[$j] > $key; $j--) {
$arr[$j + 1] = $arr[$j];
}
$arr[$j + 1] = $key;
}
return $arr;
}
$originalArray = [4, 2, 7, 1, 3];
$sortedArray = insertionSort($originalArray);
echo "Original Array : " . implode(", ", $originalArray) . "<br/>";
echo "Sorted Array : " . implode(", ", $sortedArray);
Output
Original Array : 4, 2, 7, 1, 3 Sorted Array : 1, 2, 3, 4, 7
●●●
Again , same example को इस बार while Loop से करेंगे। हालाँकि logic तो same ही रहेगा बस syntax change हो जायगा।
<?php
function insertionSort($arr) {
$n = count($arr);
$i=1;
while($i < $n) {
$key = $arr[$i];
$j = $i - 1;
// Shift elements of arr[0..i-1] that are greater than key.
while ($j >= 0 && $arr[$j] > $key) {
$arr[$j + 1] = $arr[$j];
$j = $j - 1;
}
$arr[$j + 1] = $key;
$i++;
}
return $arr;
}
$originalArray = [4, 2, 7, 1, 3];
$sortedArray = insertionSort($originalArray);
echo "Original Array : " . implode(", ", $originalArray) . "<br/>";
echo "Sorted Array : " . implode(", ", $sortedArray);
?>
Output
Original Array : 4, 2, 7, 1, 3 Sorted Array : 1, 2, 3, 4, 7
●●●
I hope , ये blog आपके लिए helpful रहा होगा और आपको Insertion Sort के बारे में अच्छे से समझ आया होगा ।
Loading ...
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