-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
35 lines (32 loc) · 861 Bytes
/
Copy pathSelectionSort.java
File metadata and controls
35 lines (32 loc) · 861 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.sort;
/**
* @author cx
* @date 2018/8/29 9:43
* @description 选择排序O(n^2) 从i=0开始每次取后面最小的放在i上面
*/
public class SelectionSort {
public static void selectionSort(int arr[]){
int temp=0;
for (int i = 0; i < arr.length-1; i++) {
temp=i;
for (int j = i+1; j < arr.length; j++) {
if (arr[j]<arr[temp]){
temp=j;
}
}
swap(arr,i,temp);
}
}
public static void swap(int arr[],int a,int b){
int temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
public static void main(String[] args) {
int arr[]={1,3,2,45,65,33,12};
selectionSort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}