zoukankan      html  css  js  c++  java
  • frequentlyused Algorithm

    anybody can use these code. It's free.
    class SortAlgorithm { public void BubbleSort(int[] R) { int i, j, temp; bool exchange; for (i = 0; i < R.Length; i++) { exchange = false; for (j = 0; j < R.Length - 1; j++) { if (R[j + 1] < R[j]) { temp = R[j + 1]; R[j + 1] = R[j]; R[j] = temp; exchange = true; } } if (!exchange) { break; } } PrintList(R); } public void SelectionSort(int[] list) { int min; for (int i = 0; i < list.Length - 1; i++) { min = i; for (int j = i + 1; j < list.Length; j++) { if (list[j] < list[min]) min = j; } int t = list[min]; list[min] = list[i]; list[i] = t; } PrintList(list); } public void InsertSort(int[] arr) { for (int i = 1; i < arr.Length; i++)//从第二个元素开始 { //1, 5, 3, 6, 10, 55, 9 int temp = arr[i]; int j = i - 1; while (arr[j] > temp)//从从当前元素往右边找插入点 { arr[j + 1] = arr[j];//后移 j--; if (j == -1) break; } arr[j + 1] = temp;//前面已经后移了,会留下一个空位置,现在就插入 PrintList(arr); } } public void PrintList(int[] R) { foreach (int r in R) { Console.WriteLine("value is:{0}", r); } } static void Main(string[] args) { SortAlgorithm sa = new SortAlgorithm(); int[] list = new int[] { 1, 5, 3, 6, 10, 55, 9 }; sa.BubbleSort(list); sa.SelectionSort(list); sa.InsertSort(list); Console.Read(); } }

      

  • 相关阅读:
    k8s 基础 pod操作
    python 字典 get 小例子
    linux 日志
    python 基础 文件操作
    k8s 基础 k8s架构和组件
    k8s 基础 核心概念
    HDU1272--小希的迷宫(并查集)
    POJ1182--食物链(经典并查集)并查集看不出来系列2
    HDU 3038 How Many Answers Are Wrong (并查集)---并查集看不出来系列-1
    二分快速幂
  • 原文地址:https://www.cnblogs.com/ilawrence/p/2860757.html
Copyright © 2011-2022 走看看