xref: /f-stack/app/redis-5.0.5/deps/lua/test/sort.lua (revision 572c4311)
1*572c4311Sfengbojiang-- two implementations of a sort function
2*572c4311Sfengbojiang-- this is an example only. Lua has now a built-in function "sort"
3*572c4311Sfengbojiang
4*572c4311Sfengbojiang-- extracted from Programming Pearls, page 110
5*572c4311Sfengbojiangfunction qsort(x,l,u,f)
6*572c4311Sfengbojiang if l<u then
7*572c4311Sfengbojiang  local m=math.random(u-(l-1))+l-1	-- choose a random pivot in range l..u
8*572c4311Sfengbojiang  x[l],x[m]=x[m],x[l]			-- swap pivot to first position
9*572c4311Sfengbojiang  local t=x[l]				-- pivot value
10*572c4311Sfengbojiang  m=l
11*572c4311Sfengbojiang  local i=l+1
12*572c4311Sfengbojiang  while i<=u do
13*572c4311Sfengbojiang    -- invariant: x[l+1..m] < t <= x[m+1..i-1]
14*572c4311Sfengbojiang    if f(x[i],t) then
15*572c4311Sfengbojiang      m=m+1
16*572c4311Sfengbojiang      x[m],x[i]=x[i],x[m]		-- swap x[i] and x[m]
17*572c4311Sfengbojiang    end
18*572c4311Sfengbojiang    i=i+1
19*572c4311Sfengbojiang  end
20*572c4311Sfengbojiang  x[l],x[m]=x[m],x[l]			-- swap pivot to a valid place
21*572c4311Sfengbojiang  -- x[l+1..m-1] < x[m] <= x[m+1..u]
22*572c4311Sfengbojiang  qsort(x,l,m-1,f)
23*572c4311Sfengbojiang  qsort(x,m+1,u,f)
24*572c4311Sfengbojiang end
25*572c4311Sfengbojiangend
26*572c4311Sfengbojiang
27*572c4311Sfengbojiangfunction selectionsort(x,n,f)
28*572c4311Sfengbojiang local i=1
29*572c4311Sfengbojiang while i<=n do
30*572c4311Sfengbojiang  local m,j=i,i+1
31*572c4311Sfengbojiang  while j<=n do
32*572c4311Sfengbojiang   if f(x[j],x[m]) then m=j end
33*572c4311Sfengbojiang   j=j+1
34*572c4311Sfengbojiang  end
35*572c4311Sfengbojiang x[i],x[m]=x[m],x[i]			-- swap x[i] and x[m]
36*572c4311Sfengbojiang i=i+1
37*572c4311Sfengbojiang end
38*572c4311Sfengbojiangend
39*572c4311Sfengbojiang
40*572c4311Sfengbojiangfunction show(m,x)
41*572c4311Sfengbojiang io.write(m,"\n\t")
42*572c4311Sfengbojiang local i=1
43*572c4311Sfengbojiang while x[i] do
44*572c4311Sfengbojiang  io.write(x[i])
45*572c4311Sfengbojiang  i=i+1
46*572c4311Sfengbojiang  if x[i] then io.write(",") end
47*572c4311Sfengbojiang end
48*572c4311Sfengbojiang io.write("\n")
49*572c4311Sfengbojiangend
50*572c4311Sfengbojiang
51*572c4311Sfengbojiangfunction testsorts(x)
52*572c4311Sfengbojiang local n=1
53*572c4311Sfengbojiang while x[n] do n=n+1 end; n=n-1		-- count elements
54*572c4311Sfengbojiang show("original",x)
55*572c4311Sfengbojiang qsort(x,1,n,function (x,y) return x<y end)
56*572c4311Sfengbojiang show("after quicksort",x)
57*572c4311Sfengbojiang selectionsort(x,n,function (x,y) return x>y end)
58*572c4311Sfengbojiang show("after reverse selection sort",x)
59*572c4311Sfengbojiang qsort(x,1,n,function (x,y) return x<y end)
60*572c4311Sfengbojiang show("after quicksort again",x)
61*572c4311Sfengbojiangend
62*572c4311Sfengbojiang
63*572c4311Sfengbojiang-- array to be sorted
64*572c4311Sfengbojiangx={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}
65*572c4311Sfengbojiang
66*572c4311Sfengbojiangtestsorts(x)
67