|
Outils logiciels pour les cours Paris II
Cours Paris II
Stages/ Thèses/ Séminaires |
Cours 5Lire et Ecrire dans un fichier. Le tri rapide
For I=1 to N step 1 .... Next Do While (test) .... Loop
If (test) then Else ... End if
Sub wfile()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim Fileout As Object
Set Fileout = fso.CreateTextFile("C:\Documents and Settings\mdr\Mes documents\VBA\ES.txt", True, True)
M = 20
i = 1
Do While i < M + 1
Cells(i + 1, 2) = "a" & Str(i)
' print in a file
Fileout.Write "a" & Str(i) & Chr(13) & Chr(10)
i = i + 1
Loop
' close the file
Fileout.Close
End Sub
' preparing the file
Open myFile For Input As #1
i = 1
Do Until EOF(1)
Line Input #1, textline
Cells(i+1, 5) = textline
i = i + 1
Loop
' close the file
Close #1
End Sub
Détails de Quicksort (Wikipedia) Sub Main()
Dim myArray(15) As Double
For I = 1 To 15 Step 1
myArray(I) = Cells(I, 1)
Next
Call QSort(myArray, 0, 4)
For I = 1 To 15 Step 1
Cells(I, 6) = myArray(I)
Next
End Sub
Sub QSort(sortArray() As Double, ByVal leftIndex As Integer, _
ByVal rightIndex As Integer)
Dim compValue As Double
Dim I As Integer
Dim J As Integer
Dim tempNum As Double
I = leftIndex
J = rightIndex
compValue = sortArray(Int((I + J) / 2))
Do
Do While (sortArray(I) < compValue And I < rightIndex)
I = I + 1
Loop
Do While (compValue < sortArray(J) And J > leftIndex)
J = J - 1
Loop
If I <= J Then
tempNum = sortArray(I)
sortArray(I) = sortArray(J)
sortArray(J) = tempNum
I = I + 1
J = J - 1
End If
Loop While I <= J
If leftIndex < J Then QSort sortArray(), leftIndex, J
If I < rightIndex Then QSort sortArray(), I, rightIndex
End Sub
Recherche dans un tableau trié A(N), d'une valeur a:
|