快速排序,冒泡排序,插入排序 完整示例
2011-07-15 10:19
330 查看
快速排序
using System.Collections.Generic;
using System.Collections;
using System.Text;
namespace ConsoleApplication12
{
class Program
{
static void Main(string[] args)
{
int[] number=new int[]{5679,4,36,67976,124,235,453,436,6589,0,5,6,346,457,34,3642};
QuickSort(ref number);
foreach (int j in number)
{
Console.WriteLine(j);
}
Console.ReadKey();
}
static void QuickSort(ref int[] number)
{
for (int i = 0; i < number.Length; i++)
{
// 改变排序方式, 只要改变这里的比较方式
for (int j = 0; j < number.Length - i-1; j++)
{
if (number[j]>number[j+1])
{
int temp = number[j];
number[j] = number[j + 1];
number[j + 1] = temp;
}
}
}
}
}
}
插入排序
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
namespace ConsoleApplication12
{
class Program
{
static void Main(string[] args)
{
int[] number=new int[]{5679,4,36,67976,124,235,453,436,6589,0,5,6,346,457,34,3642};
QuickSort(ref number);
foreach (int j in number)
{
Console.WriteLine(j);
}
Console.ReadKey();
}
static void QuickSort(ref int[] number)
{
for (int i = 1; i < number.Length; i++)
{
int temp = number[i];
int j=i;
while (j>0 &&number[j-1]>temp)
{
number[j] = number[j-1];
--j;
}
number[j] = temp;
}
}
}
}
using System; using System.Collections.Generic; using System.Collections; using System.Text; namespace ConsoleApplication12 { class Program { static void Main(string[] args) { ArrayList at = new ArrayList(); int[] number = new int[] { 5679, 4, 36, 67976, 124, 235, 453, 436, 6589, 0, 5, 6, 346, 457, 34, 3642 }; foreach (int i in number) { at.Add(i); } ArrayList al = QuickSort(at); foreach (int j in al) { Console.WriteLine(j); } Console.ReadKey(); } static ArrayList QuickSort(ArrayList arrayList) { if (arrayList.Count <= 1) return arrayList; int pivotIndex = arrayList.Count / 2; int pivot = (int)arrayList[pivotIndex]; arrayList.Remove(pivot); ArrayList left = new ArrayList(); ArrayList right = new ArrayList(); for (int i = 0; i < arrayList.Count; i++) { // 改变排序方式, 只要改变这里的比较方式 if ((int)arrayList[i] < pivot) { left.Add(arrayList[i]); } else { right.Add(arrayList[i]); } } ArrayList newArrayList = QuickSort(left); newArrayList.Add(pivot); newArrayList.AddRange(QuickSort(right)); return newArrayList; } } } 冒泡排序using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
namespace ConsoleApplication12
{
class Program
{
static void Main(string[] args)
{
int[] number=new int[]{5679,4,36,67976,124,235,453,436,6589,0,5,6,346,457,34,3642};
QuickSort(ref number);
foreach (int j in number)
{
Console.WriteLine(j);
}
Console.ReadKey();
}
static void QuickSort(ref int[] number)
{
for (int i = 0; i < number.Length; i++)
{
// 改变排序方式, 只要改变这里的比较方式
for (int j = 0; j < number.Length - i-1; j++)
{
if (number[j]>number[j+1])
{
int temp = number[j];
number[j] = number[j + 1];
number[j + 1] = temp;
}
}
}
}
}
}
插入排序
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
namespace ConsoleApplication12
{
class Program
{
static void Main(string[] args)
{
int[] number=new int[]{5679,4,36,67976,124,235,453,436,6589,0,5,6,346,457,34,3642};
QuickSort(ref number);
foreach (int j in number)
{
Console.WriteLine(j);
}
Console.ReadKey();
}
static void QuickSort(ref int[] number)
{
for (int i = 1; i < number.Length; i++)
{
int temp = number[i];
int j=i;
while (j>0 &&number[j-1]>temp)
{
number[j] = number[j-1];
--j;
}
number[j] = temp;
}
}
}
}
相关文章推荐
- 选择排序、冒泡排序、插入排序、基数排序、快速排序、归并排序完整C++实现
- java必须知道的八大种排序算法:冒泡排序、 选择排序、插入排序、快速排序、希尔算法、归并排序算法、基数排序、堆排序算法
- Java-冒泡排序、快速排序、插入排序、快速排序
- 快速排序、插入排序、选择排序和冒泡排序的实现
- 几种简单的排序:选择排序,插入排序,快速排序,冒泡排序
- 各种排序算法实现——基数排序、归并排序、插入排序、冒泡排序、选择排序、快速排序、堆排序、希尔排序
- 浅谈C++之冒泡排序、希尔排序、快速排序、插入排序、堆排序、基数排序性能对比分析(好戏在后面,有图有真相)
- 排序算法合集(插入排序,折半插入排序,希尔排序,冒泡排序,快速排序,简单选择排序,堆排序,归并排序)
- Java中的几种排序算法:冒泡排序,插入排序,二分法排序,简单排序,快速排序
- Java-冒泡排序、快速排序、插入排序、快速排序
- 浅谈C++之冒泡排序、希尔排序、快速排序、插入排序、堆排序、基数排序性能对比分析之后续补充说明(有图有真相)
- 【C++】冒泡排序、插入排序、快速排序
- 七大内部排序算法总结(插入排序、希尔排序、冒泡排序、简单选择排序、快速排序、归并排序、堆排序)
- 几种常用的排序算法的分析及java实现(希尔排序,堆排序,归并排序,快速排序,选择排序,插入排序,冒泡排序)
- C#实现所有经典排序算法(选择排序,冒泡排序,快速排序,插入排序,希尔排序)
- js冒泡排序,快速排序,插入排序
- PHP四种排序算法实现及效率分析【冒泡排序,插入排序,选择排序和快速排序】
- 已完成以下算法:冒泡排序,插入排序,希尔排序,快速排序,基数排序
- 浅谈C++之冒泡排序、希尔排序、快速排序、插入排序、堆排序、基数排序性能对比分析
- PHP常用排序算法实例小结【基本排序,冒泡排序,快速排序,插入排序】