冒泡排序

// 冒泡排序 bubble sort
public static int[] BubbleSort(int []array)
{
bool isContinue = true;
for (int i = ; i < array.Length && isContinue; i++)
{
isContinue = false;
for (int j = ; j < array.Length - i - ; j++)
{
if (array[j] > array[j + ])
{
int temp = array[j];
array[j] = array[j + ];
array[j + ] = temp;
isContinue = true;
}
}
}
return array;
}
双向冒泡

// 双向冒泡 Double sided bubble sort
public static int[] TowWayBubbleSort(int[] array)
{
bool isContinue = true;
for (int i = ; i < array.Length && isContinue; i++)
{
isContinue = false;
for (int j = ; j < array.Length - i - ; j++)
{
if (array[j] > array[j + ])
{
int temp = array[j];
array[j] = array[j + ];
array[j + ] = temp;
isContinue = true;
}
if (array[array.Length - j - ] < array[array.Length - j - ])
{
int temp = array[array.Length - j - ];
array[array.Length - j - ] = array[array.Length - j - ];
array[array.Length - j - ] = temp;
isContinue = true;
}
}
}
return array;
}
插入排序

// 插入排序 Insertion sort
public static int[] InsertionSort(int[] array)
{
for (int i = ; i < array.Length; i++)
{
int current = array[i];
int j = i;
while (j > && current < array[j - ])
{
array[j] = array[j - ];
j--;
}
array[j] = current;
} return array;
}
折半插入

// 折半插入排序 Half insertion sort
public static int[] HalfInsertionSort(int[] array)
{
for (int i = ; i < array.Length; i++)
{
int current = array[i];
int low = ;
int high = i - ;
int mid;
while (low <= high)
{
mid = (low + high) / ;
if (array[mid] < current)
{
low = mid + ;
}
else
{
high = mid - ;
}
}
for (int j = i; j > low; j--)
{
array[j] = array[j - ];
}
array[low] = current;
}
return array;
}
选择排序

// 选择排序 Selection sort
public static int[] SelectionSort(int[] array)
{
for (int i = ; i < array.Length; i++)
{
int min = i;
for (int j = i + ; j < array.Length; j++)
{
if (array[j] < array[min])
{
min = j;
}
}
int temp = array[i];
array[i] = array[min];
array[min] = temp;
} return array;
}
快速排序

// 快速排序 Quick sort
public static void QuickSort(int[] array, int low, int high)
{
if (low < high)
{
int midValue = array[low];
int left = low;
int right = high;
while (left < right)
{
while (left < right && array[right] >= midValue)
{
right--;
}
if (left < right)
{
array[left++] = array[right];
}
while (left < right && array[left] < midValue)
{
left++;
}
if (left < right)
{
array[right--] = array[left];
}
}
array[left] = midValue;
QuickSort(array, low, left - );
QuickSort(array, left + , high);
}
}
找第二大的数

public static int GetSecondNum(int[] array)
{
int first = ;
int second = -;
for (int i = ; i < array.Length; i++)
{
if (array[first] < array[i])
{
second = first;
first = i;
}
else if (second == - || (array[i] < array[first] && array[second] < array[i]))
{
second = i;
}
} return array[second];
}
反转句子单词不反转

public static string WordReverse(string str)
{
char[] array = str.ToArray();
CharArrayReverse(array, , array.Length - );
int start = -;
int end = -;
for (int i = ; i < array.Length; i++)
{
if (!((array[i] >= 'a' && array[i] <= 'z') || (array[i] >= 'A' && array[i] <= 'Z')))
{
if (start < end)
{
CharArrayReverse(array, start + , end);
}
start = i;
}
else
{
end = i;
}
}
return new string(array);
} public static void CharArrayReverse(char[] array, int start, int end)
{
if (array != null && start < array.Length && end < array.Length)
while (start < end)
{
char temp = array[start];
array[start] = array[end];
array[end] = temp;
start++;
end--;
}
}

