Two days ago just loaded Python 3.1.1, can't help itched write point code.
1. Select sort
Copy Code code as follows:
>>> def selsort (L):
Length=len (L)
For I in Range (length-1):
Minidx=i
Minval=l[i]
J=i+1
While J<length:
If MINVAL>L[J]:
Minidx=j
MINVAL=L[J]
J=j+1
L[i],l[minidx]=l[minidx],l[i]
Return L
2. Bubble sort
Copy Code code as follows:
>>> def bubsort (L):
Swapped=true
While swapped:
Swapped=false
For I in range (len (L)-1):
If L[I]>L[I+1]:
L[i],l[i+1]=l[i+1],l[i]
Swapped=true
Return L
3. Merge sort
Copy Code code as follows:
>>> def merge (Left,right):
Result=[]
i,j=0,0
While I<len (left) and J<len (right):
If LEFT[I]<=RIGHT[J]:
Result.append (Left[i])
I=i+1
Else
Result.append (Right[j])
J=j+1
Result+=left[i:]
Result+=right[j:]
return result
>>> def mergesort (L):
If Len (L) < 2:
Return L
Else
middle = Int (len (L)/2)
left = MergeSort (L[:middle])
right = MergeSort (l[middle:])
Return merge (left, right)