1.排序
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200318135949361.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2x5MDcyNG9r,size_16,color_FFFFFF,t_70)
1.冒泡排序
package sort;
public class BubbleSort
{
private static int[] bubbleSort(int[] a)
{
int len = a.length;
for (int i = 1; i < len - 1; i++)
{
for (int j = 1; j < len - 1-i; j++)
{
if (a[j + 1] < a[j])
{
swap(a, j + 1, j);
}
}
}
return a;
private static void swap(int[] a, int i, int j)
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
public static void main(String[] args)
{
int[] a = {1, 4, 6, 8, 99, 9, 2, 99};
int[] sort = bubbleSort(a);
for (int s : sort)
{
System.out.print(s + " ");
}
}
}
2.快速排序
package sort;
public class QuickSort
{
private static int[] quickSort(int[] a, int low, int high)
{
int mid = 0;
if (low < high)
{
id = partition(a, low, high);
quickSort(a, low, mid - 1);
quickSort(a, mid + 1, high);
}
return a;
}
private static int partition(int[] a, int low, int high)
{
int b = a[low];
while (low < high)
{
while (low < high && a[high] >= b)
{
high--;
}
a[low] = a[high];
while (low < high && a[low] <= b)
{
low++;
}
a[high] = a[low];
}
a[low] = b;
return low;
}
public static void main(String[] args)
{
int[] a = {1, 14, 6, 8, 99, 9, 2, 99};
int[] sort = quickSort(a, 0, 7);
for (int s : sort)
{
System.out.print(s + " ");
}
}
}