insertion sort?

Started by
1 comment, last by rippingcorpse 16 years, 2 months ago

Public Class Form1
    Dim Original(0 To 10) As Integer
    Dim Sorted(0 To 10) As Integer
    Dim FinalList As String
    Dim n As Integer
    Dim m As Integer
    Dim pos As Integer
    Dim f As Integer

    Private Sub SortButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SortButton.Click
        If TextBox1.Text = "" Then
            MsgBox("Please enter a value in all the boxes")
            Exit Sub
        ElseIf TextBox2.Text = "" Then
            MsgBox("Please enter a value in all the boxes")
            Exit Sub
        ElseIf TextBox3.Text = "" Then
            MsgBox("Please enter a value in all the boxes")
            Exit Sub
        ElseIf TextBox4.Text = "" Then
            MsgBox("Please enter a value in all the boxes")
            Exit Sub
        ElseIf TextBox5.Text = "" Then
            MsgBox("Please enter a value in all the boxes")
            Exit Sub
        ElseIf TextBox6.Text = "" Then
            MsgBox("Please enter a value in all the boxes")
            Exit Sub
        ElseIf TextBox7.Text = "" Then
            MsgBox("Please enter a value in all the boxes")
            Exit Sub
        ElseIf TextBox8.Text = "" Then
            MsgBox("Please enter a value in all the boxes")
            Exit Sub
        ElseIf TextBox9.Text = "" Then
            MsgBox("Please enter a value in all the boxes")
            Exit Sub
        ElseIf TextBox10.Text = "" Then
            MsgBox("Please enter a value in all the boxes")

           Exit Sub
        End If

        Original(1) = TextBox1.Text
        Original(2) = TextBox2.Text
        Original(3) = TextBox3.Text
        Original(4) = TextBox4.Text
        Original(5) = TextBox5.Text
        Original(6) = TextBox6.Text
        Original(7) = TextBox7.Text
        Original(8) = TextBox8.Text
        Original(9) = TextBox9.Text
        Original(10) = TextBox10.Text

        For n = 1 To 10
            pos = n
            For m = 1 To (n - 1)
                If Sorted(m) > Original(n) Then
                    pos = m
                    For f = (n - 1) To m Step -1
                        Sorted(f + 1) = Sorted(f)
                    Next f
                    m = n
                End If
            Next m
            Sorted(pos) = Original(n)

        Next n

        FinalList = ""
        For n = 1 To 10
            FinalList = FinalList & Str(Sorted(n))
            If n < 10 Then
                FinalList = FinalList & ", "
            Else
                FinalList = FinalList & "."
            End If
        Next n

        SortedValuesLabel.Text = FinalList
    End Sub
End Class


well, theres my code for my insertion sort which i did a few months back at school, but now i'm trying to add as many comments to it as i can, so in the future i can come back and understand it easily. The problem is, i have no idea of how it does what it does. i realise that it creates an unsorted and sorted 'pile' and goes through the list with each one checking where it fits in, but the teacher used his set code, so i have no idea what some of the variables mean at all. especially the f? if someone has the time to help me could you just explain segments of the code (or all of it if possible) in plain english? i've made a few other things but what makes me understand them is clever variable names, but these are just giving me a headache. thanks in advance for any help x btw, i'm using visual basic and visual basic 2005 express edition, thanks x also, one of the main things i can't understand besides the f, is what something - 1 is, i understand for n = 1 to 10 means the numbers 1-10 in the array (i think?) but how the hell does something go into minuses? anyways, thanks again for any help x (sorry Justin Rebuilt :)) [Edited by - qprmanzz on February 2, 2008 7:00:53 PM]
Advertisement
Edit the code and surround it with [ source lang="vb" ] [ /source ] only without all the spaces.
Dim num(100) As Integern = 100For pass = 2 To n    temp = num(pass)    slide = pass        While temp < num(slide - 1)        num(slide) = num(slide - 1)        slide = slide - 1    Wend    num(slide) = tempNext passFor i = 1 To nList1.AddItem num(i)Next i


Try looking at that, it should make more sense.

This topic is closed to new replies.

Advertisement