当前位置:编程学习 > C#/ASP.NET >>

aspx二维数据排序代码(c#,vb.net)

 

 代码如下 复制代码
using System;
class Program
{
  static void Main(string[] args)
  {
  //冒泡排序 循环总共执行次数
  int BubbleSortCount = 0;
  //改进的冒泡排序 循环总共执行次数
  int ImprovedBubbleSortCount = 0;
  //冒泡排序所需的临时变量
  int temp;
  //一个交错二维数组,用于保存测试数据
  int[][] arr2 = new int[1000][];
  //创建一个随机类,用于生成随机测试数据
  Random r = new Random();
  //通过嵌套的for循环,使用上面的随机数对象生成随机测试数据
  for (int i = 0; i < arr2.Length; i++)
  {
  //新建一个一维数组
  int[] arr = new int[10];
  for (int j = 0; j < arr.Length; j++)
  {
  //通过调用随机类对象 r 的Next方法,随机生成1000以内的测试数据
  arr[j] = r.Next(1000);
  }
  //将包含测试数据的一维数组作为二维数组的元素
  arr2[i] = arr;
  }
  //另外保存一份相同的测试数据,用于测试改进的冒泡排序算法效率
  int[][] ImprovedArr2 = new int[1000][];
  for (int i = 0; i < arr2.Length; i++)
  {
  ImprovedArr2[i] = new int[10]; 
  arr2[i].CopyTo(ImprovedArr2[i], 0);
  }
  //针对二维数组的每个一维数组元素进行冒泡排序,也就是说测试冒泡排序1000次
  for (int h = 0; h < arr2.Length; h++)
  {
  //冒泡排序
  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]) //将每相邻的两个数相比
  {
  temp = arr2[h][j];
  arr2[h][j] = arr2[h][j + 1];
  arr2[h][j + 1] = temp;
  }
  } 
  //记录每一轮排序
  BubbleSortCount++;
  }
  }
  Console.WriteLine("对1000个长度为10的随机数组使用【冒泡排序算法】排序总次数:/t" + BubbleSortCount);
 
  //针对二维数组的每个一维数组元素进行【改进的冒泡排序】,也就是说测试【改进的冒泡排序】1000次
  bool flag = true;
  for (int h = 0; h < ImprovedArr2.Length; h++)
  {
  //使用改进的冒泡排序进行测试
  for (int i = 1; i < ImprovedArr2[h].Length; i++)
  {
  //每轮排序结束后都复位标识位
  flag = true;
  for (int j = 0; j < ImprovedArr2[h].Length - 1; j++)
  {
  //将每相邻的两个数相比
  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("对1000个长度为10的随机数组使用【改进的冒泡排序算法】排序总次数:/t" + ImprovedBubbleSortCount); 
  }

vb.net操作

 代码如下 复制代码

Class Program
    Private Shared Sub Main(args As String())
        '冒泡排序 循环总共执行次数
        Dim BubbleSortCount As Integer = 0
        '改进的冒泡排序 循环总共执行次数
        Dim ImprovedBubbleSortCount As Integer = 0
        '冒泡排序所需的临时变量
        Dim temp As Integer
        '一个交错二维数组,用于保存测试数据
        Dim arr2 As Integer()() = New Integer(999)() {}
        '创建一个随机类,用于生成随机测试数据
        Dim r As New Random()
        '通过嵌套的for循环,使用上面的随机数对象生成随机测试数据
        For i As Integer = 0 To arr2.Length - 1
            '新建一个一维数组
            Dim arr As Integer() = New Integer(9) {}
            For j As Integer = 0 To arr.Length - 1
                '通过调用随机类对象 r 的Next方法,随机生成1000以内的测试数据
                arr(j) = r.[Next](1000)
            Next
            '将包含测试数据的一维数组作为二维数组的元素
            arr2(i) = arr
        Next
        '另外保存一份相同的测试数据,用于测试改进的冒泡排序算法效率
        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
        '针对二维数组的每个一维数组元素进行冒泡排序,也就是说测试冒泡排序1000次
        For h As Integer = 0 To arr2.Length - 1
            '冒泡排序
            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
                        '将每相邻的两个数相比
                        temp = arr2(h)(j)
                        arr2(h)(j) = arr2(h)(j + 1)
                        arr2(h)(j + 1) = temp
                    End If
                Next
                '记录每一轮排序
                BubbleSortCount += 1
            Next
        Next
        Console.WriteLine("对1000个长度为10的随机数组使用【冒泡排序算法】排序总次数:/t" & BubbleSortCount)

        '针对二维数组的每个一维数组元素进行【改进的冒泡排序】,也就是说测试【改进的冒泡排序】1000次
        Dim flag As Boolean = True
        For h As Integer = 0 To ImprovedArr2.Length - 1
            '使用改进的冒泡排序进行测试
            For i As Integer = 1 To ImprovedArr2(h).Length - 1
                '每轮排序结束后都复位标识位
                flag = True
                For j As Integer = 0 To ImprovedArr2(h).Length - 2
                    '将每相邻的两个数相比
                    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 For
                End If
            Next
        Next
        Console.WriteLine("对1000个长度为10的随机数组使用【改进的冒泡排序算法】排序总次数:/t" & ImprovedBubbleSortCount)
    End Sub
End Class

补充:asp.net教程,.Net开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,