zoukankan      html  css  js  c++  java
  • C#算法(一)

    一、冒泡排序(Bubble)
    代码
     1     public class BubbleSorter
     2     {
     3         public void Sort(int[] list)
     4         {
     5             int i, j, temp;
     6             bool done = false;
     7             j = 1;
     8             while ((j < list.Length) && (!done))
     9             {
    10                 done = true;
    11                 for (i = 0; i < list.Length - j; i++)
    12                 {
    13                     if (list[i] > list[i + 1])
    14                     {
    15                         done = false;
    16                         temp = list[i];
    17                         list[i] = list[i + 1];
    18                         list[i + 1= temp;
    19                     }
    20                 }
    21                 j++;
    22             }
    23         }
    24     }
    25 
    26     public class MainClass
    27     {
    28         public static void Main()
    29         {
    30             int[] iArrary = new int[] { 151361055992871234,   753347 };
    31             BubbleSorter sh = new BubbleSorter();
    32             sh.Sort(iArrary);
    33             for (int m = 0; m < iArrary.Length; m++)
    34                 Console.Write("{0} ", iArrary[m]);
    35             Console.WriteLine();
    36         }
    37     }
    二、选择排序(Selection)

    代码
     1     public class SelectionSorter
     2     {
     3         private int min;
     4         public void Sort(int[] list)
     5         {
     6             for (int i = 0; i < list.Length - 1; i++)
     7             {
     8                 min = i;
     9                 for (int j = i + 1; j < list.Length; j++)
    10                 {
    11                     if (list[j] < list[min])
    12                         min = j;
    13                 }
    14                 int t = list[min];
    15                 list[min] = list[i];
    16                 list[i] = t;
    17             }
    18         }
    19     }
    20 
    21     public class MainClass
    22     {
    23         public static void Main()
    24         {
    25             int[] iArrary = new int[] { 1536105592871234753347 };
    26             SelectionSorter ss = new SelectionSorter();
    27             ss.Sort(iArrary);
    28             for (int m = 0; m < iArrary.Length; m++)
    29                 Console.Write("{0} ", iArrary[m]);
    30             Console.WriteLine();
    31         }
    32     }

    三、插入排序(InsertionSorter)

    代码
     1     public class InsertionSorter
     2     {
     3         public void Sort(int[] list)
     4         {
     5             for (int i = 1; i < list.Length; i++)
     6             {
     7                 int t = list[i];
     8                 int j = i;
     9                 while ((j > 0&& (list[j - 1> t))
    10                 {
    11                     list[j] = list[j - 1];
    12                     --j;
    13                 }
    14                 list[j] = t;
    15             }
    16         }
    17     }
    18 
    19     public class MainClass
    20     {
    21         public static void Main()
    22         {
    23             int[] iArrary = new int[] { 113361055982871234753347 };
    24             InsertionSorter ii = new InsertionSorter();
    25             ii.Sort(iArrary);
    26             for (int m = 0; m < iArrary.Length; m++)
    27                 Console.Write("{0}", iArrary[m]);
    28             Console.WriteLine();
    29         }
    30     }

    四、希尔排序(ShellSorter)

    代码
     1 
     2     public class ShellSorter
     3     {
     4         public void Sort(int[] list)
     5         {
     6             int inc;
     7             for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
     8             for (; inc > 0; inc /= 3)
     9             {
    10                 for (int i = inc + 1; i <= list.Length; i += inc)
    11                 {
    12                     int t = list[i - 1];
    13                     int j = i;
    14                     while ((j > inc) && (list[j - inc - 1> t))
    15                     {
    16                         list[j - 1= list[j - inc - 1];
    17                         j -= inc;
    18                     }
    19                     list[j - 1= t;
    20                 }
    21             }
    22         }
    23     }
    24 
    25     public class MainClass
    26     {
    27         public static void Main()
    28         {
    29             int[] iArrary = new int[] { 151361055992871234753347 };
    30             ShellSorter sh = new ShellSorter();
    31             sh.Sort(iArrary);
    32             for (int m = 0; m < iArrary.Length; m++)
    33                 Console.Write("{0} ", iArrary[m]);
    34             Console.WriteLine();
    35         }
    36     }
  • 相关阅读:
    cocos2d多语言互调之一---lua调java
    vue 组件
    封装 XMLHttpRequest
    Ajax 请求
    jq 显示和隐藏 计时器
    jq 动画DOM及其操作
    jq 表单提交
    jq 事件绑定
    js 一些实用的封装和兼容
    工厂、原型、混合模式
  • 原文地址:https://www.cnblogs.com/Mayvar/p/wanghonghua200912071933.html
Copyright © 2011-2022 走看看