Ruby implementation Insert sort algorithm and advanced two-way insert sort code example _ruby topic

Source: Internet
Author: User

Basis
inserts a record into a sorted table to get an ordered table with an additional record. And the most important point is that it moves the record larger than the current element back, to empty out the position of "own" the insertion. The record is an ordered sequence when the n-1 trip is completed.

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] #先与左侧第一个比自己大的交换位置 while
     (x < tarray[j].to_i) do# to find a smaller one than himself, and put it behind
      Tarray[j+1]=tarray[j]
      #puts tarray[j].class
      j=j-1
     End
     tarray[j+1]=x
   end
   i=i+1
  end

a=[5,2,6,4,7,9,8]
insertsort (a)
Print a

[2, 4, 5, 6, 7, 8, 9]>exit code:0

When the code was first written, there was no To_i method at x< Tarray[j], and the following error occurred:

Final.rb:10:in ' < ': Comparison of Fixnum with Nil failed (Argumenterror)

At first I was very confused, then in the output of X.class,tarray[j].class, but both of the output is fixnum. Later found that Ruby's array class is not the same as other, Ruby allows an array object to store different types of elements, when an element of a is assigned to X, can not determine with x compared to A[i] is the fixnum type, so the error, this is only my own understanding.

Advanced
2-way insert sort based on binary insert sort:

def two_way_sort data first,final = 0,0 temp = [] temp[0] = data[0] result = [] len = Data.length to I in 1. (len-1) if data[i]>=temp[final] Final +=1 temp[final] = data[i] elsif data[i]<= Temp[first] (fi  Rst-1 + len)%len Temp[first] = data[i] else if data[i]<temp[0] Low = i = len-1 while
     Low <=high does m = (low + high) >>1 if data[i]>temp[m] Low = m + 1 Else high = m-1 
 
    End-End j = first-1-I--=1-J < high does temp[j] = temp[j+1] J +=1 End

     Temp[high] = data[i] Else low =0 high = final while low <=high does m = (low + high) >>1 If data[i]>=temp[m] Low = m + 1 Else high = M-1 End j = Final + 1 final 
 +=1 while J > I temp[j] = temp[j-1] J-=1 End Temp[low] = data[i] End P TEMP End i = 0 for J in FirsT..


(len-1) Result[i] = temp[j] I +=1 end for J-0..final result[i] = temp[j] I +=1 end
 data = [4,1,5,6,7,2,9,3,8].shuffle p Data result = Two_way_sort Data P 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.