本文介绍插入排序和快速排序算法的java实现代码。
1
package test;
2
import java.util.*;
3
4
/**
5
* @author HaoXiangShan
6
* 插入排序算法实现
7
*
8
*/
9
public class InsertSort {
10
11
ArrayList al;
12
13
/**
14
*
15
* 随机生成待排序数据
16
* @param num
17
* @param mod
18
*/
19
public InsertSort(int num, int mod){
20
al=new ArrayList();
21
Random rand=new Random();
22
for(int i=0;i<num;i++){
23
al.add(new Integer(Math.abs(rand.nextInt())%mod+1));
24
}
25
System.out.println(al.toString());
26
}
27
28
/**
29
*
30
* 对数列进行插入排序
31
*/
32
public void Sort(){
33
Integer tempInt;
34
for(int i=1;i<al.size();i++){
35
// i位置的元素在每一次循环之后都是0..i中最大的,而且这i+1个元素按从小到大的有序排列
36
tempInt=(Integer)al.get(i);
37
if(tempInt.intValue()<((Integer)al.get(i-1)).intValue())
38
{
39
al.remove(i);
40
for(int j=0;j<i;j++){
41
if(((Integer)al.get(j)).intValue()>=tempInt.intValue()){
42
al.add(j,tempInt);
43
break;
44
}
45
}
46
}
47
}
48
System.out.println(al.toString());
49
}
50
51
/**
52
* @param args
53
*/
54
public static void main(String[] args) {
55
InsertSort s1=new InsertSort(10,100);
56
s1.Sort();
57
}
58
59
}
60

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

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

1
package test;
2
3
import java.util.ArrayList;
4
import java.util.Random;
5
6
/**
7
* @author HaoXiangShan
8
* 快速排序算法实现
9
*
10
*/
11
public class QuickSort {
12
13
private static final int CUTOFF=2;//当元素数大于这个CUTOFF时,使用快速排序
14
private Comparable[] obj;
15
Comparable tmp=null;
16
17
/**
18
*
19
* 随机生成待排序数据
20
* @param num
21
* @param mod
22
*/
23
public QuickSort(int num, int mod){
24
obj=new Integer[num];
25
Random rand=new Random();
26
for(int i=0;i<num;i++){
27
obj[i]=new Integer(Math.abs(rand.nextInt())%mod+1);
28
System.out.print(obj[i]+" ");
29
}
30
System.out.println();
31
}
32
33
/**
34
* 调用quickSort方法实现快速排序
35
*/
36
public void sort(){
37
if (obj == null)
38
{
39
throw new NullPointerException("The argument can not be null!");
40
}
41
42
quickSort(obj, 0, obj.length - 1);
43
44
for(int i=0;i<obj.length;i++){
45
System.out.print(obj[i]+" ");
46
}
47
System.out.println();
48
}
49
50
/**
51
* @param obj
52
* @param left
53
* @param right
54
*/
55
private void quickSort(Comparable[] obj, int left, int right) {
56
if(left+CUTOFF<=right){
57
pivot(obj,left,right);
58
int i=left,j=right-1;
59
while(true){
60
while(obj[++i].compareTo(obj[right-1])<0) {};
61
while(obj[--j].compareTo(obj[right-1])>0) {};
62
63
if(i<j)
64
{
65
tmp=obj[i];
66
obj[i]=obj[j];
67
obj[j]=tmp;
68
}
69
70
else
71
{
72
break;
73
}
74
}
75
76
// 将枢纽值与i指向的值交换
77
tmp=obj[i];
78
obj[i]=obj[right-1];
79
obj[right-1]=tmp;
80
81
////对枢纽值左侧和右侧数组继续进行快速排序
82
quickSort(obj,left,i-1);
83
quickSort(obj,i+1,right);
84
}else{
85
if(obj[left].compareTo(obj[right])>0){
86
tmp=obj[left];
87
obj[left]=obj[right];
88
obj[right]=tmp;
89
}
90
}
91
92
}
93
94
/**
95
* 在数组obj中选取枢纽元,选取方法为取数组第一个、中间一个、最后一个元素中中间的一个。
96
* 将枢纽元置于倒数第二个位置,三个中最大的放在数组最后一个位置,最小的放在第一个位置
97
* @param obj
98
* @param left
99
* @param right
100
*/
101
private void pivot(Comparable[] obj, int left, int right) {
102
int center=(left+right)/2;
103
104
if(obj[left].compareTo(obj[center])>0){
105
tmp=obj[left];
106
obj[left]=obj[center];
107
obj[center]=tmp;
108
}
109
110
if(obj[left].compareTo(obj[right])>0){
111
tmp=obj[left];
112
obj[left]=obj[right];
113
obj[right]=tmp;
114
}
115
116
if(obj[center].compareTo(obj[right])>0){
117
tmp=obj[center];
118
obj[center]=obj[right];
119
obj[right]=tmp;
120
}
121
122
tmp=obj[center];
123
obj[center]=obj[right-1];
124
obj[right-1]=tmp;
125
126
// }
127
}
128
129
/**
130
* @param args
131
*/
132
public static void main(String[] args) {
133
QuickSort qs=new QuickSort(10,100);
134
qs.sort();
135
}
136
137
}
138

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

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138
