2012-12-13 2 views
0

배열로 입력을받는 정렬 프로그램에서 작업하고 있습니다. 나는 이미 Min, Max, Average를 만들었습니다. 이제 Median, Mode, Sorting (Max에서 Min, Min에서 Max까지)을 수행해야합니다. 여기 Sort 함수를 사용하지 않고 VB에서 배열 정렬

내가 정렬에있어 코드의 [업데이트] 새 코드]

 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 
+0

왜 ['Array.Sort'] (http://msdn.microsoft.com/en-us/library/system.array.sort (v = vs.110) .aspx)를 사용할 수 없습니까? –

+0

작업은 정렬 함수를 만드는 것입니다 .. –

+0

여기를 참고하십시오 http://en.wikipedia.org/wiki/Bubble_sort – Teejay

답변

0

이 그것이 할 반전 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 

로 변환 위키 백과에서 선택 정렬 알고리즘은 내림차순 정렬은 어렵지 않아야합니다. 일단 위를 완전히 이해했다면.

+0

감사합니다. @Teejay, 내가 제공 한 코드를 적용하고 이해하는 중입니다. 그냥 질문 : 위의 코드는 작동하지 않을까요? 그것은 논리적으로해야 할 것 같습니다 .. –

+0

@ Ds.109 테스트를 거쳤지만 제대로 작동하는 것으로 보입니다.하지만 출력이 많이 나오므로 마지막 시리즈 만 고려하면됩니다. – Teejay

+0

나에게는 약 20 개의 숫자 만 인쇄됩니다. 입력 {4 1 2 8 5} - 정렬 후 출력 {1 2 5 0 1 2 0 0 1 0 0 0 0 0 0 0 0 0 0} ... 그래서 일종의, 일종의 아니에요. –

0

Sort이 마음에 들지 않으면 OrderBy을 사용할 수 있습니다. LINQ를 사용하면 Min, MaxAverage을 직접 계산할 수 있으므로 바퀴를 다시 만들 필요가 없습니다.

Using LINQ to Calculate Basic Statistics

주 (분산, StandardDeviation, 중간, 모드 등의 코드를 포함) : 자세한 내용은이 문서를 참조 코드는 C#을하지만, 쉽게 VB로 변환 할 수 있습니다. NET, 둘 다. NET 언어이기 때문에.

+0

그는 말했다 : "작업 정렬 기능을 만드는 것입니다 .." – Teejay

+0

@Teejay : 숙제 작업 ... 좋아. 미안하지만. 아무도 최소한의 개발 노력, 즉 비즈니스 필요성으로 통계 기능을 구현해야하는 경우에 대비하여 여기에 답을 남겨 둘 것입니다. – Neolisk

0

다차원 배열을 정렬하려면 단일 차원 배열을 정렬하는 것과 완전히 동일한 절차를 사용할 수 있습니다.

배열을 반복하고 인접한 두 멤버의 순서가 잘못되어있는 경우 스 와이프합니다.

는, 그래서 다중 차원 배열과 같은

Dim Array(12, 4) String 

이 당신이 예를 들어, 마지막 열을 기준으로 배열을 주문한다고 가정 상상해보십시오.

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 column 

Temp4 = 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 way 

Temp3 = Array(index2, 3) 
Array(index2, 3) = Array(index2 + 1, 3) 
Array(index2 + 1, 3) = Temp3 

Temp2 = Array(index2, 2) 
Array(index2, 2) = Array(index2 + 1, 2) 
Array(index2 + 1, 2) = Temp2 

Temp1 = Array(index2, 1) 
Array(index2, 1) = Array(index2 + 1, 1) 
Array(index2 + 1, 1) = Temp1 

Temp0 = Array(index2, 0) 
Array(index2, 0) = Array(index2 + 1, 0) 
Array(index2 + 1, 0) = Temp0 

Next 
Next 

이제 모든 열은 마지막 열로 정렬됩니다.

네 개의 열이 문자열이고 한 개의 열이 숫자 인 경우 정렬 할 수있는 방법이 궁금 할 수 있습니다. 그리고 숫자 열을 기준으로 정렬하려는 경우를 생각해보십시오.

당신은 할 수있는이

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 column 

Temp4 = 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 way 

Temp3 = Array(index2, 3) 
Array(index2, 3) = Array(index2 + 1, 3) 
Array(index2 + 1, 3) = Temp3 

Temp2 = Array(index2, 2) 
Array(index2, 2) = Array(index2 + 1, 2) 
Array(index2 + 1, 2) = Temp2 

Temp1 = Array(index2, 1) 
Array(index2, 1) = Array(index2 + 1, 1) 
Array(index2 + 1, 1) = Temp1 

Temp0 = Array(index2, 0) 
Array(index2, 0) = Array(index2 + 1, 0) 
Array(index2 + 1, 0) = Temp0 

Next 
Next 

내가 행한 모든 후, 단일로 문자열에서 마지막 컬럼에 데이터 형식을 변환 이전과 인접한 값을 비교하는 것입니다.

그래서 이것은 다차원 배열을 정렬하는 아주 기본적인 방법이며, 문자열과 숫자가 포함 된 혼합 배열에도 적용됩니다.

관련 문제