From: Hugh Sasse Date: 2008-11-26T07:38:41+09:00 Subject: Re: Chris Pine tutorial assistance chapter 7 sort data without use of .sort method ---476953799-1737282135-1227653000=:29481 Content-Type: MULTIPART/MIXED; BOUNDARY="-476953799-1737282135-1227653000=:29481" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. ---476953799-1737282135-1227653000=:29481 Content-Type: TEXT/PLAIN; charset=ISO-8859-1 Content-Transfer-Encoding: 8BIT On Wed, 26 Nov 2008, jgheal@googlemail.com wrote: > On Nov 25, 2:37�pm, Hugh Sasse wrote: > > [lots of stuff worth trimming] > > # basic mental model - store words in 1st array (words_array). Find > the 'smallest' word alphabetically by selecting the last word (call it > wordCheck) from words_array and compare wordCheck with the first word, > second word and so on..... and exchange the value of wordCheck with > the other word (1st/2nd... word) if the other word value happens to be > less. Start the checking again with the first word, second word.... > and so on until a counter has been reached (counter equal to 'current' > length of words_array). Once the counter has been reached, put the > value of wordCheck into a second array (sortedWords_array) and delete > wordCheck value from first unsorted array (words_array) and repeat > until words_array is empty. I think this should work. You will learn more efficient ways to do this, but this is a good start. Knuth recommends thinking about how to solve sorting problems yourself first, before consulting other algorithms. > > puts 'Type in as many words as you want and once you\'ve had enough, > just \'enter\' a clear line.' > > words_array = [] # create 1st array called words_array to store users > unsorted values > > loop do > input = gets.downcase.chomp # take the user input, remove the > 'enter' off the tail and lower the case and call ... > break if input.empty? # .. it 'input' , break from the loop if > 'input' is equal to empty > words_array << input # push 'input' to the words_array > > end > > wordCheck = words_array.last # wordCheck is equal to the last 'input' > stored in the words_array > rotateCounter = 0 > sortedWords_array = [] # create 2nd array to contain 'sorted' values > > until words_array.length == 0 # exit once no words left in the first > unsorted array > > while rotateCounter <= words_array.length # exit if rotate counter > greater than words contained within first array > if words_array[0] >= wordCheck # continue if first word in 1st array > greater or equal to wordCheck value > > words_array.push words_array.shift # rotate the first unsorted > array > rotateCounter = rotateCounter + 1 # add 1 to counter > > else wordCheck = words_array[0] # replace current value (wordCheck) > with 'smaller' word > rotateCounter = 0 # reset counter to zero > end > > end > > sortedWords_array.push wordCheck # add wordCheck value to 2nd array > > until wordCheck = words_array.last # rotate first array until a > matching .. > words_array.push words_array.shift # .. value found at the back of > the first array > end > words_array.pop # remove the last value contained within the 1st > array > rotateCounter = 0 # reset rotate counter > wordCheck = words_array.last # replace current value of wordCheck > with new last word from 1st array > end > puts sortedWords_array # print values within 2nd array, which should > be in low to high alphabetical order > > # Hope the above is legible > > So, what output do you get that is wrong? This code works for me, for the input "words" a b a c a b Hugh ---476953799-1737282135-1227653000=:29481-- ---476953799-1737282135-1227653000=:29481--