-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertSort.java
More file actions
32 lines (30 loc) · 816 Bytes
/
Copy pathInsertSort.java
File metadata and controls
32 lines (30 loc) · 816 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
package com.sort;
/**
* @author cx
* @date 2018/8/30 15:53
* @description 直接插入排序O(n)~O(n^2) 从i=1开始取i上的数插入0~i数组中
*/
public class InsertSort {
public static void main(String[] args) {
int arr[]={1,3,2,45,65,33,12};
insertSort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
public static void insertSort(int arr[]){
for (int i = 1; i < arr.length; i++) {
int j;
int temp = arr[i];
for ( j = i-1; j >= 0&&arr[j] > temp; j--) {
arr[j+1]=arr[j];
}
arr[j+1] = temp;
}
}
public static void swap(int arr[],int a,int b){
int temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
}