答案: 这个程序必须提示使用者提供输入数据,对其进行排序,并输出排序结要。这个程序的设计过程如下: 1.陈述问题我们刚才没有指定要排序的数据类型。如果数据是数字,那么问题的陈述如下。开发一个程序,它能够读取在命令窗口中输入的任意类型的数字,用独立的自定义函数对读取的值进行排序,并在命令窗口写出排序结果。 2.定义输入输出量这个程序的输入值是在命令窗口键入的数字值。这个程序的输出量是写在命令窗口中的排序结果。 3.设计算法这个问题可以分解为三大步:
Read the input data into an array Sort the data in ascending order Write the sorted data
第一大步是读取数据。我们必须提示使用者输入输入数据的个数,然后读取数据。因为我们知道所要读取的数的确切个数,所以可以用for循环主读取合适的数据。它的伪代码如下:
Prompt user for the number of data values Read the number of data values Preallocate an input array for ii = 1:number of values Promptfor next value Readvalue end
下一步,我们必须要用独立的函数对数据进行排序。我们需要对数据进行naval-1次浏览,每一次找出一个最小值。我们将用一个指针来寻找每一次浏览的最小值。一量最小值被找到,如果它不在列表的顶端,它就与列表顶端的元素进行交换。伪代码如下:
for ii = 1:nvals -1 % Find the minimum value in a(ii) through a(nvals) iptr ← ii for jj =ii +1 to nvals ifa(jj) < a(iptr) iptr ← a(iptr) end end % iptr now points to the min value, so swap a(iptr) % with a(ii) if iptr ~= ii. if ii ~=iptr temp ← a(ii) a(ii)← a(iptr) a(iptr) ← temp end end
最后一步是输出排序结果。这个步骤的伪代码不需要重复。最终的伪代码是这三大步伪代码的联合。 4.把伪代码转化为MATLAB语言选择性排序的MATLAB代码如下所示:
function out = ssort(a) %SSORT Selection sort data in ascending order % Function SSORT sorts a numeric data set into % ascending order. Note that the selection sort % is relatively inefficient. DO NOT USE THIS % FUNCTION FOR LARGE DATA SETS. Use MATLAB's % "sort" function instead. % Define variables: % a --Input array to sort % ii --Index variable % iptr --Pointer to min value % jj --Index variable % nvals --Number of values in "a" % out --Sorted output array % temp --Temp variable for swapping % Record of revisions: % Date Programmer Description ofchange % ==== ========== ===================== % 12/19/98 S. J. Chapman Original code % Get the length of the array to sort nvals = size(a,2); % Sort the input array for ii = 1:nvals-1 % Find the minimum value in a(ii) through a(n) iptr =ii; for jj =ii+1:nvals ifa(jj) < a(iptr) iptr = jj; end end % iptr now points to the minimum value, so swapa(iptr) % with a(ii) if ii ~= iptr. if ii ~=iptr temp= a(ii); a(ii)= a(iptr); a(iptr) = temp; end end % Pass data back to caller out = a;
调用选择性排序函数的程序如下:
% Script file: test_ssort.m % % Purpose: % To read in an input data set, sort it intoascending % order using the selection sort algorithm, and to % write the sorted data to the Command window. This % program calls function "ssort" to dothe actual % sorting. % % Record of revisions: % Date Programmer Description ofchange % ==== ========== ===================== % 12/19/98 S. J. Chapman Original code % % Define variables: % array --Input data array % ii --Index variable % nvals --Numberof input values % sorted --Sorted data array % Prompt for the number of values in the data set nvals = input('Enter number of values to sort: '); % Preallocate array array = zeros(1,nvals); % Get input values for ii = 1:nvals %Promptfor next value string =['Enter value ' int2str(ii) ': ']; array(ii)=input(string); end % Now sort the data sorted = ssort(array); % Display the sorted result. fprintf('\nSorted data:\n'); for ii = 1:nvals fprintf('%8.4f \n',sorted(ii)); end
5.检测程序 为了检测这个程序,我们应当创建一个输入数据集,并运行这个程序。这个数据集应当包含由正负数混合组成,还有至少包括一个完全一样的值,以观察在这些情况下程序是否正常工作。
>> test_ssort Enter number of values to sort: 6 Enter value 1: -5 Enter value 2: 4 Enter value 3: -2 Enter value 4: 3 Enter value 5: -2 Enter value 6: 0 Sorted data: -5.0000 -2.0000 -2.0000 0.0000 3.0000 4.0000
对于我们检测的数据,程序给出了正确的结果。注意从正数到负数,还有重复值,这个程序工作正常。
|