Skip to content

Commit

Permalink
Update 七大排序.md
Browse files Browse the repository at this point in the history
  • Loading branch information
dackh authored Apr 13, 2020
1 parent 933e7d5 commit 7840b0e
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions 七大排序.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,36 @@
- 数据移动是最少的:只需N次交换
- 不稳定,例如:(2,2,5,4,7,8,1)

```
import java.util.*;
public class Selection
{
public static void main(String[] args){
int[] arr = new int[]{1,6,43,2,3,65,98,54,2,8,0,9};
sort(arr);
System.out.println(Arrays.toString(arr));
}
import java.util.*;
public class Selection
{
public static void main(String[] args){
int[] arr = new int[]{1,6,43,2,3,65,98,54,2,8,0,9};
sort(arr);
System.out.println(Arrays.toString(arr));
}

public static void sort(int[] arr){

for(int i = 0; i < arr.length;i++){
int min = i;
for(int j = i;j<arr.length;j++){
if(arr[j] < arr[min]){
min = j;
}
}
exch(arr,i,min);
}
}

public static void exch(int[] arr,int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
public static void sort(int[] arr){
for(int i = 0; i < arr.length;i++){
int min = i;
for(int j = i;j<arr.length;j++){
if(arr[j] < arr[min]){
min = j;
}
}
exch(arr,i,min);
}
}
public static void exch(int[] arr,int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
```
# **冒泡排序**
从左到右不断交换相邻逆序的元素,在一轮的循环之后,可以让未排序的最大元素上浮到右侧。

Expand Down

0 comments on commit 7840b0e

Please sign in to comment.