您的位置:首页 > 编程语言 > Java开发

08-Little prince's trip to Java-数组排序

2018-11-18 15:44 162 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/WeiBlogProcedure/article/details/84194682

常用的排序方法有以下几种:

  • 1> 冒泡排序
  • 2> 选择排序
  • 3> 插入排序
  • 4> shell排序(希尔排序)

1.冒泡排序

  • 时间复杂度:O(n^2)
  • 空间复杂度:O(1)
  • 稳定性:稳定


执行过程如下:

第一次循环过程如下:

   如图:执行完第一次循环最大值 9 被换到了最后一位,剩余循环同第一次循环依次将较大值换到相应位置。

完整代码:

public class TestArray_BubbleSort {
public static void bubbleSort(int [] array) {
int temp = 0;
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int [] array = {6,4,8,2,3,9,1,7,5};
bubbleSort(array);
System.out.println(Arrays.toString(array));
}
}

运行结果:

冒泡排序的优化

  • 时间复杂度: O(n)

  定义一个计数器count,如果是一个有序数组进行第一次for循环之后进行判断count的真假。count为假,则跳出循环。

public class TestArray_BubbleSort {
public static void bubbleSort(int [] array) {
boolean count = false;
int temp = 0;
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
count = true;
}
if (!count ) {//加上该语句后冒泡排序的时间复杂度变为O(n)
break;
}
}
}
}
public static void main(String[] args) {
int [] array = {1,2,3,4,5,6,7,8,9};
bubbleSort(array);
System.out.println(Arrays.toString(array));
}
}

2.选择性排序

  • 时间复杂度:O(n^2)
  • 空间复杂度:O(1)
  • 稳定性:不稳定

执行过程如下:

第一次循环过程如下:

完整代码:

public class TestSortDemo {

public static void selectSort(int [] array) {
int temp = 0;
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i] > array[j]) {
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
}
public static void main(String[] args) {
int [] array = {12,4,3,6,1,7};
selectSort(array);
System.out.println(Arrays.toString(array));
}
}

运行结果:

3.直接插入排序

  • 时间复杂度:
      1> 无序:O(n^2)
      2> 有序:O(n)------越有序越快


执行过程:

执行一次循环过程:

  第一次循环较短不明显故此处列举第二次循环过程:

完整代码:

public class TestSortDemo {
public static void insertSort(int [] array) {
int temp = 0;
for (int i = 1; i < array.length; i++) {
temp = array[i];
int j = 0;
for (j = i - 1; j >= 0; j--) {
if (array[j] > temp) {
array[j + 1] = array[j];
}else {
break;
}
}
array[j + 1] = temp;
}
}
public static void main(String[] args) {
int [] array = {12,4,3,6,1,7};
insertSort(array);
System.out.println(Arrays.toString(array));
}
}

运行结果:

4.shell排序

  • 稳定性:不稳定
  • 采用分组思想,组内进行插入排序最后分为一组进行直接插入排序(越有序越快,此时大数字基本都在数组末尾)

注:因为shell排序用到了直接插入排序如果不注意条件,可能代码也可运行而且结果正确。


完整代码:

public class TestDemoShell {
public static void shellSort(int [] array) {
int drr[] = {5,3,1};
for (int i = 0; i < drr.length; i++) {
shell(array,drr[i]);
}
}
public static void shell(int [] array, int gap) {
for (int i = gap; i < array.length; i++) {
int temp = array[i];
int j = 0;
for (j = i - gap; j >= 0; j-=gap) {
if (array[j] > temp) {
array[j + gap] = array[j];
}else {
break;
}
}
array[j + gap] = temp;
}
}
public static void main(String[] args) {
int [] array = {7,4,9,34,0,8,5,22,55,6,12,33,56,89,77};
shellSort(array);
System.out.println(Arrays.toString(array));
}
}

运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: