Question Sorting Character on listbox with Quick Sort

anggalisdiyanto

New member
Joined
Jul 1, 2011
Messages
3
Programming Experience
1-3
I cannot sort a list of character on listbox. I just can sort a list of integer data type. I need help, here is my code :


Dim langkah As Integer = 0
    Private nilai2() As Integer
    Private Sub Quicksort2(ByVal list() As Integer, ByVal min As Integer, ByVal max As Integer)
        Dim rand As New Random
        Dim num_letters As Integer = _
    Integer.Parse(txtNumLetters.Text)
        Dim num_words As Integer = _
            Integer.Parse(txtNumWords.Text)
        Dim letters() As Char = _
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
        Dim letter_num As Integer = rand.Next(0, _
    letters.Length - 1)
        Dim word As String = ""
        ' Append the letter.
        word &= letters(letter_num)
        Dim angka_acak As New Random
        Dim nilai_tengah As Integer
        Dim hi As Integer
        Dim lo As Integer
        Dim i As Integer
        Dim a As Integer


        If min >= max Then Exit Sub


        i = rand.Next(min, max + 1)
        nilai_tengah = list(i)


        list(i) = list(min)


        lo = min
        hi = max
        Do
            Do While list(hi) >= nilai_tengah
                hi = hi - 1
                a += 1
                If hi <= lo Then Exit Do
            Loop
            If hi <= lo Then
                a += 1
                list(lo) = nilai_tengah
                Exit Do
            End If


            list(lo) = list(hi)
            a += 1


            lo = lo + 1
            a += 1
            Do While list(lo) < nilai_tengah
                lo = lo + 1
                a += 1
                If lo >= hi Then Exit Do
            Loop
            If lo >= hi Then
                lo = hi
                a += 1
                list(hi) = nilai_tengah
                a += 1
                Exit Do
            End If


            list(hi) = list(lo)
            a += 1
        Loop


        Quicksort(list, min, lo - 1)
        a += 1
        Quicksort(list, lo + 1, max)
        a += 1
        Label1.Text = CStr(a)
    End Sub


    'Sorting words in listbox1
    Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click
        Dim rand As New Random
        Dim num_letters As Integer = _
    Integer.Parse(txtNumLetters.Text)
        Dim num_words As Integer = _
            Integer.Parse(txtNumWords.Text)
        Dim letters() As Char = _
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
        Dim letter_num As Integer = rand.Next(0, _
    letters.Length - 1)
        Dim word As String = ""
        ListBox3.Items.Clear()
        ' Append the letter.
        word &= letters(letter_num)
        Dim i As Integer
        Dim txt As String
        Dim jumlah As Integer = Val(txtNumLetters.Text)
        'ListBox1.Items.Add("")
        'ListBox1.Items.Add("Hasil pengurutan")
        'ListBox1.Items.Add("")
        Quicksort2(nilai2, 0, num_letters)
        For letter_num = 1 To num_letters
            If nilai2(letter_num) < nilai2(letter_num - 1) Then Stop
            word &= letters(nilai2(letter_num))
            ListBox3.Items.Add(word)
        Next letter_num


    End Sub


    Generate random words to listbox2
    Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
        Dim letter_num As Integer
        Dim num_letters As Integer = Integer.Parse(txtNumLetters.Text)
        Dim num_words As Integer = Integer.Parse(txtNumWords.Text)
        Dim letters() As Char = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray
        Dim rand As New Random()
        ListBox4.Items.Clear()
        ReDim nilai2(num_letters)
        For i As Integer = 1 To num_words
            Dim word As String = ""
            For j As Integer = 1 To num_letters
                ' Pick a random number between 0 and 25
                ' to select a letter from the letters array.
                nilai2(letter_num) = rand.Next(0, letters.Length - 1)


                ' Append the letter.
                word &= letters(nilai2(letter_num))
            Next j


            ' Add the word to the list.
            ListBox4.Items.Add(word)
        Next i
    End Sub



Then, this is random string screenshot :
1. This is the random character generated with 5 letters and 10 words :
1.PNG

2. When i do sort, the result (blue rounded) is wrong. It's ugly :
2.PNG

I want to sort the words. Example :
Unsorted random words : zsdfg, akasf, plksn
Then i want to sort them :
Sorted words : akasf, plksn, zsdfg
Anyone can help me what is wrong with my code? I will very happy if there are any answers. Thanks in advance.
 
Last edited:

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

Just set ListBox.Sorted = True and the ListBox will sort things for you as and when you enter text items into the ListBox.

Hope that helps.

Cheers,

Ian
 

anggalisdiyanto

New member
Joined
Jul 1, 2011
Messages
3
Programming Experience
1-3
Hi IanRyder, thankyou for your answer. But i want to do a quick sort, not just a simple like that. My purpose is to implement a quick sort to a list of integer and char data type.
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

I feel like I am missing something obvious here since I am not sure what you are asking for? You quoted that you wanted:-

Unsorted random words : zsdfg, akasf, plksn
Then i want to sort them :
Sorted words : akasf, plksn, zsdfg
Which is what ListBox.Sorted=True would give you.

Are you trying to say that you want to sort your random string instead? i.e UnSortedString = "DEACB" and SortedString = "ABCDE"?

Hope that helps.

Cheers,

Ian
 

anggalisdiyanto

New member
Joined
Jul 1, 2011
Messages
3
Programming Experience
1-3
It's not just sort, i want to analyze the step and timing count in milisecond of quick sort. In this problem, sorting these randommize words with quick sort. Not just sort.
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

My apologies, but trying to decipher your posts is like a round in the final of the Krypton Factor.

I sort of get the idea now that what you are trying to achieve is to time your own Sort routine of a list of random strings held within a ListBox? Am I right?

If this is not the case then please, start again, and describe in explicit detail what you are looking to achieve so that we can help you better.

If this is the case then I have to ask Why? I will NOT take the time to debug your own code for a sort routine when VS provides other class structures which are specifically designed to sort collection elements as you need.

Cheers,

Ian
 

bayu27

New member
Joined
May 14, 2015
Messages
3
Programming Experience
1-3
can I get a quicksort aplication example, please!
I've try, but I can't made it.
 
Top Bottom