学习C#中基本的排序算法

1、遍历排序的方法

1、冒泡排序法

冒泡排序的思想是:

两两比较相邻记录的关键码,如果反序则交换,直到没有反序的记录为止。

代码如下:

using System;
using System.Collections;

namespace system2
{
   class Program
  {
       static void Main(string[] args)
      {
           test o = new test();
           int[] a = o.put();
           int[] b = a;
           o.a(a);
           Console.ReadLine();
      }
  }
   public class test
  {
       public int[] put()//输入
      {
           Console.WriteLine("请输入待排序数组:");
           string str = Console.ReadLine();
           string[] str1 = str.Split(",");
           int[] arr = new int[str1.Length];
           for (int i = 0; i < str1.Length; i = i + 1)
          {
               arr[i] = Convert.ToInt16(str1[i]);
          }
           return arr;
      }
       public void a(int[] arr)//冒泡排序
      {
           int exchange = arr.Length - 1;
           int bound;
           int temp;
           int a = 0, b = 0;
           while (exchange != 0)
          {
               bound = exchange;
               exchange = 0;
               for (int i = 0; i < bound; i = i + 1)
              {
                   a = a + 1;
                   if (arr[i] > arr[i + 1])
                  {
                       b = b + 1;
                       temp = arr[i];
                       arr[i] = arr[i + 1];
                       arr[i + 1] = temp;
                       exchange = i;
                  }
              }
          }
           Console.WriteLine("排序后为:");
           foreach (int s in arr)
          {
               Console.Write("{0}\t", s);
          }
           Console.WriteLine("遍历次数为:{0},调换次数为:{1}", a, b);
      }
  }
}

 

2、快速排序法

快速排序的基本思想是:

通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此使整个数据变成有序序列。

using System;
using System.Collections;

namespace system2
{
   class Program
  {
       static void Main(string[] args)
      {
           test o = new test();
           int[] b = o.put();
           o.quick(b, b.Length);
           Console.ReadLine();
      }
  }
   public class test
  {
       public int[] put()//输入
      {
           Console.WriteLine("请输入待排序数组:");
           string str = Console.ReadLine();
           string[] str1 = str.Split(",");
           int[] arr = new int[str1.Length];
           for (int i = 0; i < str1.Length; i = i + 1)
          {
               arr[i] = Convert.ToInt16(str1[i]);
          }
           return arr;
      }
       public void swap(int[] arr,int x,int y)
      {
           int temp = arr[x];
           arr[x] = arr[y];
           arr[y] = temp;
      }
       public void sort(int[] arr,int start,int end)
      {
           if (start >= end)
          {
               return;
          }
           int mid = arr[end];
           int left = start, right = end - 1;
           while (left < right)
          {
               while (arr[left] < mid && left < right)
              {
                   left = left + 1;
              }
               while (arr[right] >= mid && left < right)
              {
                   right = right - 1;
              }
               swap(arr, left, right);
          }
           if (arr[left] >= arr[end])
          {
               swap(arr, left, end);
          }
           else
          {
               left = left + 1;
          }
           if (left>0)
          {
               sort(arr, start, left - 1);
          }
           sort(arr, left + 1, end);
           
      }
       public void quick(int[] arr,int len)
      {
           sort(arr, 0, len - 1);
      }
  }
}

3、直接插入法

基本思想:依次将待排序序列中的每一个记录插入到一个已排好序的序列中,直到全部记录都排好序。

注意:使用直接插入法对数组进行排序时要注意避免数组下标越界的问题。

using System;
using System.Collections;

namespace system2
{
   class Program
  {
       static void Main(string[] args)
      {
           Console.WriteLine("请输入待排序数组:");
           string str = Console.ReadLine();
           string[] str1 = str.Split(",");
           int[] arr = new int[str1.Length];
           for(int i = 0; i < str1.Length; i = i + 1)
          {
               arr[i] = Convert.ToInt16(str1[i]);
          }
           int temp;
           for(int i = 1; i < arr.Length; i = i + 1)
          {
               int j = i - 1;
               temp = arr[i];
               for (; temp < arr[j] && j >= 0; j = j - 1)
              {
                   arr[j + 1] = arr[j];
                   if (j == 0)
                  {
                       j = j - 1;
                       break;
                  }
              }
               arr[j + 1] = temp;
          }
           Console.WriteLine("将数组元素从小到大排列之后为:");
           foreach(int s in arr)
          {
               Console.Write("{0}\t", s);
          }
           Console.ReadLine();
      }
  }
}

4、希尔排序法

