⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sort.frm

📁 初级学员的程序_对分查找,可以在计算时进行使用.
💻 FRM
字号:
VERSION 5.00
Begin VB.Form Form1 
   Appearance      =   0  'Flat
   BackColor       =   &H80000005&
   Caption         =   "对分查找"
   ClientHeight    =   5820
   ClientLeft      =   1455
   ClientTop       =   1710
   ClientWidth     =   7365
   ForeColor       =   &H80000008&
   LinkTopic       =   "Form1"
   PaletteMode     =   1  'UseZOrder
   ScaleHeight     =   5820
   ScaleWidth      =   7365
   Begin VB.CommandButton cmdSearch2 
      Caption         =   "对分查找"
      Height          =   375
      Left            =   2760
      TabIndex        =   12
      Top             =   2640
      Width           =   1695
   End
   Begin VB.CommandButton cmdSearch 
      Caption         =   "顺序查找"
      Height          =   375
      Left            =   2760
      TabIndex        =   10
      Top             =   1920
      Width           =   1695
   End
   Begin VB.TextBox txtSearch 
      Height          =   375
      Left            =   3360
      TabIndex        =   8
      Text            =   "txtSearch"
      Top             =   1320
      Width           =   1455
   End
   Begin VB.TextBox txtSize 
      Appearance      =   0  'Flat
      Height          =   285
      Left            =   3600
      TabIndex        =   3
      Text            =   "1000"
      Top             =   360
      Width           =   1335
   End
   Begin VB.CommandButton Command2 
      Appearance      =   0  'Flat
      Caption         =   "开始冒泡排序--->"
      Height          =   375
      Left            =   2640
      TabIndex        =   5
      Top             =   5040
      Width           =   1935
   End
   Begin VB.CommandButton Command1 
      Appearance      =   0  'Flat
      Caption         =   "<--- 自动生成数据"
      Height          =   375
      Left            =   2400
      TabIndex        =   4
      Top             =   720
      Width           =   2535
   End
   Begin VB.ListBox lstSorted 
      Appearance      =   0  'Flat
      Height          =   5070
      Left            =   5040
      TabIndex        =   7
      Top             =   360
      Width           =   2175
   End
   Begin VB.ListBox lstUnsorted 
      Appearance      =   0  'Flat
      Height          =   5070
      Left            =   120
      TabIndex        =   1
      Top             =   360
      Width           =   2175
   End
   Begin VB.Label lblPosition 
      BackColor       =   &H80000005&
      Caption         =   "位置(lblPosition):"
      Height          =   615
      Left            =   2640
      TabIndex        =   11
      Top             =   4080
      Width           =   2175
   End
   Begin VB.Label Label4 
      BackColor       =   &H80000005&
      Caption         =   "查找内容"
      Height          =   255
      Left            =   2520
      TabIndex        =   9
      Top             =   1320
      Width           =   855
   End
   Begin VB.Label Label3 
      Appearance      =   0  'Flat
      AutoSize        =   -1  'True
      BackColor       =   &H80000005&
      Caption         =   "排序数据量"
      ForeColor       =   &H80000008&
      Height          =   195
      Left            =   2400
      TabIndex        =   2
      Top             =   360
      Width           =   900
   End
   Begin VB.Label Label2 
      Appearance      =   0  'Flat
      AutoSize        =   -1  'True
      BackColor       =   &H80000005&
      Caption         =   "已排序"
      ForeColor       =   &H80000008&
      Height          =   195
      Left            =   5040
      TabIndex        =   6
      Top             =   120
      Width           =   540
   End
   Begin VB.Label Label1 
      Appearance      =   0  'Flat
      AutoSize        =   -1  'True
      BackColor       =   &H80000005&
      Caption         =   "待排序:"
      ForeColor       =   &H80000008&
      Height          =   195
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   720
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim mArray()



Private Sub cmdSearch_Click()
    Dim Index, lSearch
    lSearch = CLng(txtSearch)
    Index = 0
    Do While Index <= UBound(mArray) - 1
        If mArray(Index) = lSearch Then
            lblPosition = "找到位置:在数组(" & Index & ")中"
            Exit Do
        End If
        Index = Index + 1
    Loop
    If Index = UBound(mArray) Then
        lblPosition = "在数组中没找到(" & lSearch & ")"
    End If
End Sub

Private Sub cmdSearch2_Click()      '对分查找
    Dim Index, lSearch
    Dim lLeft, lMidd, lRight
    
    lSearch = CLng(txtSearch)
    lLeft = 0
    lRight = UBound(mArray)
    Do While lLeft <= lRight
        lMidd = Fix((lLeft + lRight) / 2)
        If mArray(lMidd) = lSearch Then
            lblPosition = "找到位置:在数组(" & lMidd & ")中"
            Exit Sub
        End If
        If mArray(lMidd) < lSearch Then
            lLeft = lMidd + 1
        Else
            lRight = lMidd - 1
        End If
    Loop
    
    lblPosition = "在数组中没找到(" & lSearch & ")"
    
End Sub

Private Sub Command1_Click()
    Dim i, J
    If Len(Trim$(txtSize)) = 0 Then txtSize = "0"
    ReDim mArray(CDbl(txtSize) - 1)
    
    Randomize
    lstUnsorted.Clear
    For i = 0 To CDbl(txtSize) - 1
        J = Int(32768 * Rnd)
        lstUnsorted.AddItem Str$(J)
        mArray(i) = J
    Next

    Command2.Enabled = True

End Sub

Private Sub Form_Load()

    lstUnsorted.AddItem "322"
    lstUnsorted.AddItem "21571"
    lstUnsorted.AddItem "5111"
    ReDim mArray(0 To 2)
    mArray(0) = 322
    mArray(1) = 21571
    mArray(2) = 5111
    
    txtSearch = 123

End Sub

Private Sub Command2_Click()    '冒泡
    Dim i, Index, TEMP, NextElement
    lstSorted.Clear
    For NextElement = 0 To UBound(mArray) - 1
        For Index = UBound(mArray) To NextElement + 1 Step -1
            If mArray(Index) < mArray(Index - 1) Then
                TEMP = mArray(Index)
                mArray(Index) = mArray(Index - 1)
                mArray(Index - 1) = TEMP
            End If
        Next
    Next
    For i = 0 To UBound(mArray)
        lstSorted.AddItem mArray(i)
    Next
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -