Sortowanie tablicy w VB bez użycia funkcji sortowania


Pracuję nad programem do sortowania, w którym pobiera dane wejściowe do tablicy. Zrobiłem już Min, Max i Average. Teraz muszę zrobić medianę, mod i sortuje (od Max do min i od min do Max).
Oto kod, który otrzymałem do sortowania [zaktualizowany nowy kod]
RichTextBox1.Text = RichTextBox1.Text.Replace(" ", ",")
marks = RichTextBox1.Text.Split(New String() {","}, StringSplitOptions.RemoveEmptyEntries)
Label3.Text = Nothing Dim z As Integer = marks.Length - 1
Dim y As Integer
Dim TEMP As Integer For X = 1 To z
For y = 1 To (z - 1) If marks(y) > marks(y + 1) Then
TEMP = marks(y)
marks(y) = marks(y + 1)
marks(y + 1) = TEMP End If
Label3.Text = Label3.Text & vbCrLf & marks(y)
Next y
Next X

Zaproszony:
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

To jest algorytm sortowania wyboru z

Wikipedia

przekonwertowany na VB.net
Public Shared Function Sort(ByVal a As Int32()) As Int32()
Dim n As Int32 = a.Length ' a(n-1) is the last element
Dim i As Integer, j As Integer
Dim iMin As Integer ' Advance the position through the entire array
' we could do "from j = 0 to n-2" (that is "for j < n-1")
' because single element is also min element
For j = 0 To n - 2
' Find the min element in the unsorted a[j .. n-1] ' Assume the min is the first element
iMin = j
' Test against elements after j to find the smallest
For i = j + 1 To n - 1
' If this element is less, then it is the new minimum
If a(i) < a(iMin) Then
' Found new minimum, remember its index
iMin = i
End If
Next ' iMin is the index of the minimum element,
' swap it with the current position
If iMin <> j Then
Dim tmp As Int32 = a(j)
a(j) = a(iMin)
a(iMin) = tmp
End If
Next Return a
End Function

Odwrócenie go w celu sortowania z góry na dół nie powinno być trudne, gdy w pełni zrozumiesz powyższe.
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Jeśli nie lubisz funkcji
Sort
, możesz użyć funkcji
OrderBy
. LINQ pozwala również bezpośrednio obliczyć
Min
,
Max
i
Average
, więc nie musisz wymyślać koła na nowo. Więcej informacji znajdziesz w tym artykule:
Używanie LINQ do obliczania podstawowych statystyk
http://www.codeproject.com/Art ... stics
(zawiera kod wariancji, odchylenia standardowego, mediany, trybu itp.)
Uwaga: kod to C #, ale można go łatwo przekonwertować na VB.NET, ponieważ oba języki to .NET.
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Możesz użyć tej samej procedury do sortowania tablicy wielowymiarowej, jak w przypadku sortowania tablicy jednowymiarowej.
Przechodzisz przez tablicę w pętli, a jeśli dwa sąsiednie elementy są w złej kolejności, zamieniasz je.
Wyobraź sobie, że masz wielowymiarową tablicę, taką jak
Dim Array(12, 4) String

Załóżmy więc, że chcesz uporządkować tablicę według ostatniej kolumny, np.
For index1 = 0 to Array.length - 1
For index2 = 0 to Array.length - 2
If Array(index2, 4) > Array(index2 + 1, 4) Then// this sorts the last columnTemp4 = Array(index2, 4)
Array(index2, 4) = Array(index2 + 1, 4)
Array(index2 + 1, 4) = Temp4// and we repeat this swopping pattern for each of the other columns, so they are all sorted in the same wayTemp3 = Array(index2, 3)
Array(index2, 3) = Array(index2 + 1, 3)
Array(index2 + 1, 3) = Temp3Temp2 = Array(index2, 2)
Array(index2, 2) = Array(index2 + 1, 2)
Array(index2 + 1, 2) = Temp2Temp1 = Array(index2, 1)
Array(index2, 1) = Array(index2 + 1, 1)
Array(index2 + 1, 1) = Temp1Temp0 = Array(index2, 0)
Array(index2, 0) = Array(index2 + 1, 0)
Array(index2 + 1, 0) = Temp0Next
Next

Więc teraz wszystkie kolumny są sortowane według ostatniej kolumny.
Możesz się zastanawiać, jak możesz sortować, jeśli 4 kolumny to ciągi, a jedna kolumna to liczba, i załóżmy, że chcesz sortować według kolumny liczb.
Możesz to zrobić
For index1 = 0 to Array.length - 1
For index2 = 0 to Array.length - 2
If CSng(Array(index2, 4)) > CSng(Array(index2 + 1, 4)) Then// this sorts the last columnTemp4 = Array(index2, 4)
Array(index2, 4) = Array(index2 + 1, 4)
Array(index2 + 1, 4) = Temp4// and we repeat this swopping pattern for each of the other columns, so they are all sorted in the same wayTemp3 = Array(index2, 3)
Array(index2, 3) = Array(index2 + 1, 3)
Array(index2 + 1, 3) = Temp3Temp2 = Array(index2, 2)
Array(index2, 2) = Array(index2 + 1, 2)
Array(index2 + 1, 2) = Temp2Temp1 = Array(index2, 1)
Array(index2, 1) = Array(index2 + 1, 1)
Array(index2 + 1, 1) = Temp1Temp0 = Array(index2, 0)
Array(index2, 0) = Array(index2 + 1, 0)
Array(index2 + 1, 0) = Temp0Next
Next

Wszystko, co zrobiłem, to przekonwertowanie typu danych na ostatnią kolumnę z String na Single, a następnie porównanie sąsiednich wartości, jak poprzednio.
Jest to więc bardzo łatwy sposób sortowania tablicy wielowymiarowej, który działa również w przypadku tablic mieszanych zawierających ciągi i liczby.

Aby odpowiedzieć na pytania, Zaloguj się lub Zarejestruj się