几种排序方式的java实现(01:插入排序,冒泡排序,选择排序,快速排序)

以下为集中排序的java代码实现(部分是在引用别人代码):

插入排序(InsertSort):

//代码原理
public static void iSort(int[] a){
    for(int i = 1;i < a.length; i++){
        if(a[i] < a[i-1]){
            int temp = a[i];
            while(temp < a[i-1]){
                a[i] = a[i-1];
                if(i-1 > 0){
                    i--;
                }else{
                    break;
                }
            }
            a[i-1] = temp;
        }
    }
}
//精简代码1
public static void iSort2(int[] a){
        for(int i = 1;i < a.length; i++){
            if(a[i] < a[i-1]){
                int temp = a[i];
                while(temp < a[i-1]){
                    a[i] = a[i-1];
                    if(i-1 > 0){
                        i--;
                    }else{
                        break;
                    }
                }
                a[i-1] = temp;
            }
        }
    }
//精简代码2
for(current=1;current<=arr.length-1;current++){
    //每当current变化,key和before都随之变化
    key = arr[current];
    before = current-1;//红色有序索引第一个值。

    while(before>=0 && arr[before]>key){

        arr[before+1] = arr[before];//大值向后移一位
        before--;
    }

    //插入点:before+1
    arr[before+1] = key;
}

冒泡排序(BubbletSort):

public class BubbleSort {
    public static int[] bSort(int[] a){
        for(int j = a.length-1; j >= 1; j--){
//            如果flag没有变为false那么证明数组本身有序
            boolean flag = true;
            for(int i = 0; i <= j-1; i++){
                if(a[i] > a[i+1]){
                    int temp = a[i];
                    a[i+1] = a[1];
                    a[i] = temp;
                    flag = false;
                }
            }
            if(flag)
                break;
        }
        return a;
    }
}

选择排序(SelectionSort):

/*
 * 选择排序基本思路:
 * 把第一个元素依次和后面的所有元素进行比较。
 * 第一次结束后,就会有最小值出现在最前面。
 * 依次类推
 */
public class SelectionSort {
    public static void sort(int[] data) {
        for (int x = 0; x < data.length - 1; x++) {
            for (int y = x + 1; y < data.length; y++) {
                if (data[y] < data[x]) {
                    SortTest.swap(data, x, y);
                }
            }
        }
    }
}

选择排序(QuickSort):

/**
 * 快速排序
 * @author mly11
 *
 */
public class QuickSort {
    static int low = 0;
    static int high = 0;
    static int mid = 0;
    public static void main(String[] args) {
        int[] arr = {5,1,3,9,2,7,2,4};

//        开始的数组为
        System.out.println("开始的数组为:");
        for(int i = 0; i < arr.length; i++){
            System.out.print(arr[i]+"\t");
        }
        System.out.println();

//        排序
        judge(arr);

//        排序后遍历
        System.out.println("排序后数组为:");
        for(int i = 0; i < arr.length; i++){
            System.out.print(arr[i]+"\t");
        }
        System.out.println();
    }

//    判断数组是否为空
    public static void judge(int[] arr){
        if(arr.length > 0){
            all(arr,0,arr.length-1);
        }else{
            System.out.println("换个吧");;
        }
    }

//    循环条件,递归调用循环体
    public static void all(int[] arr,int low,int high){
        if(low < high){
//            用mid记录每一次选择的分界数字的角标,
            mid = structure(arr, low, high);

//            当low < mid -1的时候,从low到mid-1进行递归
            if(low < mid - 1){
                all(arr, low, mid-1);
            }

//            当high>mid+1时候,从mid+1到high递归
            if(high > mid +1){
                all(arr, mid+1, high);
            }
        }
    }

//    循环体   一次循环
    public static int structure(int[] arr,int low,int high){
//        当索引low小于high时,进行运算,让小于所选值在左边,否则在右边
/*        原理:
        取出a[low]的数据存入temp,使a[low]为空;开始循环:
        当low < high 时,一直循环
        while(low < high){
            当low从第一个开始时,从后向前一一查找,如果比temp大,则high--;
            否则交换a[low]和a[high],此时a[low]的值为a[high],a[high]为空;
            现在从前(a[low]被a[high]占据的位置)向后查找,如果比temp小,则low++;
            否则将a[low]的位置存入a[high](上一步为空的a[high]),此时a[low]为空;
        }
        a[low] = temp;
        返回low,此时low的位置之前数据全部<a[low],low之后的位置的数据全部>a[low];
*/

        int temp = arr[low];
        while (low < high) {
            while (low < high && arr[high] >= temp) {
                high--;
            }
            arr[low] = arr[high];
            while (low < high && arr[low] < temp) {
                low++;
            }
            arr[high] = arr[low];
        }
        arr[low] = temp;
        return low;    

/*错误的方法
    while(low < high){
            int temp = arr[low];
            while(arr[low] <= arr[high]){
                high--;
            }
            arr[low] = arr[high];
            low++;
            arr[high] = arr[low];
            arr[low] = temp;
        }
        return low;*/
    }

}

以上代码为自己在最开始接触的时候写的,不足之处请谅解。

