首页 > 代码库 > 数组排序-冒泡排序-插入排序-选择排序-Java实现

数组排序-冒泡排序-插入排序-选择排序-Java实现

package org.lyk.entities;

public class SortHelper
{
    public static void BubbleSort(int[] array)
    {
        for(int i = array.length-1; i >= 1 ; i--)
        {
            for(int j = 0; j < i ; j++)
            {
                if(array[j] > array[j+1])
                {
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                }
            }
        }
    }
    public static void SelectionSort(int[] array)
    {
        for(int i = array.length-1;i >= 1; i--)
        {
            int j = 0;
            int position = 0;
            for(j = 0,position = 0; j <= i; j++)
            {
                if(array[j] > array[position])
                {
                    position = j;
                }
            }
            int temp = array[i];
            array[i] = array[position];
            array[position] = temp;
        }
    }
    
    public static void InsertionSort(int[] array)
    {
        for(int i = 0; i < array.length;i++)
        {
            for(int j = 0; j < i; j++)
            {
                if(array[j] > array[i])
                {
                    int temp = array[i];
                    for(int k = i-1; k >= j; k-- )
                    {
                        array[k+1] = array[k];
                    }
                    array[j] = temp;
                }
            }
        }
    }
}

 

数组排序-冒泡排序-插入排序-选择排序-Java实现