zoukankan
html css js c++ java
常用排序方法CommonSort
public
class
CommonSort
{
int
[] srcArray
=
new
int
[]
{
49
,
38
,
65
,
97
,
76
,
13
,
0
,
-
1
,
65464
,
4654
,
575
,
4
,
46
,
45654
,
45654
,
45
,
-
100
,
6456
,
45654
,
45654
,
45645
,
64
,
27
,
49
,
2423
,
456
,
465
,
5765
,
5756
,
5756
,
567
,
5
,
2342
,
234245
,
43
,
45
}
;
int
[] sortArray;
int
[] getArray;
int
[] tempArray;
long
beginTime;
long
endTime;
long
spendTime;
public
int
[] InsertSort(
int
[] sa)
{
System.out.println(
"
++InsertSort++
"
);
//
startsort(sa);
int
length
=
sa.length;
tempArray
=
null
;
tempArray
=
new
int
[length];
System.arraycopy(sa,
0
, tempArray,
0
, length);
sortArray
=
null
;
sortArray
=
new
int
[length
+
1
];
//
sortArray[0]为监视窗,交换用
int
j;
for
(
int
i
=
0
;i
<
length;i
++
)
{
sortArray[
0
]
=
tempArray[i];
j
=
i;
while
(j
>
0
&&
sortArray[
0
]
<
sortArray[j])
//
比小
{
sortArray[j
+
1
]
=
sortArray[j];
//
后移一位
j
--
;
}
sortArray[j
+
1
]
=
sortArray[
0
];
//
插入
}
System.arraycopy(sortArray,
1
, tempArray,
0
, length);
//
endsort(sa);
return
tempArray;
}
public
int
[] SelectSort(
int
[] sa)
{
System.out.println(
"
++SelectSort++
"
);
//
startsort(sa);
int
length
=
sa.length;
tempArray
=
null
;
tempArray
=
new
int
[length];
System.arraycopy(sa,
0
, tempArray,
0
, length);
int
i
=
0
;
int
j
=
0
;
int
k
=
0
;
for
(i
=
0
;i
<
length
-
1
;i
++
)
{
k
=
i;
for
(j
=
i
+
1
;j
<
length;j
++
)
{
if
(tempArray[j]
<
tempArray[k])
//
选最小的k
k
=
j;
}
if
(k
!=
i)
swap(tempArray,i,k);
}
//
endsort(sa);
return
tempArray;
}
public
int
[] BubbleSort(
int
[] sa)
{
System.out.println(
"
++BubbleSort++
"
);
//
startsort(sa);
int
length
=
sa.length;
tempArray
=
null
;
tempArray
=
new
int
[length];
System.arraycopy(sa,
0
, tempArray,
0
, length);
for
(
int
i
=
0
;i
<
length;i
++
)
{
for
(
int
j
=
length
-
1
;j
>
0
;j
--
)
{
if
(tempArray[j]
<
tempArray[j
-
1
])
//
逐个比
swap(tempArray,j,j
-
1
);
}
}
//
endsort(sa);
return
tempArray;
}
public
int
[] QuickSort(
int
low,
int
high,
int
[] sa)
{
System.out.println(
"
++QuickSort++
"
);
//
startsort(sa);
int
length
=
sa.length;
tempArray
=
null
;
tempArray
=
new
int
[length];
System.arraycopy(sa,
0
, tempArray,
0
, length);
int
pivot;
if
(low
<
high)
{
pivot
=
partition(low, high,tempArray);
QuickSort(low, pivot
-
1
,tempArray);
QuickSort(pivot
+
1
, high,tempArray);
}
//
endsort(sa);
return
tempArray;
}
public
int
partition(
int
low,
int
high,
int
sa[])
{
int
pivot, p_pos, i;
p_pos
=
low;
pivot
=
sa[p_pos];
for
(i
=
low
+
1
; i
<=
high; i
++
)
{
if
(sa[i]
<
pivot)
{
p_pos
++
;
swap(sa, p_pos, i);
}
}
swap(sa, low, p_pos);
return
p_pos;
}
public
int
[] HeapSort(
int
[] sa)
{
System.out.println(
"
++HeapSort++
"
);
int
length
=
sa.length;
tempArray
=
null
;
tempArray
=
new
int
[length];
System.arraycopy(sa,
0
, tempArray,
0
, length);
//
startsort(sa);
//
endsort(sa);
return
tempArray;
}
public
int
[] MeregSort(
int
low,
int
high,
int
[] sa)
{
System.out.println(
"
++MeregSort++
"
);
//
startsort(sa);
int
length
=
sa.length;
tempArray
=
null
;
tempArray
=
new
int
[length];
System.arraycopy(sa,
0
, tempArray,
0
, length);
int
mid;
if
(low
<
high)
{
mid
=
(low
+
high)
/
2
;
//
求中值
MeregSort(low,mid,tempArray);
//
递归合并
MeregSort(mid
+
1
,high,tempArray);
//
递归合并
Mereg(low,mid,high,tempArray);
//
合并
}
//
endsort(sa);
return
tempArray;
}
public
void
Mereg(
int
low,
int
mid,
int
high,
int
[]sa)
{
int
length
=
sa.length;
sortArray
=
null
;
sortArray
=
new
int
[length
+
1
];
int
h,i,j,k;
h
=
low;
i
=
low;
j
=
mid
+
1
;
while
((h
<=
mid)
&&
(j
<=
high))
{
if
(sa[h]
<=
sa[j])
{
sortArray[i]
=
sa[h];
h
=
h
+
1
;
}
else
{
sortArray[i]
=
sa[j];
j
=
j
+
1
;
}
i
++
;
}
if
(h
>
mid)
{
for
(k
=
j;k
<=
high;k
++
)
{
sortArray[i]
=
sa[k];
i
++
;
}
}
else
{
for
(k
=
h;k
<=
mid;k
++
)
{
sortArray[i]
=
sa[k];
i
++
;
}
}
for
(k
=
low;k
<=
high;k
++
)
sa[k]
=
sortArray[k];
}
public
void
printArray(
int
[] pa)
{
int
length
=
pa.length;
for
(
int
i
=
0
;i
<
length;i
++
)
{
System.out.println(
"
array[
"
+
i
+
"
]
"
+
pa[i]);
}
System.out.println(
"
length:
"
+
length
+
"
:----
"
);
}
public
void
startsort(
int
[] sa)
{
System.out.println(
"
--------------
"
);
printArray(sa);
beginTime
=
System.currentTimeMillis();
System.out.println(
"
starttime:
"
+
beginTime);
}
public
void
endsort(
int
[] sa)
{
printArray(sa);
endTime
=
System.currentTimeMillis();
System.out.println(
"
endtime:
"
+
endTime);
spendTime
=
endTime
-
beginTime;
System.out.println(
"
spendtime:
"
+
spendTime
+
"
:-------
"
);
System.out.println();
}
public
int
[] swap(
int
[] sa,
int
i,
int
j)
{
int
tmp
=
sa[i];
sa[i]
=
sa[j];
sa[j]
=
tmp;
return
sa;
}
public
CommonSort()
{
getArray
=
null
;
startsort(srcArray);
getArray
=
InsertSort(srcArray);
endsort(getArray);
startsort(srcArray);
getArray
=
SelectSort(srcArray);
endsort(getArray);
startsort(srcArray);
getArray
=
BubbleSort(srcArray);
endsort(getArray);
startsort(srcArray);
getArray
=
MeregSort(
0
,srcArray.length
-
1
,srcArray);
endsort(getArray);
startsort(srcArray);
getArray
=
QuickSort(
0
,srcArray.length
-
1
,srcArray);
endsort(getArray);
}
public
static
void
main(String[] args)
{
new
CommonSort();
}
}
查看全文
相关阅读:
jquery easyui-datagrid手动增加删除重置行
jsp中一行多条数据情况
JQuery操作下拉框
解决juqery easyui combobox只能选择问题
oracle中WMSYS.WM_CONCAT函数的版本差异
oracle wm_concat(column)函数的使用
Javascript九大排序算法详解
C#和VB新版本的最新特性列表
Oracle中如何区别用户和模式
远程控制数据库实用SQL重启功能
原文地址:https://www.cnblogs.com/bluespot/p/877329.html
最新文章
2017年12月23日记
H3C交换机配置的备份与恢复(转)
关于IBM小机取诊断日志方法
windows 如何查看磁盘序号(转)
sun阵列常见日志信息整理
db2表空间自动扩展
学习计划
vmware学习笔记-草稿
Python 学习课堂
Oracle 11g 体系结构--数据字典
热门文章
Oracle 11g 体系结构 --SGA PGA 前后台进程
Linux-文本编辑器
Linux -入门学习
Oracle 11g 体系结构概述
Oracle 11g 概述
linux中shell变量$#,$@,$0,$1,$2的含义解释
ORACLE获取某个时间段之间的月份列表和日期列表
ORACLE游标概念讲解
Oracle 游标使用全解
Oracle中如何插入特殊字符:& 和 ' (多种解决方案)
Copyright © 2011-2022 走看看