FreeBASIC マニュアルのトップに戻る

FreeBASIC クイック・ソート・ルーチン

目次→フォーラム→FreeBASIC→補足linear indexing of sorted data←オリジナル・サイト

クイック・ソート・ルーチン 左にメニュー・フレームが表示されていない場合は、ここをクリックして下さい

←リンク元に戻る プログラム開発関連に戻る

 このプログラムは、配列データを複合型に登録して、ポインタを使って、クイック・ソートするルーチンです。
 名前、住所、年齢の 3項目のデータを配列に登録して、この配列を、年齢の昇順でソートします。

 アルゼンチンの paul doe さんに教えていただいたものです。

'linear indexing of sorted Data
'Re: Dictionary Class
'by paul doe ≫ May 20, 2018 16:05
'https://www.freebasic.net/forum/viewtopic.php?f=7&p=247578&sid=f1f8d87f561e0314a8c80b12b12a478c#p247578


/'
  Define a composite type, with all it's members public. This will
  represent the 'data' that our app will process.
  複合型を定義します。複合型の全メンバーを公開します。
  これは、アプリが処理する 'データ' を表します。
'/
Type Record
   Public:
      Declare Constructor()
      Declare Constructor( _
         ByRef As Const String, ByRef As Const String, ByVal As Integer )
      
      Declare Destructor()
      
      As String      Name
      As String      address
      As Integer   age
End Type

Constructor Record()
  Name = "Unknown"
  address = "Unknown"
  age = 0
End Constructor

Constructor Record( _
   ByRef pName As Const String, ByRef pAddress As Const String, ByVal pAge As Integer )

   Name = pName
   address = pAddress
   age = pAge
End Constructor

Destructor Record()

End Destructor

'' Sort an array of Records by age
'' レコードの配列を年齢順にソートする
Sub sortArray( ByVal startIndex As Integer, ByVal endIndex As Integer, array() As Record Ptr )
   Dim As Integer firstHalf, secondHalf
   Dim As Integer pivot
   
   firstHalf = startIndex
   secondHalf = endIndex
   
   '' Use the median value as pivot
   '' 中央値をピボットとして使用する
   pivot = array( ( firstHalf + secondHalf ) \ 2 )->age 'Sort an array of Records by age

  Do
    '' Find > pivot
    Do While( array( firstHalf )->age < pivot )         'Sort an array of Records by age
      firstHalf = firstHalf + 1
    Loop
      
      '' Find < pivot
      Do While( array( secondHalf )->age > pivot )      'Sort an array of Records by age
         secondHalf = secondHalf - 1
      Loop
      
      '' Swap if needed
      If( firstHalf <= secondHalf ) Then
         Swap array( firstHalf ), array( secondHalf )
         
         firstHalf = firstHalf + 1
         secondHalf = secondHalf - 1
      End If
   Loop Until( firstHalf > secondHalf )
 
  '' Repeate sort until each half is sorted
  '' 各半分がソートされるまでソートを繰り返す
   If( secondHalf > startIndex ) Then
      sortArray( startIndex, secondHalf, array() )
  End If
   
   If( firstHalf < endIndex ) Then
      sortArray( firstHalf, endIndex, array() )
   End If
End Sub

/'
  Main code
  主コード
'/
Dim As Record Ptr records( 0 To 2 )

records( 0 ) = New Record( "Paul Doe", "1st Street", 36 )
records( 1 ) = New Record( "Jane Doe", "2st Street", 30 )
records( 2 ) = New Record( "Shaiel Doe", "Dreamland", 9 )

'' Sort the array of records
'' レコードの配列をソートする
sortArray( 0, UBound( records ), records() )

For i As Integer = 0 To UBound( records )
  ? records( i )->Name
  ? records( i )->address
  ? records( i )->age
  ?
Next

'' Cleanup
For i As Integer = 0 To UBound( records )
  Delete( records( i ) )
Next

? "何かキー入力でプログラムを終了します。"
Sleep()

ページの頭に戻る
 
補足 に戻る
←リンク元に戻る プログラム開発関連に戻る
ページ歴史:2018-05-23
日本語翻訳:WATANABE Makoto、原文著作者:paul doe

ホームページのトップに戻る

Copyright (c) 2018 paul doe
このページの内容は、MITライセンスで公開します。
https://opensource.org/licenses/mit-license.php