{"id":421,"date":"2026-02-17T04:29:51","date_gmt":"2026-02-17T04:29:51","guid":{"rendered":"https:\/\/mitalgoswami.in\/?p=421"},"modified":"2026-02-17T04:36:22","modified_gmt":"2026-02-17T04:36:22","slug":"menu-driven-sorting-program-in-unix-bash","status":"publish","type":"post","link":"https:\/\/mitalgoswami.in\/?p=421","title":{"rendered":"Menu-Driven Sorting Program in UNIX (Bash)"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">!\/bin\/bash<\/h1>\n\n\n\n<p>echo &#8220;Enter numbers separated by space:&#8221;<br>read -a arr<\/p>\n\n\n\n<p>n=${#arr[@]}<\/p>\n\n\n\n<p>while true<br>do<br>echo &#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8220;<br>echo &#8220;1. Sort Ascending&#8221;<br>echo &#8220;2. Sort Descending&#8221;<br>echo &#8220;3. Exit&#8221;<br>echo &#8220;Enter your choice:&#8221;<br>read choice<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>case $choice in\n\n1)\n    # Ascending Bubble Sort\n    for ((i=0; i&lt;n; i++))\n    do\n        for ((j=0; j&lt;n-i-1; j++))\n        do\n            if &#91; ${arr&#91;j]} -gt ${arr&#91;j+1]} ]\n            then\n                temp=${arr&#91;j]}\n                arr&#91;j]=${arr&#91;j+1]}\n                arr&#91;j+1]=$temp\n            fi\n        done\n    done\n    echo \"Sorted Array (Ascending): ${arr&#91;@]}\"\n    ;;\n\n2)\n    # Descending Bubble Sort\n    for ((i=0; i&lt;n; i++))\n    do\n        for ((j=0; j&lt;n-i-1; j++))\n        do\n            if &#91; ${arr&#91;j]} -lt ${arr&#91;j+1]} ]\n            then\n                temp=${arr&#91;j]}\n                arr&#91;j]=${arr&#91;j+1]}\n                arr&#91;j+1]=$temp\n            fi\n        done\n    done\n    echo \"Sorted Array (Descending): ${arr&#91;@]}\"\n    ;;\n\n3)\n    echo \"Exiting program...\"\n    break\n    ;;\n\n*)\n    echo \"Invalid choice! Please try again.\"\n    ;;\n\nesac<\/code><\/pre>\n\n\n\n<p>done<\/p>\n\n\n\n<p><strong><span style=\"text-decoration: underline;\">Sample Output<\/span><\/strong><\/p>\n\n\n\n<p>Enter numbers separated by space:<br>8 3 5 1 9<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Sort Ascending<\/li>\n\n\n\n<li>Sort Descending<\/li>\n\n\n\n<li>Exit<br>Enter your choice:<br>1<\/li>\n<\/ol>\n\n\n\n<p>Sorted Array (Ascending): 1 3 5 8 9<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>!\/bin\/bash echo &#8220;Enter numbers separated by space:&#8221;read -a arr n=${#arr[@]} while truedoecho &#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8220;echo &#8220;1. Sort Ascending&#8221;echo &#8220;2. Sort Descending&#8221;echo &#8220;3. Exit&#8221;echo &#8220;Enter your choice:&#8221;read choice done Sample Output Enter numbers separated by space:8 3 5 1 9 Sorted Array (Ascending): 1 3 5 8 9<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-421","post","type-post","status-publish","format-standard","hentry","category-unix"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=421"}],"version-history":[{"count":1,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/421\/revisions"}],"predecessor-version":[{"id":422,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/421\/revisions\/422"}],"wp:attachment":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}