# Beginner question: Bubble sort using array.each\_index

**URL:** <https://rubytalk.org/t/beginner-question-bubble-sort-using-array-each-index/65638>\
**Category:** ruby-talk\
**Created:** [10 June 2012 21:46 UTC](https://rubytalk.org/t/beginner-question-bubble-sort-using-array-each-index/65638 "2012-06-10T21:46:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [10 June 2012 21:46 UTC](https://rubytalk.org/t/beginner-question-bubble-sort-using-array-each-index/65638/1 "2012-06-10T21:46:44Z")

</div>

Hi,

I'm a beginner going through Chris Pine's online book & exercises.

One of them required me to alphabetize a list of words WITHOUT using the  
.sort method.

I discovered the following bubble sort algorithm online and understand  
it except for one thing.

How is it able to determine that array|first| points to position 0 on  
the array, and array[second] points to position 1, in order to make the  
comparison?

To me, it looks like both array[first] and array[second] would equal one  
another.

array.each\_index do |first|  
&nbsp;&nbsp;array.each\_index do |second|  
if array[first] \< array[second]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[first], array[second] = array[second], array[first]  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

puts array

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [10 June 2012 22:25 UTC](https://rubytalk.org/t/beginner-question-bubble-sort-using-array-each-index/65638/2 "2012-06-10T22:25:54Z")

</div>

Hi,

You misunderstand the code. "first" and "second" aren't fixed values.  
They are the block parameters belonging to the two iterators, so their  
value will change on each iteration.

Have a look at the outer iterator first:

array.each\_index do |first|

end

This iterator loops over the indices of "array". On each iteration, the  
current index is assigned to the "first" parameter. So if array is ['x',  
'z', 'y'] for example, the parameter will be assigned the values 0, 1  
and 2 subsequently.

You can test this:

array.each\_index do |first|  
&nbsp;&nbsp;puts first  
end

Then there is also an inner iterator, which also loops over the indices  
of array. This means: While the outer iterator is going through the  
indices 0, 1, 2, 3, ..., the inner iterator will do the same on every  
step.

See for yourself:

array.each\_index do |first|  
&nbsp;&nbsp;array.each\_index do |second|  
&nbsp;&nbsp;&nbsp;&nbsp;puts "first (outer): #{first}, second (inner): #{second}"  
&nbsp;&nbsp;end  
end

By the way, the code you posted is \*not\* bubble sort. I do not even know  
it, but it is definitely a bad algorithm, since it involves a lot of  
unnecessary element swapping.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [10 June 2012 22:40 UTC](https://rubytalk.org/t/beginner-question-bubble-sort-using-array-each-index/65638/3 "2012-06-10T22:40:12Z")

</div>

Thank you for clearing that up.

The reason I google'd and used that algorithm is because Chris Pine  
states that we should try to alphabetize an array of user-input words,  
without using the sort method: [http://pine.fm/LearnToProgram/?Chapter=07](http://pine.fm/LearnToProgram/?Chapter=07)

Thus, I wrote:

word = 'word'  
listOfWords = []

while word != ''  
&nbsp;&nbsp;puts 'Enter a word and press enter: '  
&nbsp;&nbsp;word = gets.chomp.downcase  
&nbsp;&nbsp;listOfWords.push word  
end

listOfWords.pop

But became stuck at how I could alphabetize the array, using only the  
things we have learned so far.

If you have a better way I could accomplish this without using the  
previously described algorithm, I would very much appreciate it. It's  
something I've been struggling with all night.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [10 June 2012 22:52 UTC](https://rubytalk.org/t/beginner-question-bubble-sort-using-array-each-index/65638/4 "2012-06-10T22:52:10Z")

</div>

Michael Sung wrote in post #1063963:

> If you have a better way I could accomplish this without using the  
> previously described algorithm, I would very much appreciate it. It's  
> something I've been struggling with all night.

Well, you can use \*any\* sorting algorithm. You may also stick to the one  
above. I didn't say it's wrong, I just said it's inefficient (and not  
bubble sort).

Typical and simple sorting algorithms are insertion sort, selection sort  
and bubble sort (the actual one). You'll find good explanations at  
wikipedia:

> **[Category:Sorting algorithms](https://en.wikipedia.org/wiki/Category:Sorting_algorithms)**
>
> This category has the following 6 subcategories, out of 6 total.
> The following 47 pages are in this category, out of 47 total. This list may not reflect recent changes.

And if you're looking for implementations, this is a good source:

[http://en.wikibooks.org/wiki/Algorithm\_Implementation/Sorting](http://en.wikibooks.org/wiki/Algorithm_Implementation/Sorting)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
