几种常用的排序算法
来源:互联网 发布:平价凉鞋推荐知乎 编辑:程序博客网 时间:2024/06/06 17:16
/**
* 冒泡排序
* 比较相邻的元素。如果第一个比第二个大,就交换他们两个。
* 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。
* 针对所有的元素重复以上的步骤,除了最后一个。
* 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。
* @param numbers 需要排序的整型数组
*/
public static void bubbleSort(int[] numbers)
{
long start=System.currentTimeMillis();
int temp = 0;
for(int i = 0 ; i < numbers.length-1; i ++)
{
for(int j = 0 ;j < numbers.length-1-i ; j++)
{
/*System.out.println(numbers[j]+"----"+numbers[j+1]);*/
if(numbers[j] > numbers[j+1]) //交换两数位置
{
temp = numbers[j];
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
for(int s=0;s<numbers.length;s++){
System.out.print(numbers[s]+" ");
}
System.out.println("");
}
}
}
long end=System.currentTimeMillis();
System.out.println("冒泡排序:"+(end-start));
}
/*
* 查找出中轴(默认是最低位low)的在numbers数组排序后所在位置
*
* @param numbers 带查找数组
* @param low 开始位置
* @param high 结束位置
* @return 中轴所在位置
*/
public static int getMiddle(int[] numbers, int low,int high)
{
long start=System.currentTimeMillis();
int temp = numbers[low]; //数组的第一个作为中轴
while(low < high)
{
while(low < high && numbers[high] > temp)
{
high--;
}
numbers[low] = numbers[high];//比中轴小的记录移到低端
while(low < high && numbers[low] < temp)
{
low++;
}
numbers[high] = numbers[low] ; //比中轴大的记录移到高端
}
numbers[low] = temp ; //中轴记录到尾
long end=System.currentTimeMillis();
System.out.println("查找出中轴"+(end-start));
return low ; // 返回中轴的位置
}
//2)选择排序:
/*每一次从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。*/
public static void sort(int data[]) {
long start=System.currentTimeMillis();
int minVal;
int minIndex;
for (int i = 0; i < data.length - 1; i++) {
minVal = data[i];
minIndex = i;
for (int j = i + 1; j < data.length; j++) {
if (data[j] < minVal) {
minVal = data[j];
minIndex = j;
}
}
if (minVal != data[i] && minIndex != i) {
data[minIndex] = data[i];
data[i] = minVal;
}
}
long end=System.currentTimeMillis();
System.out.println("选择排序"+(end-start));
}
//3)插入排序:
/*将数列分为有序和无序两个部分,每次处理就是将无序数列的第一个元素与有序数列的元素从后往前逐个进行比较,找出插入位置,将该元素插入到有序数列的合适位置中。*/
public static void sort1(int data[]) {
long start=System.currentTimeMillis();
for (int i = 1; i < data.length; i++) {
for (int j = i; j > 0; j--) {
if (data[j] < data[j - 1]) {
int temp = data[j];
data[j] = data[j - 1];
data[j - 1] = temp;
}
for(int s=0;s<data.length;s++){
System.out.print(data[s]+" ");
}
System.out.println("");
}
System.out.println("");
}
long end=System.currentTimeMillis();
System.out.println("插入排序"+(end-start));
}
* 冒泡排序
* 比较相邻的元素。如果第一个比第二个大,就交换他们两个。
* 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。
* 针对所有的元素重复以上的步骤,除了最后一个。
* 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。
* @param numbers 需要排序的整型数组
*/
public static void bubbleSort(int[] numbers)
{
long start=System.currentTimeMillis();
int temp = 0;
for(int i = 0 ; i < numbers.length-1; i ++)
{
for(int j = 0 ;j < numbers.length-1-i ; j++)
{
/*System.out.println(numbers[j]+"----"+numbers[j+1]);*/
if(numbers[j] > numbers[j+1]) //交换两数位置
{
temp = numbers[j];
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
for(int s=0;s<numbers.length;s++){
System.out.print(numbers[s]+" ");
}
System.out.println("");
}
}
}
long end=System.currentTimeMillis();
System.out.println("冒泡排序:"+(end-start));
}
/*
* 查找出中轴(默认是最低位low)的在numbers数组排序后所在位置
*
* @param numbers 带查找数组
* @param low 开始位置
* @param high 结束位置
* @return 中轴所在位置
*/
public static int getMiddle(int[] numbers, int low,int high)
{
long start=System.currentTimeMillis();
int temp = numbers[low]; //数组的第一个作为中轴
while(low < high)
{
while(low < high && numbers[high] > temp)
{
high--;
}
numbers[low] = numbers[high];//比中轴小的记录移到低端
while(low < high && numbers[low] < temp)
{
low++;
}
numbers[high] = numbers[low] ; //比中轴大的记录移到高端
}
numbers[low] = temp ; //中轴记录到尾
long end=System.currentTimeMillis();
System.out.println("查找出中轴"+(end-start));
return low ; // 返回中轴的位置
}
//2)选择排序:
/*每一次从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。*/
public static void sort(int data[]) {
long start=System.currentTimeMillis();
int minVal;
int minIndex;
for (int i = 0; i < data.length - 1; i++) {
minVal = data[i];
minIndex = i;
for (int j = i + 1; j < data.length; j++) {
if (data[j] < minVal) {
minVal = data[j];
minIndex = j;
}
}
if (minVal != data[i] && minIndex != i) {
data[minIndex] = data[i];
data[i] = minVal;
}
}
long end=System.currentTimeMillis();
System.out.println("选择排序"+(end-start));
}
//3)插入排序:
/*将数列分为有序和无序两个部分,每次处理就是将无序数列的第一个元素与有序数列的元素从后往前逐个进行比较,找出插入位置,将该元素插入到有序数列的合适位置中。*/
public static void sort1(int data[]) {
long start=System.currentTimeMillis();
for (int i = 1; i < data.length; i++) {
for (int j = i; j > 0; j--) {
if (data[j] < data[j - 1]) {
int temp = data[j];
data[j] = data[j - 1];
data[j - 1] = temp;
}
for(int s=0;s<data.length;s++){
System.out.print(data[s]+" ");
}
System.out.println("");
}
System.out.println("");
}
long end=System.currentTimeMillis();
System.out.println("插入排序"+(end-start));
}
阅读全文
0 0
- 几种常用的基本排序算法
- C#几种常用的排序算法
- C#几种常用的排序算法
- C#几种常用的排序算法:
- 几种常用的排序算法
- C#几种常用的排序算法
- 几种常用的排序算法
- 几种常用的排序算法
- 几种常用的排序算法
- 几种常用的排序算法
- 几种常用的排序算法
- 常用的几种排序算法
- 几种常用的排序算法
- 几种常用排序算法的比较
- 几种常用的排序算法
- 几种常用的排序算法总结
- 几种常用排序算法的复习
- java几种常用的排序算法
- 使用TabHost点击第一次无法获得数据的问题,自定义TabHost。
- CSRF原理
- 长时间后mysql自动关闭连接
- zynq u-boot网络配置
- 多线程编程
- 几种常用的排序算法
- 【集训】jzoj 2017.7.7 noip模拟赛A 总结
- 欢迎使用CSDN-markdown编辑器
- 关于打印机打印网页出现字迹显示不全的问题心得
- Git常用命令大全,迅速提升你的Git水平
- 高位优先字符串排序
- MySQL的重新安装或卸载、以及删除注册表的信息
- JS_简单实现页面输入
- 文件系统介绍