A sorting job in Visual Foxpro

Source: Internet
Author: User

Some time ago, I helped a student complete a Visual FoxPro Sorting Algorithm assignment question and recorded it here.

 

I. Question requirements:

The program requires the user to enter n numbers first, finally, the original input series are output in sequence, the series after the ascending order, the series after the ascending to the smallest arrangement, and the series after the large to small arrangement at both ends.

Ii. program code:

  1. Clear
  2. Numcount = 0
  3. Input "Enter the number of N to be sorted:" To numcount
  4. If numcount <1
  5. ? "The number of input data must be greater than 0! "
  6. Return
  7. Endif
  8. Dime numarr (numcount)
  9. * Accept numbers from keyboard
  10. For I = 1 to numcount
  11. Input "Enter the" + alltrim (STR (I) + "number:" To numarr (I)
  12. Endfor
  13. * Print orginal numbers
  14. ? "Original input series :"
  15. For I = 1 to numcount
  16. ?? Alltrim (STR (numarr (I) + ""
  17. Endfor
  18. * Sort the Array
  19. For curindex = 2 to numcount
  20. Curdata = numarr (curindex)
  21. Sortedminindex = 1
  22. Sortedmaxindex = curindex-1
  23. * Query the positon that the curdata shocould put in
  24. Do While sortedminindex <= sortedmaxindex
  25. Midindex = (sortedminindex + sortedmaxindex)/2
  26. If numarr (midindex) <curdata
  27. Sortedminindex = sortedminindex + 1
  28. Else
  29. Sortedmaxindex = sortedmaxindex-1
  30. Endif
  31. Enddo
  32. * Move data forward index from sortedminindex
  33. For J = curindex-1 to sortedminindex step-1
  34. Numarr (J + 1) = numarr (j)
  35. Endfor
  36. * Put curdata to its position
  37. Numarr (sortedminindex) = curdata
  38. Endfor
  39. * Print data from min to Max
  40. ? "Sort from small to large :"
  41. For I = 1 to numcount
  42. ?? Alltrim (STR (numarr (I) + ""
  43. Endfor
  44. * Print data from Max to Min
  45. ? "Sort from big to small :"
  46. For I = numcount to 1 step-1
  47. ?? Alltrim (STR (numarr (I) + ""
  48. Endfor
  49. * Print from both ends to center
  50. ? "The two ends are large and small :"
  51. For I = numcount to 1 step-2
  52. If I> 0
  53. ?? Alltrim (STR (numarr (I) + ""
  54. Endif
  55. Endfor
  56. If I = 0
  57. Startindex = 1
  58. Else
  59. Startindex = 2
  60. Endif
  61. For I = startindex to numcount Step 2
  62. If I <= numcount
  63. ?? Alltrim (STR (numarr (I) + ""
  64. Endif
  65. Endfor

Iii. Programming ideas:

First, we need to use an array to receive the raw data input by the user. Defines the array dimension based on the number of data specified by the user, and receives user input data one by one and saves it to the array. After the user inputs the data, the original sequence is printed.
In order to sort the data in ascending order, ascending order, and ascending order, we must first sort the array in ascending order, then it is relatively easy to implement the other two sorting methods.
There are many ways to sort the original sequence, such as Bubble sorting, selection sorting, Hill sorting, and insertion sorting. Here we choose to use the insertion sorting algorithm.
The general idea is to assume that there is already a sorted sequence, insert a new data into the sequence, and place the new data in the appropriate position of the sorted sequence, ensure that the sequence after inserting new data is still an ordered sequence.
For an original sequence a [1], a [2],… whose length is n, A [n], we can think that the first data a [1] is ordered (that is, this ordered sequence only contains one data a [1]), in this case, we insert the first 2nd data in the original sequence a [2] into the ordered sequence containing only one data, and ensure that the inserted sequence "A [1], A [2] "is still ordered. At this point, the original sequence can be divided into two parts: sorted and unordered. "A [1], a [2]" of 1st and 2nd data are ordered, the Nth to nth data "A [3], a [4],… A [n] "is still unordered. At this time, we continue to insert 3rd data items A [3] into the ordered sequence (the sequence "A [1], a [2]" containing 1st data items and 2nd data items). make sure that the inserted 1st to 3rd data records "A [1], a [2], a [3]" are ordered. So forth until the n data is inserted into an ordered sequence consisting of 1st to the N-1 data, "A [1], a [2],… A [N-1], and ensure that the n data after insertion is orderly.
In general, assume that the first to the first (1 <= I <n) data in the sequence is "A [1], a [2],… A [I] "is sorted, and we insert the I + 1 data into a sequence consisting of 1st to I-1 data, to ensure that the inserted sequence containing the I + 1 data is still ordered, we need to place the I + 1 data a [I + 1] in an appropriate position in the sequence.
Because the sequence "A [1], a [2],… A [I] "is sorted. We can compare data a [I + 1] With the sorted sequence in ascending or descending order, to find the position where a [I + 1] should be in the ordered sequence. For example, data a [I + 1] is in sequence with data a [1], a [2],… If a [I] is compared, if a [I + 1] is smaller than the current data a [J] (1 <= j <= I, place a [I + 1] At A [J]; otherwise, compare with the next data.
Because the sequence "A [1], a [2],… A [n] is sorted. Therefore, when inserting a [I + 1], you can use the compromise search algorithm, instead of comparing a [I + 1] with the data in an ordered series one by one, this greatly reduces the number of comparisons and complexity. Set M = (1 + I)/2, that is, a [m] is a [1]… For the intermediate data of a [I] series, we can directly compare a [I + 1] with a [M]. If a [I + 1]> A [m], then, we continue to convert a [I + 1] and "A [M + 1]… Compare the intermediate data of a [n], and vice versa. Compare the intermediate data of a [s-1. And so on until the position of a [I + 1] is finally found.
In a [1]... After sorting a [n], you can print the ascending sequence in the order of 1. N and... 1.
In order to print the large and small sequences at both ends, we can print them in two steps. The first step is to print from the sequential interval of N .. 1, that is, print a [n], a [N-2], a [N-4]… A [3] and a [1]. (If n is an even number, print a [n], a [N-2], a [N-4]… A [4], a [2]). Step 2: 1... Print the order interval of n a [2], a [4]… A [N-1] (if n is an even number, print a [1], a [3],… A [N-1]).

 

Iv. Program Analysis


1. Save the "Data Count" entered by the user to the variable numcount, declare an array numarr with a length of numcount, and then save the data input by the user to the array numarr.
2. Output all data in the array numarr in a loop, that is, the original sequence of user input.
3. Sort the array numarr in ascending order. From 2nd data records to the last data loop, insert the data into the sorted sequence.
1) curindex is the data to be inserted in the current loop, that is, by this cycle, 1st to curIndex-1 data has been sorted; sortedminindex and sortedmaxindex are the minimum and maximum indexes of sorted sequences;
2) use the compromise lookup method to locate the location of the curindex data in the sorted series (that is, the location of the final sortedminindex );
3) sortedminindex to the curIndex-1 data in turn move;
4) Place the original data in the location of curindex to the sortedminindex location, so that the data from 1 to curindex is sorted.
4. output the sorted array numarr in sequence from 1 to numcount, that is, output a series from small to large;
5. output the sorted data from numcount to 1, that is, the output data series from large to small;
6. Output A series of two large numbers and small numbers:
1) The sequence of output values from numcount to 1, that is, the step of cyclic variable I is-2.
2) After the first step of output is complete, the last output data can only be the first or second data of the numarr array. That is, after the loop ends, the I value is-1 or 0. If I is 0, the last output of the first step is the 2nd data records of numarr. The current output should start with 1st data records; otherwise, the last output is the 1st data records of numarr, this cycle should start with two data records. And this loop is output until the numcount interval.

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.