Quick Sort Demo : Sort « Data Structure « VB.Net

VB.Net
1. 2D
2. Application
3. Class
4. Data Structure
5. Database ADO.net
6. Development
7. Event
8. File Directory
9. Generics
10. GUI
11. Language Basics
12. Network Remote
13. Thread
14. Windows System
15. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
VB.Net » Data Structure » SortScreenshots 
Quick Sort Demo
Quick Sort Demo

Imports System

Public Class MainClass
    Private Shared Values() As Integer
    Private Const NUM_VALUES As Integer = 99
    
    Shared Sub Main(ByVal args As String())
        Dim random_number As New Random
        Dim As Integer
        
        ReDim Values(NUM_VALUES)
        For i = To NUM_VALUES
            Values(i= random_number.Next(1001000)
            Console.WriteFormat$(Values(i)) " " )
        Next i

        Console.WriteLine"Sorted" )

        
        ' Sort the numbers.
        Quicksort(Values, 0, NUM_VALUES)
        For i = To NUM_VALUES
            Console.WriteFormat$(Values(i)) " " )
        Next i
        

    End Sub
    ' "Ready-to-Run Visual Basic Algorithms"
    ' (http://www.vb-helper.com/vba.htm).
    Shared Public Sub Quicksort(ByVal list() As Integer, ByVal min As Integer, ByVal max As Integer)
        Dim random_number As New Random
        Dim med_value As Integer
        Dim hi As Integer
        Dim lo As Integer
        Dim i As Integer

        ' If min >= max, the list contains 0 or 1 items so
        ' it is sorted.
        If min >= max Then Exit Sub

        ' Pick the dividing value.
        i = random_number.Next(min, max + 1)
        med_value = list(i)

        ' Swap it to the front.
        list(i) = list(min)

        lo = min
        hi = max
        Do
            ' Look down from hi for a value < med_value.
            Do While list(hi) >= med_value
                hi = hi - 1
                If hi <= lo Then Exit Do
            Loop
            If hi <= lo Then
                list(lo) = med_value
                Exit Do
            End If

            ' Swap the lo and hi values.
            list(lo) = list(hi)

            ' Look up from lo for a value >= med_value.
            lo = lo + 1
            Do While list(lo) < med_value
                lo = lo + 1
                If lo >= hi Then Exit Do
            Loop
            If lo >= hi Then
                lo = hi
                list(hi) = med_value
                Exit Do
            End If

            ' Swap the lo and hi values.
            list(hi) = list(lo)
        Loop

        ' Sort the two sublists.
        Quicksort(list, min, lo - 1)
        Quicksort(list, lo + 1, max)
    End Sub
    

End Class

           
       
Related examples in the same category
w___ww__.__j___a___va2__s___.___c__o__m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.