希尔排序又称缩小增量排序,其基本思想是:先将整个待排序的一组序列分割成为若干子序列,然后分别进行直接插入排序,待整个序列中的数“基本有序”时再对全体记录进行一次直接插入排序。

using System;
using System.Collections;

namespace system2
{
   class Program
  {
       static void Main(string[] args)
      {
           test o = new test();
           int[] arr = o.get();
           o.sherp(arr);
           Console.ReadLine();
      }
  }
   public class test
  {
       public int[] get()//输入得到数组
      {
           Console.WriteLine("请输入待排序数组:");
           string str = Console.ReadLine();
           string[] str1 = str.Split(",");
           int[] arr = new int[str1.Length];
           for (int i = 0; i < str1.Length; i = i + 1)
          {
               arr[i] = Convert.ToInt16(str1[i]);
          }
           return arr;
      }
       public void sherp(int[] arr)
      {
           int L = arr.Length;
           for(int a = 2; a < arr.Length; a = a * 2)//a为每组个数
          {
               int b = L / a;//b为组数
               for(int c = 0; c < b; c = c + 1)//c为组数
              {
                   arr=insert(arr,c*a,a*(c+1));
              }
          }
           arr = insert(arr,0,arr.Length);
           foreach (int s in arr)
          {
               Console.Write("{0}\t", s);
          }
      }
       public int[] insert(int[] arr,int start,int end)//插入排序法
      {
           int temp;
           for (int i = start+1; i <end; i = i + 1)
          {
               int j = i - 1;
               temp = arr[i];
               for (; temp < arr[j] && j >= 0; j = j - 1)
              {
                   arr[j + 1] = arr[j];
                   if (j == start)
                  {
                       j = j - 1;
                       break;
                  }
              }
               arr[j + 1] = temp;
          }
           for (int i = start; i < end; i = i + 1)
          {
               Console.Write("{0}\t", arr[i]);
          }
           Console.WriteLine();
           return arr;
      }
  }
}

5、选择排序法

选择排序的主要思想是:每趟排序在当前排序序列中选出关键码最小的记录,添加到有序序列中。

using System;
using System.Collections;

namespace system2
{
   class Program
  {
       static void Main(string[] args)
      {
           Console.WriteLine("请输入待排序数组:");
           string str = Console.ReadLine();
           string[] str1 = str.Split(" ");
           int[] arr = new int[str1.Length];
           for(int i = 0; i < str1.Length; i = i + 1)
          {
               arr[i] = Convert.ToInt16(str1[i]);
          }
           int temp;
           for(int i = 0; i < arr.Length; i = i + 1)
          {
               int index = i;
               for (int j = i + 1; j < arr.Length; j = j + 1)
              {
                   if (arr[j] < arr[index])
                  {
                       index = j;
                  }
              }
               if (index != i)
              {
                   temp = arr[index];
                   arr[index] = arr[i];
                   arr[i] = temp;
              }
          }Console.WriteLine("将数组元素从小到大排列之后为:");
           foreach(int s in arr)
          {
               Console.Write("{0} ", s);
          }
           Console.ReadLine();
      }
  }
}

2、Array类的Sort和Reverse排序方法

C#中提供了用于数组进行排序的方法Array.Sort和Array.Reverse。其中,Array.Sort方法用于对一维数组Array数组中的元素进行排序,Array.Reverse方法用于反转一维Array数组或部分Array数组中的元素的顺序。

用Array.Sort方法对数组中的元素进行从小到大的排序

using System;
using System.Collections;

namespace system2
{
   class Program
  {
       static void Main(string[] args)
      {
           int[] arr = new int[] { 3, 9, 27, 18, 12, 21, 15 };
           Array.Sort(arr);
           Console.WriteLine("将数组元素从小到大排列之后为:");
           foreach(int s in arr)
          {
               Console.Write("{0} ", s);
          }
           Console.ReadLine();
      }
  }
}

用Array.Reverse方法对数组中的元素进行反向排序

using System;
using System.Collections;

namespace system2
{
   class Program
  {
       static void Main(string[] args)
      {
           int[] arr = new int[] { 3, 9, 27, 18, 12, 21, 15 };
           Array.Reverse(arr);
           Console.WriteLine("将数组元素从小到大排列之后为:");
           foreach(int s in arr)
          {
               Console.Write("{0} ", s);
          }
           Console.ReadLine();
      }
  }
}

 

如果想要对数组进行从大到小的排序,需要先用Sort再用R

猜你喜欢

转载自www.cnblogs.com/wei1349/p/12764343.html