c# 各种排序算法+找第二大的数+句子单词反转
// 冒泡排序 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# 各种排序算法+找第二大的数+句子单词反转的更多相关文章
- 找第二大的数SQL-Second Highest Salary
1: 找小于最大的最大的 select max(Salary) from Employee where Salary<(select MAX(Salary) from Employee); 2. ...
- 排序算法总结第二弹----冒泡排序---javascript描述
上篇博文总结了选择排序,这篇来看冒泡排序,接上篇. 冒泡排序思想:若是正再将一组数据升序排序, 第一趟:比较相邻的数据,当左侧值大于右侧值将他们进行交换,将较小值向前浮动,大值向后冒泡,直至比较到最后 ...
- python找出数组中第二大的数
#!usr/bin/env python #encoding:utf-8 ''''' __Author__:沂水寒城 功能:找出数组中第2大的数字 ''' def find_Second_large_ ...
- 算法笔记_159:算法提高 第二大整数(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 编写一个程序,读入一组整数(不超过20个),当用户输入0时,表示输入结束.然后程序将从这组整数中,把第二大的那个整数找出来,并把它打印出来 ...
- 如何使用一次for循环得到数组中第二大的数和第三大的数
装载声明:http://blog.csdn.net/lxsmk9059/article/details/77920206?locationNum=1&fps=1 ,,,,,,,,}; ]; ] ...
- sql求倒数第二大的数,效率不高,但写法新颖
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 【算法】第二类斯特林数Stirling
第二类Stirling数实际上是集合的一个拆分,表示将n个不同的元素拆分成m个集合的方案数,记为 或者 . 第二类Stirling数的推导和第一类Stirling数类似,可以从定义出发考虑第n+1个元 ...
- Microsoft的考验――查找第二大的数
#include<stdio.h> int main() { int n,m,t,max,max1; scanf("%d",&n); while(n--) { ...
- Python 基础排序算法
冒泡排序(bubble sort) 思路 以升序为例: 从第一个数开始向后两两对比,将大的数一直向后移动,直至最大的数移到最后,再找第二大的数 最好情况:O(n) 一般情况:O(n^2) 最坏情况:O ...
随机推荐
- 查询数据库后台Block 的Sql存储过程
查询数据库后台Block 的Sql存储过程 SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO /*记录SQL Server的阻塞情况 wang 200 ...
- 八、Linux下的网络服务器模型
服务器设计技术有很多,按使用的协议来分有TCP服务器和UDP服务器,按处理方式来分有循环服务器和并发服务器. 在网络程序里面,一般来说都是许多客户对应一个服务器,为了处理客户的请求,对服务端的程序就提 ...
- PayPal 开发详解(一):注册PayPal帐号
1.注册paypal帐号 https://www.paypal.com 2.使用刚才注册的paypal帐号登录3.进入开发者中心 4.登录开发者中心 5.登录 查看我们paypal Sandbox测试 ...
- CentOS学习笔记—软件管理程序RPM、YUM
软件管理程序 Linux的软件安装分为源代码编译安装和打包安装.RPM是一种打包安装方式,是由 Red Hat 这家公司开发出来的,后来实在很好用,因此很多 distributions 就使用这个机制 ...
- 解决DataGridView在多线程中无法显示滚动条的问题
在多线程中对DataGridView指定 DataSource 来填充数据,更新数据的时候,会导致DataGridView出现假死,显示错误或者滚动条无法显示的问题,在保证了DataGridView的 ...
- MVC5 Identity 用用户名登录而不用电子邮件
1.修改AccountViewModels ·修改RegisterViewModel public class RegisterViewModel { [Required] [Display(Name ...
- C# .Net三层架构[转]
C# .Net三层架构[转] 编写人:CC阿爸 2014-3-14 希望朋友们留下自己对三层架构的理解... 三层体系结构的概念 用户界面表示层(USL) 业务逻辑层(BLL) 数据访问层(D ...
- Vue.js学习 Item13 – 指令系统与自定义指令
基础 除了内置指令,Vue.js 也允许注册自定义指令.自定义指令提供一种机制将数据的变化映射为 DOM 行为. 可以用 Vue.directive(id, definition) 方法注册一个全局自 ...
- asp.net实现md5加密方法详解
MD5加密简单的说就是把一段明文 通过某种运算方式 求出密文. 例如:明文为:abcdefg 通过一些列运算 得到 密文 7ac66c0f148de9519b8bd264312c4d64 它具有两个特 ...
- Python之Flask Web开发
下载python包管理工具Pip: 访问网址:https://pip.pypa.io/en/stable/installing/ 下载文件get-pip.py到本地计算机 定位到get-pip. ...