c#排序方式

1.插入排序

 for (int i = 0; i < arr.Length; i++)
    {
        int temp = arr[i];
        int j = i;
        while ((j>0)&&(arr[j-1]>temp))
        {
            arr[j] = arr[j - 1];
            --j;
        }
        arr[j] = temp;
    }

2.冒泡排序

 int j, temp;
        for (int i = 0; i < arr.Length-1; i++)
        {
            j = i + 1;
        id:
            if (arr[i]>arr[j])
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
                goto id;
            }
            else
            {
                if (j<(arr.Length-1))
                {
                    j++;
                    goto id;
                }
            }   
        }

3.选择排序

   int min;
        for (int i = 0; i < arr.Length-1; i++)
        {
            min = i;
            for (int j = i+1; j < arr.Length; j++)
            {
                if (arr[j]<arr[min])
                {
                    min = j;
                }
            }
            int t = arr[min];
            arr[min] = arr[i];
            arr[i] = t;

        }

下面是一些c#提供的排序方法

1.升序排序

Array.Sort();

例一(int):

// sort
int arrayint[] intArray = new int[5] { 8, 10, 2, 6, 3 };
Array.Sort(intArray);
// write array
foreach (int i in intArray) Console.Write(i + " "); // output: 2 3 6 8 10

例二(string):

/ sort string array
string [] stringArray = new string[ 5 ] { "Banana" , "Watermelon" , "Tomato" , "Water" , "Apple" };
Array.Sort( stringArray );
// write array
foreach ( string str in stringArray) Console .Write ( str + " " ); // output: Apple Banana Tomato Water Watermelon

2.反转排序

list.Reverse();

int[] MyArray = new string[]{1,2,3,4,5}
Array.Reverse(Myarray);
foreach(int item in Myarray){
      print(item);
}
//return 5 ,4 ,3 ,2, 1
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# 中常用的排序算法有以下几种: 1. 冒泡排序 冒泡排序是一种简单的排序算法,每次比较相邻两个元素的大小并交换位置,重复进行直到数组有序。具体实现如下: ```csharp public static void BubbleSort(int[] arr) { int n = arr.Length; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } ``` 2. 插入排序 插入排序是一种简单且有效的排序算法,将未排序的元素依次插入到已排序的序列中,直到所有元素都有序。具体实现如下: ```csharp public static void InsertionSort(int[] arr) { int n = arr.Length; for (int i = 1; i < n; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } } ``` 3. 选择排序 选择排序是一种简单的排序算法,每次选择数组中最小的元素并将其放到已排序的序列的末尾,直到所有元素都有序。具体实现如下: ```csharp public static void SelectionSort(int[] arr) { int n = arr.Length; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } int temp = arr[minIndex]; arr[minIndex] = arr[i]; arr[i] = temp; } } ``` 4. 快速排序 快速排序是一种高效的排序算法,它使用分治的思想将数组分成两个子数组,然后递归地对这两个子数组进行排序,最后合并两个有序数组。具体实现如下: ```csharp public static void QuickSort(int[] arr, int left, int right) { if (left < right) { int pivotIndex = Partition(arr, left, right); QuickSort(arr, left, pivotIndex - 1); QuickSort(arr, pivotIndex + 1, right); } } private static int Partition(int[] arr, int left, int right) { int pivot = arr[right]; int i = left - 1; for (int j = left; j < right; j++) { if (arr[j] < pivot) { i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp1 = arr[i + 1]; arr[i + 1] = arr[right]; arr[right] = temp1; return i + 1; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值