A sort of choice in VFP implementation

Source: Internet
Author: User
Tags clear screen final integer variables sort

It is well known that the bubble sort (bubble sort) described above is the least efficient one in the common simple sort method. The sort of selection we're introducing today is also one of the simple sorts, but it's more efficient than bubble sorting and easier to implement.

These common methods of sorting are more commonly found in the C/s data, if you want to implement these sorting methods in VFP, the principle is the same, but in the code to achieve a slight difference. For example, the subscript of an array in C/s + + is starting at 0, and the subscript of an array in VFP starts at 1; for statements in C + + can use a for (i=0;i<n;i++) in this form to complete the variable assignment initial value, the variable final judgment, the variable increment these operations, VFP for the statement is somewhat weak, its variable end value can not take i<n this form to define a range. Because of the above differences, in VFP writing code to pay special attention to the array of subscript problems and cyclic variables of the initial value and final values, in the future article will not specifically mention these issues.

Let's take a look at the basic idea of choosing a sort and the sequencing process. (This part of the content quoted from Baidu Encyclopedia: http://baike.baidu.com/view/547263.htm)

Basic ideas

Each trip selects the smallest (or largest) element from the data element to be sorted, sequentially at the end of the ordered sequence, until all the data elements to be sorted are finished.

Selecting a sort is an unstable sort method. N Records of the direct selection of files can be ordered through the N-1 direct selection order results:

① Initial state: Unordered area is R[1..N], ordered area is empty.

② 1th Trip Order

In the unordered region R[1..N], the record of the smallest key word is selected R[k], and it is exchanged with the 1th record r[1] of the disordered region, so that the r[1..1 and R[2..N] respectively become the new ordered region with the number of records and the number of records reduced by 1.

......

③ I trip sort

At the beginning of the I-trip sort, the current ordered and unordered regions are r[1..i-1] and R (1≤i≤n-1) respectively. The trip sort selects the smallest key word record r[k] from the current unordered area, and converts it to the 1th record R of the unordered area, so that the r[1..i] and R change to a new ordered region with a record number and a new unordered region with a record number reduced by 1.

In this way, the direct selection of n recorded files can be ordered by the n-1 of the direct selection order results.

Sorting process

"Sample":

Initial keywords [49 38 65 97 76 13 27 49]

First trip after 13 [38 65 97 76 49 27 49]

The second trip was sorted after 13 27 [65 97 76 49 38 49]

Third trip after 13 27 38 [97 76 49 65 49]

13 27 38 49 [49 97 65 76]

The five-trip sort after 13 27 38 49 49 [97 65 76]

The six-trip sort after 13 27 38 49 49 65 [97 76]

Seventh trip after 13 27 38 49 49 76 [97 76]

Last sorted results 13 27 38 49 49 76 76 97

Know this sort of method of the basic idea and sequencing process, in VFP writing code is simple, just need to pay attention to VFP in the array of the subscript and the cyclic variables of the initial and final value and C/s difference. The running interface of the instance is shown below:

This example still takes the form of 10 random integers to obtain the data, and then use the selection method for these 10 integers from small to large sort. Implementation process:

Create a new form, add an edit box control to the form, and three command buttons, and set the Caption property of the three command buttons as shown above.

Second, add code:

1. Click event for "Generate 10 random integers" button:

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

2. "Clear Screen" button click event: thisform.edit1.value= ""

3. Click event for "Select Sort" button:

Local i,j,k,t As Integer
For I=1 to 9
K=i
For J=i+1 to 10
If S (j) <s (k)
K=j
endif
ENDfor
T=s (k)
S (k) =s (i)
S (i) =t
ENDfor
Thisform.edit1.value= ""
For I=1 to 10
Thisform.edit1.value=thisform.edit1.value+str (S (i), 5)
ENDfor

Three, 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.