Because the. net development environment supports multilingual parties, the C class may be different from the vb.net writing method. Today we have classified both of them. If you need them, you can check them out.
| The Code is as follows: |
Copy code |
Using System; Class Program { Static void Main (string [] args) { // Total number of execution times of the bubble Sort Loop Int BubbleSortCount = 0; // The total number of execution times of the improved Bubble Sorting Loop Int ImprovedBubbleSortCount = 0; // Temporary variables required for Bubble Sorting Int temp; // A staggered two-dimensional array used to save Test Data Int [] [] arr2 = new int [0, 1000] []; // Create a random class for generating random test data Random r = new Random (); // Generate random test data using the preceding random number object through nested for Loop For (int I = 0; I <arr2.Length; I ++) { // Create a new one-dimensional array Int [] arr = new int [10]; For (int j = 0; j <arr. Length; j ++) { // Call the Next method of random Class Object r to generate random test data of less than 1000 Arr [j] = r. Next (1000 ); } // Use a one-dimensional array containing test data as an element of a two-dimensional array Arr2 [I] = arr; } // Save the same test data to test the efficiency of the improved Bubble Sorting Algorithm. Int [] [] ImprovedArr2 = new int [0, 1000] []; For (int I = 0; I <arr2.Length; I ++) { ImprovedArr2 [I] = new int [10]; Arr2 [I]. CopyTo (ImprovedArr2 [I], 0 ); } // Perform Bubble Sorting for each one-dimensional array element of a two-dimensional array, that is, 1000 Bubble Sorting tests. For (int h = 0; h <arr2.Length; h ++) { // Bubble sort For (int I = 1; I <arr2 [h]. Length; I ++) { For (int j = 0; j <arr2 [h]. Length-1; j ++) { If (arr2 [h] [j]> arr2 [h] [j + 1]) // compare each adjacent two numbers { Temp = arr2 [h] [j]; Arr2 [h] [j] = arr2 [h] [j + 1]; Arr2 [h] [j + 1] = temp; } } // Record the sorting of each round BubbleSortCount ++; } } Console. WriteLine ("Use the [Bubble Sorting Algorithm] To sort 1000 Random Arrays with a length of 10:/t" + BubbleSortCount );
// Perform improved Bubble Sorting for each one-dimensional array element of a two-dimensional array, that is, test the improved Bubble Sorting for 1000 times. Bool flag = true; For (int h = 0; h <ImprovedArr2.Length; h ++) { // Use improved Bubble Sorting for testing For (int I = 1; I <ImprovedArr2 [h]. Length; I ++) { // Reset the flag after each round of sorting Flag = true; For (int j = 0; j <ImprovedArr2 [h]. Length-1; j ++) { // Compare each adjacent two numbers If (ImprovedArr2 [h] [j]> ImprovedArr2 [h] [j + 1]) { Temp = ImprovedArr2 [h] [j]; ImprovedArr2 [h] [j] = ImprovedArr2 [h] [j + 1]; ImprovedArr2 [h] [j + 1] = temp;
Flag = false; } } ImprovedBubbleSortCount ++; If (flag) Break; } } Console. WriteLine ("use [improved Bubble Sorting Algorithm] For 1000 Random Arrays with a length of 10:/t" + ImprovedBubbleSortCount ); } |
Vb.net operations
| The Code is as follows: |
Copy code |
Class Program Private Shared Sub Main (args As String ()) 'Total execution times of the Bubble Sorting Loop Dim BubbleSortCount As Integer = 0 'The total number of execution times of the improved Bubble Sorting Loop Dim ImprovedBubbleSortCount As Integer = 0 'Temporary variables required for Bubble Sorting Dim temp As Integer A staggered two-dimensional array used to save Test Data Dim arr2 As Integer () = New Integer (999 )(){} 'Create a random class to generate random test data Dim r As New Random () 'Use the preceding random number object to generate random test data through nested for Loops For I As Integer = 0 To arr2.Length-1 'Create a new one-dimensional array Dim arr As Integer () = New Integer (9 ){} For j As Integer = 0 To arr. Length-1 'By calling the Next method of random Class Object r, the test data of less than 1000 is randomly generated. Arr (j) = r. [Next] (1000) Next 'Use a one-dimensional array containing test data as an element of a two-dimensional array. Arr2 (I) = arr Next 'In addition, the same test data is saved to test the efficiency of the improved Bubble Sorting Algorithm. Dim ImprovedArr2 As Integer () = New Integer (999 )(){} For I As Integer = 0 To arr2.Length-1 ImprovedArr2 (I) = New Integer (9 ){} Arr2 (I). CopyTo (ImprovedArr2 (I), 0) Next 'Perform Bubble Sorting for each one-dimensional array element of a two-dimensional array, that is, 1000 Bubble Sorting tests. For h As Integer = 0 To arr2.Length-1 'Bubble sorting For I As Integer = 1 To arr2 (h). Length-1 For j As Integer = 0 To arr2 (h). Length-2 If arr2 (h) (j)> arr2 (h) (j + 1) Then 'Compare each adjacent two numbers Temp = arr2 (h) (j) Arr2 (h) (j) = arr2 (h) (j + 1) Arr2 (h) (j + 1) = temp End If Next 'Record every round of sorting BubbleSortCount + = 1 Next Next Console. WriteLine ("Use the [Bubble Sorting Algorithm] To sort 1000 Random Arrays with a length of 10:/t" & BubbleSortCount) 'Improve the Bubble Sorting for each one-dimensional array element of a two-dimensional array, that is, test the improved Bubble Sorting for 1000 times. Dim flag As Boolean = True For h As Integer = 0 To ImprovedArr2.Length-1 'Use improved Bubble Sorting for testing For I As Integer = 1 To ImprovedArr2 (h). Length-1 'After each round of sorting, the flag is reset. Flag = True For j As Integer = 0 To ImprovedArr2 (h). Length-2 'Compare each adjacent two numbers If ImprovedArr2 (h) (j)> ImprovedArr2 (h) (j + 1) Then Temp = ImprovedArr2 (h) (j) ImprovedArr2 (h) (j) = ImprovedArr2 (h) (j + 1) ImprovedArr2 (h) (j + 1) = temp Flag = False End If Next ImprovedBubbleSortCount + = 1 If flag Then Exit End If Next Next Console. WriteLine ("Use the [improved Bubble Sorting Algorithm] To sort 1000 Random Arrays with a length of 10:/t" & ImprovedBubbleSortCount) End Sub End Class |