c# 各种排序算法+找第二大的数+句子单词反转的更多相关文章

  1. 找第二大的数SQL-Second Highest Salary

    1: 找小于最大的最大的 select max(Salary) from Employee where Salary<(select MAX(Salary) from Employee); 2. ...

  2. 排序算法总结第二弹----冒泡排序---javascript描述

    上篇博文总结了选择排序,这篇来看冒泡排序,接上篇. 冒泡排序思想:若是正再将一组数据升序排序, 第一趟:比较相邻的数据,当左侧值大于右侧值将他们进行交换,将较小值向前浮动,大值向后冒泡,直至比较到最后 ...

  3. python找出数组中第二大的数

    #!usr/bin/env python #encoding:utf-8 ''''' __Author__:沂水寒城 功能:找出数组中第2大的数字 ''' def find_Second_large_ ...

  4. 算法笔记_159:算法提高 第二大整数(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 编写一个程序,读入一组整数(不超过20个),当用户输入0时,表示输入结束.然后程序将从这组整数中,把第二大的那个整数找出来,并把它打印出来 ...

  5. 如何使用一次for循环得到数组中第二大的数和第三大的数

    装载声明:http://blog.csdn.net/lxsmk9059/article/details/77920206?locationNum=1&fps=1 ,,,,,,,,}; ]; ] ...

  6. sql求倒数第二大的数,效率不高,但写法新颖

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. 【算法】第二类斯特林数Stirling

    第二类Stirling数实际上是集合的一个拆分,表示将n个不同的元素拆分成m个集合的方案数,记为 或者 . 第二类Stirling数的推导和第一类Stirling数类似,可以从定义出发考虑第n+1个元 ...

  8. Microsoft的考验――查找第二大的数

    #include<stdio.h> int main() { int n,m,t,max,max1; scanf("%d",&n); while(n--) { ...

  9. Python 基础排序算法

    冒泡排序(bubble sort) 思路 以升序为例: 从第一个数开始向后两两对比,将大的数一直向后移动,直至最大的数移到最后,再找第二大的数 最好情况:O(n) 一般情况:O(n^2) 最坏情况:O ...

随机推荐

  1. 浅谈C++虚函数

    很长时间都没写过博客了,主要是还没有养成思考总结的习惯,今天来一发. 我是重度拖延症患者,本来这篇总结应该是早就应该写下来的. 一.虚函数表 C++虚函数的机制想必大家都清楚了.不清楚的同学请参看各种 ...

  2. 第五章_PHP流程控制

    1.顺序结构 2.分支结构 2.1 if...else <?php $today=date("w"); //获取今天星期几 if($today==0){ echo 'Sund ...

  3. linux中的audit审计日志

    这里首先介绍auditctl的应用,具体使用指南查看man auditctl.auditctl的man 描述说明这个工具主要是用来控制audit系统行为,获取audit系统状态,添加或者删除audit ...

  4. 软件工程 speedsnail 冲刺6

    2015-5-10 完成任务:学习了黑马android教学视频13.14.15集,记分功能: 遇到问题: 问题1 android native method not found 解决1 没有解决 明日 ...

  5. URI中的常用属性

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA00AAACDCAIAAADea2ciAAAgAElEQVR4nOz9eTxU////j8/Y930pIb

  6. Creating a Unique File Name

    If you are working with files or file attachments in PeopleCode, you will typically want to create a ...

  7. Winform 打开下载的文件

    private void OpenFile(string filename) { ProcessStartInfo sInfo = new ProcessStartInfo(); sInfo.Wind ...

  8. 当数据0跟if判断冲突的时候

    我是很无奈的,以后都要2,3,4,5这样去标志状态: 分配状态:<select name="is_send" > <option selected="s ...

  9. Java 中判断两个对象是否相等

    由于每次实例化一个对象时,系统会分配一块内存地址给这个对象,而系统默认是根据内存地址来检测是否是同一个对象,所以就算是同一个类里实例化出来的对象它们也不会相等. public class Transp ...

  10. ElasticSearch.js

    ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Apach ...