2

I would really appreciate it if someone can help me on this. I want to input numbers on this array x[0,100] Then, print the whole array but with the numbers inside sorted from the smallest to biggest WITHOUT using sort array.

I have this code so far, but I get an error where I do the comparison. Can someone help me on this please?

x = [0,100]
print x 

puts "\nInput any number from 0~100"
num = gets.to_i
x.push(num)

for i in 0..x.length-1
    for j in 0..x.length-1
        if x[j] < x[j+1] 
            swap = x[j]
            x[j] = x[j+1]
            x[j+1] = swap
        end
    end
end
p x 
3
  • Is there any good reason you don't want to use standard API ? WITHOUT using sort array Commented Oct 19, 2015 at 9:14
  • My professor doesn't want us to use sort. I think it is because he want us to think our way around Commented Oct 19, 2015 at 9:16
  • 2
    You can swap in one line like this:` x[j], x[j+1] = x[j+1], x[j]` Commented Oct 19, 2015 at 15:29

2 Answers 2

5

Regardless of what the best solution would be, you get an error because your second loop is going out of bound: you're calling x[j+1] up to x.length-1, which means that you end up calling x[x.length] when the last element is x[x.length - 1].

Just replace "for j in 0..x.length-1" with "for j in 0..x.length-2"

x = [0,100]
print x

puts "\nInput any number from 0~100"
num = gets.to_i
x.push(num)

for i in 0..x.length-1
    for j in 0..x.length-2
        if x[j] < x[j+1]
            swap = x[j]
            x[j] = x[j+1]
            x[j+1] = swap
        end
    end
end
p x
Sign up to request clarification or add additional context in comments.

4 Comments

Why are you doing x = [0,100] and x = [1,49,20,30,58,10,48,9] at the beginning of this script? Also, it's generally more Ruby-esque to use each instead of for loops :)
@DaniG2k Because that's what the OP wrote ;) I just copy/pasted the code of the OP with the correction, you should ask him/her
Oops that comment was meant for @basukesu not for you :P
oops! I am sorry. I was trying different things out before I posted my code, and I forgot to delete that useless array! Thank you for noticing @DaniG2k I just deleted it but it will take some time for my edit to be processed :/
0

Ruby ranges behave pretty much like arrays, at least when looping. They are already in order.

(1..100).each { |num| puts num }

Your code looks like it is looking if the next element is higher, then swipe. But what about more elements in the array? This is recursive way

def my_sort(list, new_array = nil)

  return new_array if list.size <= 0
  if new_array == nil
    new_array = []
  end
  min = list.min
  new_array << min
  list.delete(min)

  my_sort(list, new_array)

end

puts my_sort([3, 1, 2, 20, 11, 14, 3, 6, 8, 5])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.