时间: 2024-08-02 15:45:20

几种排序方式的java实现(01:插入排序,冒泡排序,选择排序,快速排序)的相关文章

C# 插入排序 冒泡排序 选择排序 高速排序 堆排序 归并排序 基数排序 希尔排序

以下列出了数据结构与算法的八种基本排序:插入排序 冒泡排序 选择排序 高速排序 堆排序 归并排序 基数排序 希尔排序,然后是測试的样例.代码位置:http://download.csdn.net/detail/luozuolincool/8040027 排序类: public class Sortings { //插入排序 public void insertSort(int[] array) { int temp = 0; int index = 0; for (int i = 0; i <

C# 插入排序 冒泡排序 选择排序 快速排序 堆排序 归并排序 基数排序 希尔排序

下面列出了数据结构与算法的八种基本排序:插入排序 冒泡排序 选择排序 快速排序 堆排序 归并排序 基数排序 希尔排序,然后是测试的例子.代码位置:http://download.csdn.net/detail/luozuolincool/8040027 排序类: public class Sortings { //插入排序 public void insertSort(int[] array) { int temp = 0; int index = 0; for (int i = 0; i <

java代码测试---插入排序和选择排序

1 public class QuickSort { 2 3 //插入排序 4 //插入前的序列是排序好的,将新插入的数值与之前的数值比较 5 //直到找到合适的位置 6 public static int[] quickSort(int[] arr){ 7 8 for(int j=1;j<arr.length;j++){ 9 int key = arr[j]; 10 int i = j-1; 11 12 while(i>=0 && arr[i]<key){ 13 arr

【算法】插入排序/冒泡排序/选择排序

插入排序 插入排序的思想为:从数组的第二个元素开始遍历整个数组.针对每个元素,依次将其前面的所有元素和他进行比较,大于它的元素均向后移动,最后将该元素插入. 插入排序是一种稳定的排序算法. 时间复杂度T(n)=O(n^2) 最好情况下已排好序,T(n)=O(n) private void swap(int[] a, int i, int j) { int temp = a[j]; a[j] = a[i]; a[i] = temp; } 插入排序的关键代码 private void insertS

插入排序+冒泡排序+选择排序

插入排序的工作机理和打牌时,整理手中的排做法差不多.在开始摸牌的是,我们的左手是空的,排名朝下放在桌上,接着,一次从桌上摸一张牌,并将它插入左手排的正确位置上.为了找到这张牌的正确位置,要将他和手中的没一张哦从右到左进行比较,无论什么时候,左手中的牌都是排好序的.(出自:算法导论) 例如:5,4,6,2,7,3,4,1 5->4,5->4,5,6->2,4,5,6->2,4,5,6,7-->2,3,4,5,6,7-->2,3,4,4,5,6,7-->1,2,3,4

九种经典排序算法详解(冒泡排序,插入排序,选择排序,快速排序,归并排序,堆排序,计数排序,桶排序,基数排序)

综述 最近复习了各种排序算法,记录了一下学习总结和心得,希望对大家能有所帮助.本文介绍了冒泡排序.插入排序.选择排序.快速排序.归并排序.堆排序.计数排序.桶排序.基数排序9种经典的排序算法.针对每种排序算法分析了算法的主要思路,每个算法都附上了伪代码和C++实现. 算法分类 原地排序(in-place):没有使用辅助数据结构来存储中间结果的排序**算法. 非原地排序(not-in-place / out-of-place):使用了辅助数据结构来存储中间结果的排序算法 稳定排序:数列值(key)

Java再学习-算法之选择排序

 继上篇文章讲到插入排序和冒泡排序算法.这次来看一下选择排序.         和上两个循环一样,还是分成两套循环,外循环起指针作用,用来指定每次循环的元素值和元素序列,而内部循环则起到真正的快速排序逻辑.首先如果我们取到第i值,那么我们要与第i+1,i+2,....等元素进行对比,找到i元素后面最小的元素,与之交换位置即可,只不过这里的交换位置比较新颖,我们看下面的代码: package cn.tgb.sort; import java.util.Arrays; //选择排序 public c

java结构与算法之选择排序

一 .java结构与算法之选择排序 什么事选择排序:从一组无序数据中选择出中小的的值,将该值与无序区的最左边的的值进行交换. 简单的解释:假设有这样一组数据 12,4,23,5,找到最小值 4 放在最右边,然后找到 5 放在  4 的后面,重复该操作. 选择排序参考代码: public class ChooseSort { int[] array = null; @Test public void testPopSort() { array = new int[5]; array[0] = 45

算法 排序lowB三人组 冒泡排序 选择排序 插入排序

参考博客:基于python的七种经典排序算法   [经典排序算法][集锦]     经典排序算法及python实现 首先明确,算法的实质 是 列表排序.具体就是操作的列表,将无序列表变成有序列表! 一.排序的基本概念和分类 所谓排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作.排序算法,就是如何使得记录按照要求排列的方法. 排序的稳定性: 经过某种排序后,如果两个记录序号同等,且两者在原无序记录中的先后秩序依然保持不变,则称所使用的排序方法是稳定的,反之是不稳定