复习一下冒泡排序和其改进
2017-07-21 16:26
274 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace text
{
//=============冒泡
class MyClass
{
public void bubble(int[] a)
{
bool flag = true;
for (int i = 0; i < a.Length && flag; i++)
{
flag = false;
for (int j = a.Length - 1; j > i; j--)
if (a[j] < a[j - 1])
{
int temp = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
flag = true;
}
}
}
}
class Program
{
//============洗牌
static void shuffle(int[] x)
{
Random random = new Random();
for (int i = 0; i < x.Length - 2; i++)
{
int tempIndex = random.Next(i + 1, x.Length - 1);
int k = x[i];
x[i] = x[tempIndex];
x[tempIndex] = k;
}
foreach (int item in x)
{
Console.Write(item + ",");
}
Console.Write("\n");
}
static void Main(string[] args)
{
int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
shuffle(a);
MyClass myClass = new MyClass();
myClass.bubble(a);
Console.WriteLine("冒泡:");
foreach (int item in a)
{
Console.Write(item + ",");
}
}
}
}
改进后添加哨兵,如果从下往上做判断而没有发生交换,那么后续判断就不需要进行了
ps.严格的冒泡排序需要是从下往上的邻位比较,这样一来变换的次数就比较少
相关文章推荐
- 复习一下冒泡排序
- java基础复习——冒泡排序以及改进
- 复习一下冒泡排序,做水题,好像我就只会冒泡了
- 8 排序--选择排序,插入排序,冒泡排序,shell排序,快速排序(递归,迭代,改进版本),归并排序
- 复习一下最长不下降子序列
- 小学初中数据常用定理公式总结-------复习一下
- Java基础复习:线程通信—生产者消费者改进
- 冒泡排序简单改进
- 冒泡排序的算法分析与改进
- 复习一下jdbc原始封装
- 复习一下树的遍历
- 冒泡排序的改进
- 1063 sicily (木有准确理解题意,一直wrong error)——不过这事快速排序的一次复习,还是记录一下
- 复习一下Android系统的启动过程
- 冒泡排序的改进(Modified_Bubble_Sort)
- 【PM复习】进入保护模式再改进
- ASP.NET的编译方法(转,复习一下)
- 简单复习一下计算机网络知识
- 算法学习之排序——冒泡排序及其改进算法(Java)
- java_web里面的代码,想复习一下曾经写过的代码