Generating random numbers in VFP and realizing bubble sort

Source: Internet
Author: User
Tags clear screen comparison rand sort

This article introduces in VFP to realize the bubble to sort the question, considers makes the example to be good understanding, therefore this article still will use the example way to express. In this case, at the beginning of the design, it is handled in this way: Define a scoped, 10-dimensional array that uses the text box on the form to enter and receive 10 digits consecutively, and then sort the 10 numbers from small to large. But run a bit, think this way good trouble, then changed to use random function to generate 10 numbers and sort them, so this article also involves a little vfp in the random function.

This example runs the following diagram:

Here we go to the point, first look at how to generate random numbers in VFP. Some xueyou thought VFP can't produce random number, in fact VFP has a specialized rand () function to do this thing, about this function can refer to: http://bianceng.cnhttp://www.bianceng.cn/mcc/mcc16/mcc16_rand.htm. It should be noted that this function produces a random number between 0 and 1, which produces a decimal. So this example uses the value generated by this function to multiply by 100, in order to convert the sequence of random numbers produced by the rand () function into integers.

Look at the bubble sort again. The solution to the bubble sort is to compare the two numbers in one column to the next, and then put the small number on the front and the large number in the back of each 22 comparison. You can think of this number as a portrait, each time you compare it to a pebble thrown into a pond, a light bubble floats to the surface, and a heavy pebble sinks to the bottom. Let's take a look at the simple point of the problem, and analyze the process of ordering the 4 number of bubbles from small to large, as shown below:

In the above figure, there are 9, 8, 4, 23 numbers, first comparison will be the first two and 9 of the swap, the second swap of the second and third numbers 9 and 4, the third and fourth numbers 9 and 2, and 3 times 22 to end the first comparison and get a sequence of 8, 4, 2, 9, At this time, the largest number of the 4 number 9 has been "bottom", in the second comparison of the remaining 3 (that is, 8, 4, 2) 22 comparison can be;

In the second comparison, compare 8 and 4 for the first time and put the big one below, and the second to compare 8 and 2 and put the big one below. After these two comparisons, the first comparison of the remaining 3 number of the large number of 8 also has been "bottom". At this point the second trip is over, and we get a sequence of 4, 2, 8, 9, compared to 2 of the remaining 4 (i.e. 2, 22) in the third comparison.

In the third comparison, the remaining two numbers 4 and 2 were compared, swapped, the third trip was over, the whole sort process ended, and the final sort result sequence was obtained: 2, 4, 8, 9.

This is the 4-digit bubble method for the small to large sort of process, can sum up: 4 number of total need to carry out 3 trip comparison, in the first comparison of 22 compared to 3 times, the second comparison needs 22 compared to 2 times, and in the third comparison need 22 compared 1 times, that with the increase in the number of trips, The number of comparisons in a trip is a descending relationship, and the number of 22 comparisons on any trip is the difference between the number of numbers in this sequence and the number of trips. So the problem is generalized by this example: assuming that the number of n is sorted, a total of n-1 trip comparisons are needed, and a n-1 22 comparison is required in the 1th trip comparison, and a n-j 22 comparison is required in the J-trip comparison.

For a simple example of why the number of n needs to be n-1, let's say that there are 10 of people in a circle, and if one of them turns to another friend in turn, how many times does he need to hold his hand? It was obviously 9 times, because he had to exclude himself. Similarly, if it is n individuals need to shake hands n-1 time.

Here we go to the example production process:

Create a new form and add an edit box control Edit1 and 3 command button Command1~command3 to the form.

Second, the 3 command button Command1~command3 Caption property is set to: "Generate 10 random integers", "clear screen" and "bubble sort."

Third, add the event code:

1. Click event for command button Command1 ("Generate 10 random integers"):

Public S (a)
  for I=1 to S (i) =int (rand () *100) && produces a two-digit random integer
  thisform.edit1.value= Thisform.edit1.value+str (S (i), 5)
ENDfor

2, the command button Command2 ("Clear Screen") Click event: thisform.edit1.value= ""

3, the command button Command3 Click event:

For I=1 to 9
  for j=i+1 to
    if S (i) >s (j)
    T=s (i) &&t is an intermediate variable for exchanging the value of two variables
    s (i) =s (j)
    S (j) =t< C11/>endif 
  endfor
endfor
thisform.edit1.value= "" for
I=1 to ten
    thisform.edit1.value= Thisform.edit1.value+str (S (i), 5)
ENDfor
 

Four, OK, run the form bar.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.