0% found this document useful (0 votes)
24 views

Sort Algorithms

The document describes several algorithms for sorting a list of numbers: selection sort, bubble sort, quicksort, and merge sort. It defines functions for each algorithm and tests their performance on a randomly generated list of 50,000 numbers. Selection sort and quicksort are shown to correctly sort the list, with quicksort performing significantly faster on the large dataset.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Sort Algorithms

The document describes several algorithms for sorting a list of numbers: selection sort, bubble sort, quicksort, and merge sort. It defines functions for each algorithm and tests their performance on a randomly generated list of 50,000 numbers. Selection sort and quicksort are shown to correctly sort the list, with quicksort performing significantly faster on the large dataset.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 #~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

2 # UIASS > CPGE > MP/MP* > [email protected]


3 #~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
4 from random import *
5 from time import time
6 #~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
7 def TriSelection ( L ):
8 #pour chaque case i de la liste:
9 for i in range( len(L)-1 ) :
10 #1- chercher la case minimale de la liste à partir de i
11 im = i
12 for j in range(i+1, len(L) ):
13 if L[j] < L[im]: im = j
14 #2- Permuter sa valeur avec celle de la case i
15 L[i], L[im] = L[im], L[i]
16 #sans return <=> return <=> return None
17 #~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
18 def TriBulles ( L ):
19 n = len(L)
20 for j in range( n-1 ):
21 done = True # On suppose qu'il n'y aura pas de permutations
22 for i in range(n-1 -j):
23 if L[i] > L[i+1]:
24 L[i], L[i+1] = L[i+1], L[i]
25 # Il y a une permutation => repeter le traitement
26 done = False
27 if done : return
28 #~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
29 def QuickSort( L ) :
30 if len(L) < 2: return L[ : ]
31 inf = [ ] ; sup = [ ] ; equ = [ ]
32 pivot = L[0]
33 for x in L:# 1- Diviser: partionner L
34 if x < pivot: inf.append( x )
35 elif x > pivot: sup.append( x ) # <=> sup[ len(sup): ] = [x]
36 else : equ.append( x ) # <=> equ += [x]
37 # 2- résoudre les ss-pb
38 inf = QuickSort( inf )
39 sup = QuickSort( sup )
40
41 return inf + equ + sup # 3-Combiner: Concatener
42 #~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
43 def Fusion( A, B ):
44 F = [ ]
45 i = j = 0
46 while i < len(A) and j < len(B):
47 if A[i] < B[j]: F.append(A[i]) ; i = i + 1
48 else : F.append(B[j]) ; j = j + 1
49
50 #if i < len(A): F = F + A[i: ]
51 #else : F = F + B[j: ]
52 return F + A[i: ] + B[j: ]
53 #~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~."""
54 N = 50000
55 L = [ randint(10, 50) for i in range( N ) ] # ; print( L )
56 K, M, N, P = L[ : ], L[ : ], L[ : ], L[ : ]
57 t1=time(); N = QuickSort( N ) ; t2=time(); print(N==sorted(N),f"QS en {t2-t1}s")
58 t1=time(); TriSelection( K );t2=time(); print(K==sorted(K),f"SS en {t2-t1}s")
59 t1=time(); TriBulles( M ) ; t2=time(); print(M==sorted(M),f"BS en {t2-t1}s")
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

You might also like