Bubble Sort Command-Line Sorting

LinuxLinuxIntermediate
Practice Now

Introduction

Sorting is an essential operation in computer science that arranges data in a specific order. In this challenge, we will write a script that sorts a given set of numbers in ascending or descending order using the bubble sort algorithm. The script will take input from the user through command-line arguments and provide a menu for the user to choose the sorting order.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicFileOperationsGroup -.-> linux/more("`File Scrolling`") linux/BasicSystemCommandsGroup -.-> linux/declare("`Variable Declaring`") linux/BasicSystemCommandsGroup -.-> linux/exit("`Shell Exiting`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/BasicSystemCommandsGroup -.-> linux/read("`Input Reading`") linux/TextProcessingGroup -.-> linux/sort("`Text Sorting`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/VariableHandlingGroup -.-> shell/str_manipulation("`String Manipulation`") shell/VariableHandlingGroup -.-> shell/arrays("`Arrays`") shell/VariableHandlingGroup -.-> shell/param_expansion("`Parameter Expansion`") shell/ControlFlowGroup -.-> shell/for_loops("`For Loops`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/ControlFlowGroup -.-> shell/exit_status("`Exit and Return Status`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_expansion("`Arithmetic Expansion`") shell/AdvancedScriptingConceptsGroup -.-> shell/read_input("`Reading Input`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/AdvancedScriptingConceptsGroup -.-> shell/adv_redirection("`Advanced Redirection`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills shell/if_else -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} linux/more -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} linux/declare -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} linux/exit -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} linux/echo -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} linux/redirect -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} linux/read -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} linux/sort -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} shell/shebang -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} shell/comments -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} shell/quoting -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} shell/variables_decl -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} shell/variables_usage -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} shell/str_manipulation -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} shell/arrays -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} shell/param_expansion -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} shell/for_loops -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} shell/cond_expr -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} shell/exit_status -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} shell/arith_expansion -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} shell/read_input -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} shell/cmd_substitution -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} shell/subshells -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} shell/adv_redirection -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} shell/globbing_expansion -.-> lab-18285{{"`Bubble Sort Command-Line Sorting`"}} end

Sort

In this challenge, you are tasked with creating a script that sorts a given set of numbers in either ascending or descending order using the bubble sort mechanism. This challenge aims to enhance your understanding of sorting mechanisms and improve your ability to work with arrays.

Tasks

You need to complete the following tasks:

  1. Write a script named sort.sh.
  2. Pass numbers through command-line arguments.
  3. Provide a menu for the user to choose ascending or descending.
  4. Show the sorted array according to the user's choice.

Requirements

You must meet the following requirements:

  • The script should be named sort.sh.
  • Accept numbers as command-line arguments.
  • Prompt the user to choose between ascending or descending order.
  • Sort the array according to the user's choice and display the sorted result.

Example

labex:project/ $ bash sort.sh 1 5 4 3 6
inputcount : 5
content of inputValue 1 5 4 3 6
[Input] Enter your choice [Input]
1.Ascending
2.Descending
1
[OUTPUT] Ascending Order [OUTPUT]
1 3 4 5 6
labex:project/ $ bash sort.sh 1 5 4 3 6
inputcount : 5
content of inputValue 1 5 4 3 6
[Input] Enter your choice [Input]
1.Ascending
2.Descending
2
[OUTPUT] Descending Order [OUTPUT]
6 5 4 3 1

Summary

After completing this challenge, you will have a better understanding of sorting mechanisms and array manipulations. You will learn how to implement the bubble sort algorithm to sort a given set of numbers in ascending or descending order. Additionally, you will learn how to take input from the user through command-line arguments and provide a menu for the user to choose the sorting order. This challenge will help you improve your scripting skills and prepare you for more complex programming tasks.

Other Linux Tutorials you may like