QB / QB64 Discussion Forum      Other Subforums, Links and Downloads

Respond to this messageReturn to Index
Original Message
  • My solution. No sort, no swaps, no element compares.
    • (Login emoneo)
      Posted Jul 5, 2012 5:25 PM

      I wrote this solution a few years ago.
      Here's how it works:
      As you get each random number, test it's relative position in the dups array.
      If the position is zero, it means you never saw this number before, so insert the number into the next position of the unique array.

      Then add 1 to the count in the relative position of the dups array. You will be adding 1 to this count for every occurrence of a given number.

      After processing all the random numbers, the unique array will have a list of all the unique numbers that you have.

      That's it.
      --------------------------------------------------------------------------
      DEFINT A-Z
      randonmize timer
      CLS
      CONST MaxElements = 325 'maximum number of elements to be created

      ' Create work arrays
      DIM dups(1 TO MaxElements) 'Keeps a count of duplicates indexed by number.
      DIM unique(1 TO MaxElements)'Stores unique numbers sequentially as they occur.

      for x = 1 TO MaxElements
      number = INT(RND * MaxElements) + 1
      if dups(number)=0 then
      unix=unix+1
      unique(unix)=number
      end if
      dups(number)=dups(number)+1
      next x

      system

      Regards..... Moneo
    Login Status
  • You are not logged in
    • Login
      Password
       

      Optional
      Provides additional benefits such as notifications, signatures, and user authentication.


      Create Account
    Your Name
    Your Email
    (Optional)
    Message Title
    Message Text
    Options Also send responses to my email address
          


     Copyright © 1999-2014 Network54. All rights reserved.   Terms of Use   Privacy Statement