Aspx two-dimensional data sorting code (c #, vb.net)

Source: Internet
Author: User
Tags arrays

 

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

Related Article

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.