Home > other >  The algorithm of a short answer, small white won't ah.
The algorithm of a short answer, small white won't ah.

Time:12-03

A=[[" li ", "15", "male", "7 grade", "class 2", "3.4"], [" zhao four ", "16" and "male", "eight grade", "1", "4.0"]]
A.a ppend ([" furien ", "15" and "female", "eight grade", "1", "3.3"])
Anderson nsert (0, [" Zhang Xiao ", "17 years", "female", "eight grade", "class 3", "4.2"])

For I in range (len - 1) (a) : # outer loop three times I respectively 0,
For j in range (len - I - 1) (a) : # j respectively 0 ~ 2, 0, 0 ~ 1
If a float (a [I] [1]) & gt; Float (a + 1] [I [1]) :
A [I], a [I + 1)=a (I + 1], a [I]
Print (a)

The last item is scores, want to smallest, thinking is the use of bubble sort, but the result is not correct,

CodePudding user response:

 

Sorted (a, key=lambda x: float (x [1]), reverse=False)

CodePudding user response:

 
A.s ort (key=lambda x: x [1])

CodePudding user response:

The original poster to fishing, upstairs directly to fish,


CodePudding user response:

Just be free, for this kind of to the code, data, are generally willing to try,
The main problem with the original poster is the variables in the second loop body with wrong, should be j instead of I
 

A=[[" li ", "15", "male", "7 grade", "class 2", "3.4"], [" zhao four ", "16" and "male", "eight grade", "1", "4.0"]]
A.a ppend ([" furien ", "15" and "female", "eight grade", "1", "3.3"])
Anderson nsert (0, [" Zhang Xiao ", "17 years", "female", "eight grade", "class 3", "4.2"])


Def show (sr_list, info=' ') :
Print (info)
For I in sr_list:
Print (I)

Show (a, 'current')

For I in range (len - 1) (a) :
For j in range (len - I - 1) (a) : # j respectively 0 ~ 2, 0, 0 ~ 1
If a float (a [j] [1]) & gt; Float (a [m + 1] [1]) :
A [j], a [j + 1)=a [j + 1], a [j]

Show (a, 'results')

CodePudding user response:

Yes, should become a j, may explain why?

CodePudding user response:

Because the outer loop of the I only changed three times under the large cycle is right, I think I know, thank you,

CodePudding user response:

Carefully read algorithm, drawing understanding algorithms, bubbling and dichotomy, is only a foundation, the way of programming, to burn the brain,

CodePudding user response:

Uh-huh, I just started to learn, will come on,
  • Related