Home > other >  Python is a small white help based on quick sort why small elements have been the first k cycle
Python is a small white help based on quick sort why small elements have been the first k cycle

Time:10-09

# define exchange element
Def swap (sn, left, right) :
[left] sn, sn [right]=sn [right], sn [left]

# to find quick sort benchmark elements using different take big
Def findpivot (sn, left, right) :
If sn [left] <=sn [left + 1] :
Swap (sn, left, left + 1)

# based on quick sort to find the first k divided into small elements
Def findk (sn, left, right, k) :
Findpivot (sn, left, right)
I=left
J=right
The base=sn [left]
Print (sn)

# move from right to left, encounter less than base, moving from left to right, meet is greater than the base exchange position
While I & lt; J:
While i<[j] j and sn & gt;=base:
J=j - 1
While iI=I + 1
If iSwap (sn, I, j)
If I==j and sn [I] Swap (sn, left, I)

# for the first time after the exchange, look at the relationship between the I and k - 1, if it is i> K - 1, the sort of before I
While I!=k - 1:
If I & gt; K - 1:
Findk (sn, left, I - 1, k)
# if iIf I & lt; K - 1:
Findk (sn, I + 1, right, k - I)

Print (sn/k - 1)
Return


Sn=,1,4,9,2,6,5,30,32,34 [3]
Findk (sn, 0, len (sn) - 1, 4)



Thank you ~ ~ ~

CodePudding user response:

You are no return value of function,

CodePudding user response:

If you just want to sort, find the first k small elements, you can write
 
Def finds (nums, num) :
N=num
While num & gt; 0:
For I in range (len (nums) - 1) :
If nums [I] & gt; Nums [I + 1] :
Nums [I], nums=nums [I + 1], [I + 1] nums [I]
Num=1
-Return nums nums [n - 1)


Nums=(3, 1, 4, 9, 2, 6, 5, 30, 32, 34]
Print (finds (nums, 4))

CodePudding user response:

reference 1st floor max5173 response:
your function has no return value,


Oh oh good ~ thank you actually I want to use the thought of quick sort, is a recursive small element ~ I think we should find the first k was a recursive has been circulating for what to do at this time, please? Thank you ~!!
  • Related