Ruby implements the insertion Sorting Algorithm and the advanced two-way insertion sorting code example, ruby two-way

Source: Internet
Author: User

Ruby implements the insertion Sorting Algorithm and the advanced two-way insertion sorting code example, ruby two-way

Basic
Insert a record to a sorted table to obtain an ordered table with one added record. In addition, the most critical point is that it moves all records larger than the current element to leave the insert position "self. After n-1 troughs are inserted, the record is an ordered sequence.

Def insertSort (tarray) I = 1 while (I <tarray. size) do if tarray [I] <tarray [I-1] j = I-1 x = tarray [I] # puts x. class # puts tarray [I]. class tarray [I] = tarray [I-1] # first swap position greater than yourself with the first one on the left while (x <tarray [j]. to_ I) do # Find a smaller one than yourself and put it in the tarray [j + 1] = tarray [j] # puts tarray [j]. class j = J-1 end tarray [j + 1] = x end I = I + 1 end enda = [5, 2, 6, 4, 7, 9, 8] insertSort (a) print
[2, 4, 5, 6, 7, 8, 9]>Exit code: 0

At the beginning of code writing, the to_ I method was not added to x <tarray [j], and the following error occurs:

final.rb:10:in `<': comparison of Fixnum with nil failed (ArgumentError)

At the beginning, I was confused and output x. class, tarray [j]. class. However, both outputs are Fixnum. Later, we found that Ruby's Array class is not the same as other classes. Ruby allows an Array object to store different types of elements. When an element of a is assigned to x, I cannot determine whether a [I] Compared with x is of the Fixnum type. Therefore, an error is returned, which is just my understanding.

Advanced
2 insert sorting based on semi-insert sorting:

def two_way_sort data first,final = 0,0 temp = [] temp[0] = data[0] result = [] len = data.length for i in 1..(len-1)  if data[i]>=temp[final]   final +=1   temp[final] = data[i]  elsif data[i]<= temp[first]   first = (first -1 + len)%len   temp[first] = data[i]  else   if data[i]<temp[0]    low = first    high = len -1       while low <=high do     m = (low + high)>>1     if data[i]>temp[m]      low = m + 1     else      high = m -1     end    end        j = first - 1    first -=1    while j < high do     temp[j] = temp[j+1]     j +=1    end     temp[high] = data[i]   else    low =0    high = final    while low <=high do     m =(low + high)>>1     if data[i]>=temp[m]      low = m + 1     else      high = m - 1     end    end    j = final + 1    final +=1    while j > low do     temp[j] = temp[j-1]     j -=1    end     temp[low] = data[i]   end  end   p temp  end i = 0 for j in first..(len - 1)  result[i] = temp[j]  i +=1 end for j in 0..final  result[i] = temp[j]  i +=1 end return resultenddata = [4,1,5,6,7,2,9,3,8].shufflep dataresult = two_way_sort datap result

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.