栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java数组排序示例(冒泡排序、快速排序、希尔排序、选择排序)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java数组排序示例(冒泡排序、快速排序、希尔排序、选择排序)

快速排序法主要是运用了Arrays中的一个方法Arrays.sort()实现。

冒泡法是运用遍历数组进行比较,通过不断的比较将最小值或者最大值一个一个的遍历出来。

选择排序法是将数组的第一个数据作为最大或者最小的值,然后通过比较循环,输出有序的数组。

插入排序是选择一个数组中的数据,通过不断的插入比较最后进行排序。

复制代码 代码如下:
package com.firewolf.sort;

public class MySort {

 
 public static void main(String[] args) {
  int array[] = {45,32,54,12,43,65,11,3,43,6,33,90,44,1,178};
  MySort mySort = new MySort();
  mySort.insertSort(array);
  System.out.print("插入排序结果 :  ");
  mySort.printArray(array);
  System.out.println();
  mySort.bubbleSort(array);
  System.out.print("冒泡排序结果 :  ");
  mySort.printArray(array);
  mySort.qsort(array);
  System.out.println();
  System.out.print("快速排序结果 :  ");
  mySort.printArray(array);
  mySort.shellSort(array);
  System.out.println();
  System.out.print("希尔排序结果 :  ");
  mySort.printArray(array);
  mySort.selectSort(array);
  System.out.println();
  System.out.print("选择排序结果 :  ");
  mySort.printArray(array);
 }

 
 public void insertSort(int[] array){
  int temp=0; 
     for(int i=1;i        int j=i-1; 
        temp=array[i]; 
        for(;j>=0&&temp         array[j+1]=array[j];                       //将大于temp的值整体后移一个单位 
        } 
        array[j+1]=temp; 
     }
 }

 
 public void bubbleSort(int[] array) {
     int temp;
     for(int i=0;i       for(int j=0;j         if(array[j]>array[j+1]){
           temp=array[j];
           array[j]=array[j+1];
           array[j+1]=temp;
         }
       }
     }
    }

 
 public void qsort(int array[]){
  if(array.length>1){
   _qsort(array,0,array.length-1);
  }
 }
 
 private void _qsort(int[] array,int low,int high){
  if(low < high){
   int middle = getMiddle(array, low, high);
   _qsort(array,low,middle-1);
   _qsort(array, middle+1, high);
  }
 }
 
 private int getMiddle(int[] array,int low,int high){
  int tmp = array[low];
  while(low < high){
   while(low < high && array[high] >= tmp)
    high--;
   array[low] = array[high];
   while(low    low++;
   array[high] = array[low];
  }
  array[low] = tmp;
  return low;
 }

 
 public void selectSort(int[] array){
  int position=0; 
        for(int i=0;i
            int j=i+1; 
            position=i; 
            int temp=array[i]; 
            for(;j            if(array[j]                temp=array[j]; 
                position=j; 
            } 
            } 
            array[position]=array[i]; 
            array[i]=temp; 
        }
 }

 
 public  void shellSort(int[] array){ 
     double d1=array.length; 
     int temp=0; 
     while(true){ 
         d1= Math.ceil(d1/2); 
         int d=(int) d1; 
         for(int x=0;x             for(int i=x+d;i                 int j=i-d; 
                 temp=array[i]; 
                 for(;j>=0&&temp                  array[j+d]=array[j]; 
                 } 
                 array[j+d]=temp; 
             } 
         } 
         if(d==1) 
             break; 
     } 
 } 

 

 public void printArray(int[] array){
  for (int i = 0; i < array.length; i++) {
   System.out.print(array[i]+" ");
  }
 }
}


下面是几种排序方法分开使用的示例

利用Arrays带有的排序方法快速排序
复制代码 代码如下:
import java.util.Arrays;
public class Test2{
        public static void main(String[] args){
                int[] a={5,4,2,4,9,1};
                Arrays.sort(a);  //进行排序
                for(int i: a){
                        System.out.print(i);
                }
        }
}

冒泡排序算法
复制代码 代码如下:
public static int[] bubbleSort(int[] args){//冒泡排序算法
                for(int i=0;i                        for(int j=i+1;j                                if (args[i]>args[j]){
                                        int temp=args[i];
                                        args[i]=args[j];
                                        args[j]=temp;
                                }
                        }
                }
                return args;
        }

选择排序算法
复制代码 代码如下:
public static int[] selectSort(int[] args){//选择排序算法
                for (int i=0;i                        int min=i;
                        for (int j=i+1;j                                if (args[min]>args[j]){
                                        min=j;
                                }
                        }
                        if (min!=i){
                        int temp=args[i];
                        args[i]=args[min];
                        args[min]=temp;       
                        }
                }
                return args;
        }

插入排序算法
复制代码 代码如下:
public static int[] insertSort(int[] args){//插入排序算法
                for(int i=1;i                        for(int j=i;j>0;j--){
                                if (args[j]                                        int temp=args[j-1];
                                        args[j-1]=args[j];
                                        args[j]=temp;       
                                }else break;
                        }
                }
                return args;
        }

以上就是java中的四种排序方法。不同的方法效率不一样,下面是不同的算法的比较和数据交换时的大O表示。

冒泡排序:比较O(N2) 数据交换O(N2)

选择排序:比较O(N2) 数据交换O(N)

插入排序:比较O(N2) 复制数据O(N)

在实际应用中,我们要尽量选择效率高的算法。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/152356.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号