今VB.netで二分探索のプログラムを作ろうとしています。
普通の二分探索のプログラムを作ることはできたのですが再帰を使っての二分探索を行うプログラムを作ろうとして悩んでいます。
再帰を行うために普通の二分探索の一部をプロシージャとして分けるところまではできました。再帰をどこに入れればいいのか分からないのですがご教授願えますでしょうか?
よろしくお願いします。下に現在できているコードを載せておきます。
Private Sub butSarch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSarch.Click
'捜索
Dim intS, intCnt, intL, intR As Integer
intS = InputBox("探索したい数値を入力してください", "数値入力")
intCnt = 1
intL = 0
intR = 9
fucTansaku(intS, intCnt, intL, intR)
End Sub
Private Function fucTansaku(ByVal intS2 As Integer, ByVal intCnt2 As Integer, ByVal intL2 As Integer, ByVal intR2 As Integer) As Integer
Dim intM As Integer
intM = (intL2 + intR2) \ 2
Do While intL2 <= intR2
If intA(intM) = intS2 Then
Exit Do
End If
If intA(intM) < intS2 Then
intL2 = intM + 1
Else
intR2 = intM - 1
End If
intM = (intL2 + intR2) \ 2
intCnt2 = intCnt2 + 1
Loop
If intL2 <= intR2 Then
MsgBox(intCnt2 & "回目の探索で見つかりました", MsgBoxStyle.OkOnly, "結果")
Else
MsgBox("見つかりませんでした", MsgBoxStyle.OkOnly, "結果")
End If
End Function
A 回答 (1件)
- 最新から表示
- 回答順に表示
No.1
- 回答日時:
Option Explicit On
Option Strict On
Option Compare Binary
Option Infer Off
'フォームに表示される内容がどうであるかと
'二分検索のプログラムは分けた方がすっきりするだろう。
'後者のみを作ってみた。
'メモ:アルゴリズムの都合上,その番号は0から探し始めて最初に見つかる番号とは限らない...と思う。
Public Class Q4631055A
Private arr As Integer()
Public Shared Sub Main()
Dim arr1 As Integer() = New Integer(){1,3,5,11,12,13,17,22,25,28}
Dim a As Q4631055A = New Q4631055A(arr1)
System.Console.WriteLine(a.FindIndex(13)) '5(0から数え始めるから)
System.Console.WriteLine(a.FindIndex(14)) '-1(存在しなかった場合)
End Sub
Public Sub New(arr1 As Integer())
arr = arr1
End Sub
Public Function FindIndex(x As Integer) As Integer
Return SubFind(x,0,arr.length - 1)
End Function
Private Function SubFind(x As Integer,left As Integer,right As Integer) As Integer
Dim center As Integer = Integer.Parse(System.Math.Floor((left + right) / 2).ToString())
If left >= right - 1 Then
If x = arr(left) Then
Return left
ElseIf x = arr(right) Then
Return right
Else
Return -1
End If
return -1
End If
If x = arr(center) Then
Return center
ElseIf x > arr(center) Then
Return SubFind(x,center,right)
ElseIf x < arr(center) Then
Return SubFind(x,left,center)
End If
End Function
End Class
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・人生のプチ美学を教えてください!!
- ・10秒目をつむったら…
- ・あなたの習慣について教えてください!!
- ・牛、豚、鶏、どれか一つ食べられなくなるとしたら?
- ・【大喜利】【投稿~9/18】 おとぎ話『桃太郎』の知られざるエピソード
- ・街中で見かけて「グッときた人」の思い出
- ・「一気に最後まで読んだ」本、教えて下さい!
- ・幼稚園時代「何組」でしたか?
- ・激凹みから立ち直る方法
- ・1つだけ過去を変えられるとしたら?
- ・【あるあるbot連動企画】あるあるbotに投稿したけど採用されなかったあるある募集
- ・【あるあるbot連動企画】フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
- ・映画のエンドロール観る派?観ない派?
- ・海外旅行から帰ってきたら、まず何を食べる?
- ・誕生日にもらった意外なもの
- ・天使と悪魔選手権
- ・ちょっと先の未来クイズ第2問
- ・【大喜利】【投稿~9/7】 ロボットの住む世界で流行ってる罰ゲームとは?
- ・推しミネラルウォーターはありますか?
- ・都道府県穴埋めゲーム
- ・この人頭いいなと思ったエピソード
- ・準・究極の選択
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
Bluestacks内でダウンロードし...
-
GPIB制御
-
マルチTIFを一枚のTIFに...
-
acrobat reader dc が正常に起...
-
インテル マネジメント エンジ...
-
VB.netでの二分探索
-
DVDFab Passkeyでブルーレイ開...
-
nethackの作り方
-
65536は2の何乗なのでしょうか?
-
VBAの再計算が反映されない件に...
-
Excelで4096点以上のFFTの方法
-
正しい五十音順について
-
0除算して、落ちるプログラムと...
-
C++でアボート(Abort)で処理が...
-
フォーカスを移さずに、クリッ...
-
モジュール、アプリケーション...
-
discordbotについて
-
変化させるセルが変化しない
-
VBAにてメール作成した際、一部...
-
PICマイコンのコピー(クローン...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
Bluestacks内でダウンロードし...
-
バッチファイルでUSB挿入時に実行
-
GPIB制御
-
アプリのプログラムを見るには...
-
インテル マネジメント エンジ...
-
マルチTIFを一枚のTIFに...
-
brotherのマシニングセンタとP...
-
iTunesがノートンにブロックさ...
-
BIOS.binをバイナリエディタで...
-
例外アプリケーションエラーを...
-
PIC16F84と16F84Aの違い
-
Socket通信だけでSQL Serverに...
-
マルチコアCPUをシングル化
-
PVを稼ぐための自動リロードの...
-
C言語学習用のフリーソフト
-
ちょっとした業務管理ソフトは...
-
再配布できないファイルの作製
-
間違ってインストールしてしま...
-
安全な取り外し
-
プログラミングソフトに打ち込...
おすすめ情報