1、vb 中一维二维数组应用(Application of one dimension two dimensional array in VB)One-dimensional arraysortFirst, select sort method:The data has been placed in a one-dimensional array, requiring small to large sorting.arrayTwentyFourThirty-sixForty-fiveOne hundred and nineThreesubscriptOneTwoThreeN-2N-1NSortin
2、g process:1, select the minimum from the first item to the item n, and then exchange the first item with the minimum item.2, select the minimum from the second item to the item n, and then exchange the second item with the minimum item.3,4, select the minimum from item n-1 to item n, and then exchan
3、ge the item n-1 with the minimum.Note: the minimum and subscript are stored by temporary variables.So, you need two layers of loops: the outer loop I executes n-1 times, and the inner loop J executes n-i-1 timesFor i=1 to n-1The minimum and subscript are stored by temporary variablesItem I of item t
4、mpVal=TmpId= item I subscriptFor j=i+1 to nIf tmpVal J, then:Item J of item tmpVal=TmpId= item J subscriptNextExchange item I with the minimum itemNextFrom big to small?Two, bubble sort method:The data has been placed in a one-dimensional array, requiring small to large sorting.arrayTwentyFourThirty
5、-sixForty-fiveOne hundred and nineThreesubscriptOneTwoThreeN-2N-1NTwo methods: decimal floating and large number sinking.Decimal floating sorting process: from item n to item K, there are two comparisons in turn. If the item M is less than item M-1, the two exchange. (K from 2 to n)First execution:
6、the result is the minimum item from item first to item n in the first item1, if item n is less than item n-1, the item n is exchanged with item n-1.2, if item n-1 is less than item n-2, the item n-1 is exchanged with item n-2.3,4, if second items are less than first items, second items will be excha
7、nged with first items.Second execution: the result is the minimum item from item second to item n in the second item1, if item n is less than item n-1, the item n is exchanged with item n-1.2, if item n-1 is less than item n-2, the item n-1 is exchanged with item n-2.3,4, if third items are less tha
8、n second items, third items will be exchanged with second items.N-1 execution:1, if item n is less than item n-1, the item n is exchanged with item n-1.So, you need two layers of loops: the outer loop I executes n-1 times, and the inner loop J executes n-i timesFor i=1 to n-1For j=n to i+1 step -1If
9、 the value of item J is less than item J-1, then:Item J-1 of item tmp=Item J-1 = item JItem J = TMPNextNextThe descending order process of large numbers: from first items to item K, there are two comparisons in turn, and if the item M is greater than item m+1, the two exchanges. (K from n-1 to 1)For
10、 i=n to 2 step -1For j=1 to I-1If the value of item J item j+1, then:Item j+1 of item tmp=Item j+1 = item JItem J = TMPNextNextFrom big to small?lookupFirst, sequential search method:The data is already in the arrayarrayTwentyFourThirty-sixForty-fiveOne hundred and nineThreesubscriptOneTwoThreeN-2N-
11、1NSearch method: compare the number from first items to item N and the number to be searched.For i=1 to nThe number to be searched is equal to the value of item I Equal returns the I value and terminates the loop.NextTwo, the binary search method:The data has already been placed in a one-dimensional
12、 array, requiring that the number in the array has been sorted.ThreeFourTwentyThirty-sixForty-fiveOne hundred and nineOneTwoThreeFourFiveSixSearching method:In comparison with (1+n) 2, if the item is less than that, it is found between items in the subscript interval (1, (1+n) 2-1), otherwise the it
13、ems in the subscript interval (1+n) 2+1, n) are searched.For example, find 45 in the upper table array1, compare (1+6) 2 items, that is, third items, 4520, find between (4, 6).2, the comparison of item (4+6) 2, that is, fifth items, is found.3, if the search is 47, then in (6, 6) between the search,
14、 compare the (6+6) 2 item, that is, sixth items, and then (7, 6) between, because the interval left larger than the right, so the end of the search.Program code (b array from small to large order):Dim I As Integer, J As Integer, TMP As Integer, bool As Boolean, n As IntegerRem I, J search the index
15、interval, TMP is the middle index of the interval, n is the number to be searched.Bool = TrueI = 1: J = UBound (b)Do While J And bool ITMP = (I + J) 2Select Case B (TMP)Case Is nJ = TMP - 1Case Is nI = TMP + 1Case nBool = FalseEnd SelectLoopIf bool then not foundinsertGenerally refers to the inserti
16、on of ordered sequencesMethod:1, find: find more than the number of the number to be inserted (the closest)2, insert: if the number of the subscript is k, then from n to K, start moving backwards, that is, the item n moves to item n+1, item n-1 moves to item n, Item K moves to item k+1, and then put
17、s the number of inserts in item K.deleteMethod 1: delete item KStarting from item k+1 to item n, move forward in advance, that is, item k+1 moves to item K, item k+2 moves to item k+1, Move item n to item n-1.Method two: delete the value of =N1, find: find the number of =N1, find: the number in the
18、array subscript is k, then from k+1 to N, start moving forward, that is, the item k+1 moves to item K, item k+2 moves to item k+1, Move item n to item n-1.The application of two dimensional array: matrixFirst, generate matrix and display (take 4*4 matrix as an example)Dual cycleDim a (1 to 4,1 to 4)
19、 As Integer 4*4 matrix, whose values consist of row *10+ column valuesFor i=1 to 4For j=1 to 4A (I, J) =i*10+jPrint a (I, J);NextPrint “output a line and then wrap it.“NextTwo. Diagonal of matrixFor i=1 to 4For j=1 to 4If i+j=5 or i=j thenPrint a (I, J);ElsePrint SPC (4);End ifNextPrint “output a li
20、ne and then wrap it.“NextThree, matrix transpose1. Transpose only at output timeFor i=1 to 4For j=1 to 4Print a (J, I);NextPrint “output a line and then wrap it.“Next2, change the array to transpose formFor i=1 to 4For j=1 to I-1Tmp= a (J, I)A (J, I) =a (I, J)A (I, J) =tmpNextNextFour, row exchange
21、(such as the 1 line and the 3 line in the output swap) in the array swap it?For i=1 to 4If i=1 thenP=3Elseif i=3 thenP=1ElseP=iEnd ifFor j=1 to 4Print a (P, J);NextPrint “output a line and then wrap it.“NextFirst, column exchange (such as 2 column and 4 column in the output swap) in the array swap it?For i=1 to 4For j=1 to 4If j=2 thenP=4Elseif j=4 thenP=2ElseP=jEnd ifPrint a (I, P);NextPrint “output a line and then wrap it.“Next