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 ...
随机推荐
- hdu1505
the main algorithm as the 1506 #include <stdio.h> #include <iostream> #include <strin ...
- Android IOS WebRTC 音视频开发总结(五六)-- 如何测试网络性能?
本文主要介绍如何测试网络性能,文章来自博客园RTC.Blacker,欢迎关注微信公众号blacker,更多详见www.rtc.help 网络性能直接决定了视频通话效果,比如qq,很多时候我们我们觉得通 ...
- 探秘Java中String、StringBuilder以及StringBuffer
探秘Java中String.StringBuilder以及StringBuffer 相信String这个类是Java中使用得最频繁的类之一,并且又是各大公司面试喜欢问 到的地方,今天就来和大家一起学习 ...
- Semantic UI 使用回调函数
html代码: <div class="ui dropdown item" id="region"> <div class="tex ...
- typedef 及其与struct的结合使用
//相当于为现有类型创建一个别名,或称类型别名. //整形等 typedef int size; //字符数组 ]; ];//=> typedef ]; Line text, secondlin ...
- linux device model简述
参考: 1)<LINUX设备驱动程序>第十四章 Linux 设备模型 2)内核源码2.6.38 内核初始化的时候会对设备模型作初始化,见init/main.c: start_kernel- ...
- C#中string[ ] args是什么意思,又有什么用呢
转载:http://blog.sina.com.cn/s/blog_8b7263d1010172jv.html C#控制台程序中static void Main(string[ ] args) str ...
- nginx php 安装
.选定源码目录选定目录 /data/klj/ cd /data/klj/ 2.安装PCRE库cd /data/klj/wget ftp://ftp.csx.cam.ac.uk/pub/software ...
- map与set的遍历
map有四种方式: 1.直接遍历 keySet 2.使用Iterator //注意next放回的对象是map.Entry<K,V>,而使用的iterator是通过entrySet返回的一个 ...
- char型变量理解
char c = 128; printf("%d", c); 问输出是多少? 正确答案应该是-128. 如下几种情况: char c=128;printf("%u\n& ...