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:
- Clear
- Numcount = 0
- Input "Enter the number of N to be sorted:" To numcount
- If numcount <1
- ? "The number of input data must be greater than 0! "
- Return
- Endif
- Dime numarr (numcount)
- * Accept numbers from keyboard
- For I = 1 to numcount
- Input "Enter the" + alltrim (STR (I) + "number:" To numarr (I)
- Endfor
- * Print orginal numbers
- ? "Original input series :"
- For I = 1 to numcount
- ?? Alltrim (STR (numarr (I) + ""
- Endfor
- * Sort the Array
- For curindex = 2 to numcount
- Curdata = numarr (curindex)
- Sortedminindex = 1
- Sortedmaxindex = curindex-1
- * Query the positon that the curdata shocould put in
- Do While sortedminindex <= sortedmaxindex
- Midindex = (sortedminindex + sortedmaxindex)/2
- If numarr (midindex) <curdata
- Sortedminindex = sortedminindex + 1
- Else
- Sortedmaxindex = sortedmaxindex-1
- Endif
- Enddo
- * Move data forward index from sortedminindex
- For J = curindex-1 to sortedminindex step-1
- Numarr (J + 1) = numarr (j)
- Endfor
- * Put curdata to its position
- Numarr (sortedminindex) = curdata
- Endfor
- * Print data from min to Max
- ? "Sort from small to large :"
- For I = 1 to numcount
- ?? Alltrim (STR (numarr (I) + ""
- Endfor
- * Print data from Max to Min
- ? "Sort from big to small :"
- For I = numcount to 1 step-1
- ?? Alltrim (STR (numarr (I) + ""
- Endfor
- * Print from both ends to center
- ? "The two ends are large and small :"
- For I = numcount to 1 step-2
- If I> 0
- ?? Alltrim (STR (numarr (I) + ""
- Endif
- Endfor
- If I = 0
- Startindex = 1
- Else
- Startindex = 2
- Endif
- For I = startindex to numcount Step 2
- If I <= numcount
- ?? Alltrim (STR (numarr (I) + ""
- Endif
- 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.