Sorting an Array in Ruby Without Using Sort Method

Sorting an Array in Ruby without using Sort method

You're missing an end after swapped = true. It would be best to indent your code thoroughly and accurately to avoid this kind of problem:

def my_sort(list)
return list if list.size <= 1

swapped = false
while !swapped
swapped = false
0.upto(list.size-2) do |i|
if list[i] > list[i+1]
list[i], list[i+1] = list[i+1], list[i]
swapped = true
end
end
end

list
end

Sorting inputed numbers on array - Ruby - without using 'sort'

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

Array Alphabetical Sorting Without .sort

Sorry if i confused you even more, I'm just trying to help another newb.lol

Here was my solution with lots of notes to explain:

puts
puts "Enter some words and I will returned them to you in alphabetical order!"
puts
input = gets.chomp
input = input.split

def swap(array)
i =0
while i < array.length #here is one iteration over the array

i2 = i + 1
while i2 < array.length #here is a second. this allows me to compare the second index (and the rest as i2 increments) to the current one
if array[i] < array[i2] #if the second is larger than the first
array[i] , array[i2] = array[i2], array[i] #switch them. this is called parallel assignment
end # if
i2 += 1 #increment the second loop before the first. it will keep incrementing until the end before the first one does. then the whole thing starts again until the first loop has reached the full length of the array
end # inner loop
i += 1
end # outer loop
puts
puts '...And just like magic ...here is your string in alphabetical order!! :'
puts
return array.reverse.join(' ')
end # def

puts swap(input)

another nifty solution i found on the web (which i was mad i didn't think of) is :

def shuffle_sort(array)
sorted = []
while !array.empty? #do this until the array is empty
sorted << array.delete(array.min) #push the word with the min value into sorted then delete it. min is a enum method
end
return sorted.join(' ')
end

puts shuffle_sort(alpha)

now how beautiful is that??! the #min method returns the object (a word in this case) with the minimum value!! :)

Sorting strings in array without sorting function - Ruby

I believe I have solved the issue. Thank you for all your help.

I reordered my algorithm: First checking if converted_words[i][x] < converted_words[i-1][x], and then checking if converted_words[i][x] == converted_words[i-1][x].

I also need to check whether if converted_words[i][x] != nil && converted_words[i-1][x] != nil, in order to avoid a NoMethodError (thanks Cary Swoveland).
Finally I combined the two algorithms.

I also realized that I did not need to convert the letters to numbers, as ruby knows which letters are larger. So instead, I left the characters as letters.

I'm aware that the code is not very efficent. If you have any suggestions on how to improve or simplify the algorithm, I would be happy to hear them.

Here's the code:

words = ["my","favorite","animals", "are", "the", "elephant", "and", "the", "antelope", "and", "the", "favela"]
puts words.to_s
#Convert all letters into numbers. Output individual words as arrays.
ordered_words = words.map(&:chars).map { |letters| letters.map { |letter| letter } }

i = 1
x = 0
while i < ordered_words.length
if ordered_words[i][x] != nil && ordered_words[i-1][x] != nil
if ordered_words[i][x] < ordered_words[i-1][x]
ordered_words[i], ordered_words[i-1] = ordered_words[i-1], ordered_words[i]
i = 1
x = 0
else
if ordered_words[i][x] == ordered_words[i-1][x]
x = x + 1
else
i = i + 1
x = 0
end
end
else
if ordered_words[i][x] == nil && ordered_words[i-1][x] == nil
i = i + 1
x = 0
else
if ordered_words[i][x] == nil
ordered_words[i], ordered_words[i-1] = ordered_words[i-1], ordered_words[i]
i = 1
x = 0
else
i = i + 1
x = 0
end
end
end
end

joined_words = []
ordered_words.each do |word|
joined_words.push(word.join)
end
puts joined_words.to_s

Creating a sort method using recursive method in Ruby (without using .sort)

The problem with what you have is that you have a couple of unassigned variables result and new_array.

Here's an example of a custom sort method that uses partition with a recursive method. It takes the first element of your array and splits your array into two parts using partition into a group containing elements lesser and greater than this first element using recursion.

def sort(arr)
return [] if arr.length == 0

f_element = arr.shift
less, greater = arr.partition {|x| x < f_element }
sort(less) + [f_element] + sort(greater)
end

arr = "happiness".chars
print sort(arr)
#=> ["a", "e", "h", "i", "n", "p", "p", "s", "s"]

Sorting an array without arr.sort() method, what's going on here?

The first function does not work because the for loop condition is for (let j = 0; j < arr.length - 1; j++) here arr.length - 1 is equal to 5, so the for loop would go from 0 until it is less than (6 - 1) so it never actually reaches j=5.

The second function works because it goes from 0 until it is less than 6, so it does reach j=5



Related Topics



Leave a reply



Submit