常见排序的java实现

常见排序的java实现

常见排序java实现

插入排序(二分插入排序)

希尔排序

快速排序(三数中值快排)

冒泡排序

选择排序

堆排序

归并排序

基数排序

计数排序

桶排序

睡眠排序

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

32publicclassSleepSort{

/**

* 方法名:main

* 说明:睡眠排序

*/

publicstaticvoidmain(String[] args){

int[] ints = {1,4,7,3,8,9,2,6,5};

SortThread[] sortThreads =newSortThread[ints.length];

for(inti =0; i < sortThreads.length; i++) {

sortThreads[i] =newSortThread(ints[i]);

}

for(inti =0; i < sortThreads.length; i++) {

sortThreads[i].start();

}

}

}

classSortThreadextendsThread{

intms =0;

publicSortThread(intms){

this.ms = ms;

}

publicvoidrun(){

try{

sleep(ms*10+10);

}catch(InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(ms);

}

}

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

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

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424/**

* 文件名:SortTest.java

* 时间:2014年11月5日下午6:05:13

* 作者:修维康

*/

packagechapter7;

importjava.util.Arrays;

/**

* 类名:SortTest 说明:各类排序分析详解

*/

publicclassSortTest{

/**

* 方法名:insertionSort 说明:插入排序 时间复杂度O(N^2)

*/

publicstatic>voidinsertionSort(

AnyType[] a) {

for(inti =1; i < a.length; i++) {

AnyType temp = a[i];

intj = i -1;

while(j >=0&& temp.compareTo(a[j]) <0) {

a[j +1] = a[j];

j--;

}

a[j +1] = temp;

}

}

/**

* 方法名:BInsertSort 说明:二分插入排序 时间复杂度O(N^2) 因为插入排序的前i - 1个元素是排好序的

* 所有将第i个元素插入到前面查到元素的时候 可以用二分查找

*/

publicstatic>voidBInsertSort(

AnyType[] a) {

for(inti =1; i < a.length; i++) {

intlow =0;

AnyType temp = a[i];

inthigh = i -1;

intposition =0;

// 二分找不到时 low 的位置是大于key的最小元素,high是小于key的最大元素

while(low <= high) {

intmid = (low + high) /2;

if(a[mid].compareTo(temp) <0)

low = mid +1;

else

high = mid -1;

}

position = high;

for(intj = i; j > position && j >0; j--)

a[j] = a[j -1];

a[position +1] = temp;

}

}

/**

* 方法名:shellSort 说明:希尔排序 时间复杂度(N^2) 利用增量序列 对用增量分组得到的序列进行插入排序。

*/

publicstatic>voidshellSort(

AnyType[] a) {

for(intgap = a.length /2; gap >0; gap /=2) {

for(inti =0; i < gap; i++) {

for(intj = i + gap; j < a.length; j += gap) {

AnyType temp = a[j];

if(temp.compareTo(a[j - gap]) <0) {

intk = j - gap;

while(k >=0&& a[k].compareTo(temp) >0) {

a[k + gap] = a[k];

k -= gap;

}

a[k + gap] = temp;

}

}

}

}

}

/**

* 方法名:bubbleSort 说明:冒泡排序 时间复杂度O(N^2)

*/

publicstatic>voidbubbleSort(

AnyType[] a) {

for(inti =0; i < a.length; i++)

for(intj = i +1; j < a.length; j++) {

if(a[i].compareTo(a[j]) >0) {

AnyType temp = a[i];

a[i] = a[j];

a[j] = temp;

}

}

}

/************ 堆排序 ***********************************/

publicstatic>voidbuildHeap(

AnyType[] array) {

for(inti = array.length /2; i >=0; i--)

shifDown(array, i, array.length);

}

/**

* 方法名:shifUp 说明:上滤,其实只用下滤就可以完成建堆,排序

*/

publicstatic>voidshifUp(

AnyType[] array,intvalPos) {

AnyType temp = array[valPos];

while(valPos >0&& array[(valPos -1) /2].compareTo(temp) <0) {

array[valPos] = array[(valPos -1) /2];

valPos = (valPos -1) /2;

}

array[valPos] = temp;

}

/**

* 方法名:shifDown 说明:下滤 注意数组越界

*/

publicstatic>voidshifDown(

AnyType[] array,intvalPos,intn) {

AnyType temp = array[valPos];

while(valPos *2+1< n) {

intchild = valPos *2+1;// 左儿子

if(child != n -1&& array[child].compareTo(array[child +1]) <0)

child++;

if(temp.compareTo(array[child]) <0)

array[valPos] = array[child];

else

break;

valPos = child;

}

array[valPos] = temp;

}

publicstatic>voidheapSort(

AnyType[] a) {

buildHeap(a);

for(inti = a.length -1; i >0; i--) {

AnyType temp = a[0];

a[0] = a[i];

a[i] = temp;

shifDown(a,0, i);

}

}

/**

* 方法名:mergeSort 说明:归并排序,JAVA中对泛型的排序用该排序,对基本类型的排序用快排

* 因为归并排序是比较次数最少的,java中对两个对象的比较 代价是昂贵的

*/

publicstatic>voidmergeSort(

AnyType[] a) {

AnyType[] tempArray = (AnyType[])newComparable[a.length];

mergeSort(a, tempArray,0, a.length -1);

}

privatestatic>voidmergeSort(

AnyType[] a, AnyType[] tempArray,intlow,inthigh) {

if(low < high) {

intmid = (low + high) /2;

mergeSort(a, tempArray, low, mid);

mergeSort(a, tempArray, mid +1, high);

merge(a, tempArray, low, mid +1, high);

}

}

privatestatic>voidmerge(

AnyType[] a, AnyType[] tempArray,intlowPos,inthighPos,

inthighEnd) {

intleftEnd = highPos -1;

inttemPos = lowPos;

intnumElements = highEnd - lowPos +1;

while(lowPos <= leftEnd && highPos <= highEnd) {

if(a[lowPos].compareTo(a[highPos]) <=0)

tempArray[temPos++] = a[lowPos++];

else

tempArray[temPos++] = a[highPos++];

}

while(lowPos <= leftEnd)

tempArray[temPos++] = a[lowPos++];

while(highPos <= highEnd)

tempArray[temPos++] = a[highPos++];

for(intq =0; q < numElements; q++, highEnd--)

a[highEnd] = tempArray[highEnd];

}

/**

* 方法名:QuickSort 说明:三数取中值快排 因为选第一个或者选最后一个可能会面对已经排好序的序列,那样会导致快排像冒泡一样

* 时间复杂度是O(N^2) 三数取中值减少这种情况

*/

publicstatic>voidquickSort(

AnyType[] a) {

quickSort(a,0, a.length -1);

}

privatestatic>voidquickSort(

AnyType[] a,intlow,inthigh) {

if(low < high) {

intkeyPos = partition(a, low, high);

quickSort(a, low, keyPos -1);

quickSort(a, keyPos +1, high);

}

}

/**

* 方法名:partition 说明:在原序列上进行划分,因为一个临时变量可以保存一个key,所以在key的原位置上可以插入

* 类似在key的位置上挖坑,在找到要移动的元素,挖该元素移到这个坑里,又留下另一个坑, 不断的进行下去 直到low==down。

*/

privatestatic>intpartition(

AnyType[] a,intlow,inthigh) {

AnyType key = median3(a, low, high);

while(low < high) {

while(low < high && a[high].compareTo(key) >=0)

--high;

a[low] = a[high];

while(low < high && a[low].compareTo(key) <=0)

++low;

a[high] = a[low];

}

a[low] = key;

returnlow;

}

publicstatic>AnyTypemedian3(

AnyType[] a,intlow,inthigh) {

intmid = (low + high) /2;

if(a[low].compareTo(a[mid]) >0) {

AnyType temp = a[low];

a[low] = a[mid];

a[mid] = temp;

}

if(a[mid].compareTo(a[high]) >0) {

AnyType temp = a[mid];

a[mid] = a[high];

a[high] = temp;

}

if(a[low].compareTo(a[mid]) >0) {

AnyType temp = a[low];

a[low] = a[mid];

a[mid] = temp;

}

AnyType tem = a[low];

a[low] = a[mid];

a[mid] = tem;

returna[low];

}

/**

* 方法名:selectSort 说明:选择排序O(N^2)的算法

*/

publicstatic>voidselectSort(

AnyType[] a) {

for(inti =0; i < a.length; i++) {

intj = selectMin(a, i);

if(i != j) {

AnyType temp = a[j];

a[j] = a[i];

a[i] = temp;

}

}

}

privatestatic>intselectMin(

AnyType[] a,intn) {

AnyType min = a[n];

intminPos = n;

;

for(inti = n +1; i < a.length; i++)

if(a[i].compareTo(min) <0) {

min = a[i];

minPos = i;

}

returnminPos;

}

/**

* 方法名:radixSort 说明:基数排序 参数是数组和该数组中的最多位数。 O(N)的算法,将数组中的数按照从低位到高位不断的分配,收集

* 基数排序里面需要用到对1位数的稳定排序,在这里我们用到了 计数排序 作为基数排序的子程序,即将其直接映射到0-9的桶里

*/

publicstaticvoidradixSort(Integer[] a,intdataNum){

int[][] array =newint[10][a.length +1];

for(inti =0; i <=9; i++)

array[i][0] =0;

intbit = dataNum;

while(dataNum-- >0) {

// 分配

for(inti =0; i < a.length; i++) {

intindex = ++array[getNum(a[i], bit - dataNum)][0];

array[getNum(a[i], bit - dataNum)][index] = a[i];

}

// 收集

for(inti =0, w =0; i <10; i++) {

for(intj =1; j <= array[i][0]; j++)

a[w++] = array[i][j];

array[i][0] =0;

}

}

}

/**

* 方法名:getNum 说明:返回数n的倒数第i位数

*/

publicstaticintgetNum(intn,inti){

intcount =0;

while(n !=0) {

intx = n %10;

if(++count == i)

returnx;

n /=10;

}

return0;

}

/**

* 方法名:countSort 说明:计数排序 O(N)的算法 空间换时间

*/

publicstaticint[] countSort(int[] a) {

int[] c =newint[findMax(a) +1];

int[] b =newint[a.length];

for(inti =0; i < c.length; i++)

c[i] =0;

for(inti =0; i < a.length; i++)

c[a[i]]++;

intcNum = c.length -1;

for(inti = a.length -1; i >=0; i--) {

while(cNum >=0&& c[(cNum)] ==0)

cNum--;

;

b[i] = cNum;

c[cNum]--;

}

returnb;

}

publicstaticintfindMax(int[] a){

intmax = a[0];

for(inti =1; i < a.length; i++) {

if(a[i] > max)

max = a[i];

}

returnmax;

}

/**

* 方法名:bucketSort 说明:桶排序 排0-999 数量不超过1000 桶排序的思想是

* 将数组里的数按照某种映射均匀的分布在n个桶里,每个桶里的数都在一定的范围内 在对每个桶进行排序 最后按照桶的范围在合并在一起

* 在这里直接按照0-99 100-199划分桶,最后直接合并就行

*/

publicstaticvoidbucketSort(Integer[] a){

Integer[][] bucket =newInteger[10][101];//定义

for(inti =0;i <10;i++)

bucket[i][0] =0;

for(inti =0; i < a.length;i++){

for(intj =0;j<10;j++){

if(a[i]>=100*j&&a[i] <=100*j+99){

intindex = ++bucket[j][0];

bucket[j][index] = a[i];

}

}

}

//利用快排对每个桶进行排序

for(inti =0;i <10;i++)

quickSort(bucket[i],1,bucket[i][0]);

//将10个桶合并

intw =0;

for(inti =0;i <10;i++){

for(intj =1;j <= bucket[i][0];j++)

a[w++] = bucket[i][j];

}

}

/**

* 方法名:main 说明:测试

*/

publicstaticvoidmain(String[] args){

// TODO Auto-generated method stub

Integer[] a =newInteger[] {10,9,8,7,6,5,4,3,2,1};

Integer[] b =newInteger[] {10,9,8,7,6,5,4,3,2,1};

Integer[] c =newInteger[] {10,9,8,7,6,5,4,3,2,1};

Integer[] d =newInteger[] {10,9,8,7,6,5,4,3,2,1};

Integer[] e =newInteger[] {991,122,333,1,44,588,656};

Integer[] f =newInteger[] {8,7,6,8,9,10,12,5,4};

Integer[] g =newInteger[] {10,9,8,7,6,5,4,3,2,1};

Integer[] h =newInteger[] {70,76,60,65,50,55,44,42,45,20,

103,103,10004};

int[] i =newint[] {10,9,8,7,6,5,4,3,2,1,1003,1003};

Integer[] k =newInteger[]{991,122,333,1,44,588,656};

insertionSort(a);

BInsertSort(b);

shellSort(c);

selectSort(d);

heapSort(e);

quickSort(f);

mergeSort(g);

radixSort(h,5);

i = countSort(i);

bucketSort(k);

System.out.println(Arrays.toString(a));

System.out.println(Arrays.toString(b));

System.out.println(Arrays.toString(c));

System.out.println(Arrays.toString(d));

System.out.println(Arrays.toString(e));

System.out.println(Arrays.toString(g));

System.out.println(Arrays.toString(f));

System.out.println(Arrays.toString(h));

System.out.println(Arrays.toString(i));

System.out.println(Arrays.toString(k));

}

}

闲的没事,测了下各个排序的速度。

随机填充了10W的Integer数组

对其进行各种排序

花费时间如下

插入排序:21148

二分插入排序:11808

希尔排序:76

选择排序:18919

堆排序:77

快速排序:257

归并排序:187

计数排序:90

桶排序:15

可以看到

因为排的是Integer 所以快排并没有归并排的快

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,524评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,869评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,813评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,210评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,085评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,117评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,533评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,219评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,487评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,582评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,362评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,218评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,589评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,899评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,176评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,503评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,707评论 2 335

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,712评论 0 33
  • 概述:排序有内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部...
    每天刷两次牙阅读 3,723评论 0 15
  • 1.插入排序—直接插入排序(Straight Insertion Sort) 基本思想: 将一个记录插入到已排序好...
    依依玖玥阅读 1,225评论 0 2
  • 某次二面时,面试官问起Js排序问题,吾绞尽脑汁回答了几种,深感算法有很大的问题,所以总计一下! 排序算法说明 (1...
    流浪的先知阅读 1,184评论 0 4
  • 老端的音频先听完,得做笔记 钱投资第2季
    格桑物理阅读 131评论 0 0