01 经典排序算法
Posted alichengxuyuan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了01 经典排序算法相关的知识,希望对你有一定的参考价值。
1 冒泡排序
import java.util.Random;
public class BubbleSort
{
public static void main(String args[])
{
int[] arrays = BubbleSort.createArrays(10);
bubble(arrays);
for (int i = 0; i < arrays.length; i++)
{
System.out.print(arrays[i]+" ");
}
}
public static int[] createArrays(int length)
{
int[] arrays = new int[length];
Random rd = new Random();
for (int i = 0; i < arrays.length; i++)
{
arrays[i] = rd.nextInt(100);
}
return arrays;
}
private static void bubble(int[] arrays)
{
for (int i = 0; i < arrays.length - 1; i++)
{
for (int j = 0; j < arrays.length - i - 1; j++)
{
if(arrays[j] > arrays[j + 1])
{
int temp = arrays[j];
arrays[j] = arrays[j + 1];
arrays[j + 1] = temp;
}
}
}
}
}
2 选择排序
import java.util.Arrays;
import java.util.Random;
public class SelectSort
{
public static void main(String[] args)
{
int[] array = SelectSort.createArrays(10);
select(array);
System.out.println(Arrays.toString(array));
}
private static void select(int[] array)
{
int temp;
for (int i = 0; i < array.length - 1; i++)
{
for (int j = i + 1; j < array.length; j++)
{
if(array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
public static int[] createArrays(int length)
{
int[] arrays = new int[length];
Random rd = new Random();
for (int i = 0; i < arrays.length; i++)
{
arrays[i] = rd.nextInt(100);
}
return arrays;
}
}
3 插入排序
import java.util.Arrays;
import java.util.Random;
public class InsertSort
{
public static int[] createArrays(int length)
{
int[] arrays = new int[length];
Random rd = new Random();
for (int i = 0; i < arrays.length; i++)
{
arrays[i] = rd.nextInt(100);
}
return arrays;
}
public static void main(String[] args)
{
int[] arrays = InsertSort.createArrays(10);
insert(arrays);
System.out.println(Arrays.toString(arrays));
}
private static void insert(int[] arrays)
{
for (int i = 1; i < arrays.length; i++)
{
for (int j = i; j > 0; j--)
{
if(arrays[j] > arrays[j - 1])
{
int temp = arrays[j];
arrays[j] = arrays[j - 1];
arrays[j - 1] = temp;
}
}
}
}
}
4 快速排序
import java.util.Arrays;
import java.util.Random;
public class QuickSort
{
public static void main(String args[])
{
int[] array = createArrays(10);
quickSort(array, 0, array.length - 1);
System.out.println(Arrays.toString(array));
}
public static int[] createArrays(int length)
{
int[] arrays = new int[length];
Random rd = new Random();
for (int i = 0; i < arrays.length; i++)
{
arrays[i] = rd.nextInt(100);
}
return arrays;
}
public static void quickSort(int array[], int low, int high)
{
if(low < high)
{
int mid = getMid(array, low, high);
quickSort(array, low, mid - 1);
quickSort(array, mid + 1, high);
}
}
public static int getMid(int array[], int low, int high)
{
int temp = array[low];
while (low < high)
{
while (low < high && temp <= array[high])
{
high--;
}
array[low] = array[high];
while (array[low] <= temp && low < high)
{
low++;
}
array[high] = array[low];
}
array[low] = temp;
return low;
}
}
以上是关于01 经典排序算法的主要内容,如果未能解决你的问题,请参考以下文章