Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7661745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:38:22+00:00 2026-05-31T13:38:22+00:00

This is my code for a bubble sort on n numbers: #!/bin/bash echo -n

  • 0

This is my code for a bubble sort on n numbers:

#!/bin/bash

echo -n  "Input n, the number of numbers"
read N
declare -a array[N]
echo -e "Input the elements, press enter after each element"
for i in seq 1 $N  
do 
    read array[$i]
done

swap1()
{ # for swapping two numbers, we are employing bubble sort
    local temp = ${array[$1]} 
    array[$1] = ${array[$2]}
    array[$2]=$temp
    return
}

numb_elements=${#array[@]}
let "comparisons = $numb_elements - 1"

count=1

while [ "$comparisons" -gt 0]
do
   index =0

   while[ "$index" -lt "$comparisons" ];do
      if [ ${array[$index]} \> ${array[ 'expr $index + 1']} ]
      then 
         swap1 $index 'expr $index + 1'
      fi
      let "index += 1" # Or, index+=1 on Bash, ver. 2.1 or newer
   done

   let "comparisons -=1"
   echo 
   echo "$count: ${array[@]}
   echo

   let "count +=1"
done
exit 0

I have two problems with this code:

  1. the input array just takes 3 numbers
  2. and then i get an error on line 42 saying syntax error for the command while do

I have tried while [] ; do, but it doesn’t work.

Its just been a day that i have been trying bash syntax.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:38:22+00:00Added an answer on May 31, 2026 at 1:38 pm

    You made a series of errors:

    • correct spaces are fundamental to shell scripting
    • missing “ apices to execute code and get the output
    • logic error (starting inserting from the second array element and using it from the first one)
    • iterating the wrong number of time for the bubblesort alg

    This is your code corrected.

    #!/bin/bash
    
    swap1() { # for swapping two numbers, we are employing bubble sort
            local temp=${array[$1]}
    
            array[$1]=${array[$2]}
            array[$2]=$temp
            return
    }
    
    
    echo -n "Input n, the number of numbers: "
    
    read N
    declare -a array[$N]
    
    echo -e "Input the elements, press enter after each element"
    for i in `seq 1 $N`
    do
            read array[$i]
    done
    
    numb_elements=${#array[@]}
    #let "comparisons = $numb_elements - 1"
    comparisons=$numb_elements
    count=1
    while [ "$comparisons" -gt 0 ]
    do
            index=1
            while [ "$index" -lt "$comparisons" ]
            do
                    tmp=`expr $index + 1`
                    if [ ${array[$index]} -gt ${array[$tmp]} ]
                    then 
                            swap1 $index $tmp
                    fi
                    let "index += 1" # Or, index+=1 on Bash, ver. 2.1 or newer
            done
            let "comparisons -= 1"
            echo
            echo "$count: ${array[@]}"
            echo
            let "count += 1"
    done
    
    exit 0  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement a delegate solution for Bubble sort. I have this code:
My code for bubble sort is in gui is this:- def bubble(self): def bubble_sort
this is my code about bubble sort. #include stdafx.h #include <iostream> #include <cstdlib> using
I've read this question mentioning Code Bubbles and I've watched their video presentation. The
Hi I have the following bubble sort algorithm (generic code taken from another site)
I am trying to sort an array of integers in MIPS using bubble sort
I am having issues the last number in this code is not being sorted.
i write a c++ code for Bubble sort algorithm and i dont know how
I wrote a bubble sort in Prolog (code below). It works but it smells.
I have this code that creates a little speech bubble. I am going to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.