数组的排序,Java自带的Arrays类的sort方法主要内容包括插入排序结合分治排序两种排序算法在里面。
先分别介绍一下两种排序方式:
第一种:通过分治法实现的并归排序。
@SuppressWarnings
(
"unchecked"
)
public
static
void
mergeSort(Comparable[] src , Comparable[] dest,
int
low,
int
high) {
if
(high - low == 1) {
return
;
}
if
(high - low == 2) {
if
(dest[low].compareTo(dest[high - 1]) > 0) {
swap(dest, low, high - 1);
}
return
;
}
int
mid = (
low
+
high
) >> 1;
mergeSort(dest, src , low, mid);
mergeSort(dest, src , mid, high);
for
(
int
i = low, p = low, q = mid; i < high; i++) {
if
(q == high|| (p != mid && src [p].compareTo(src[q]) < 0)) {
dest[i] = src[p++];
}
else
{
dest[i] = src[q++];
}
}
}
|
第二种:插入排序。
private
static
void
InsertionSort(Comparable[] src,Comparable[] dest,
int
low,
int
high){
for
(
int
i=low;i<high;i++){
for
(
int
j=i;j>low;j--){
if
(dest[j].compareTo(dest[j-1])<0){
swap(dest, j, j-1);
}
}
}
}
|
还有一个工具方法
private
static
void
swap(Object[] x,
int
a,
int
b) {
Object t = x[a];
x[a] = x[b];
x[b] = t;
}
|
整个Java类的内容如下:
public
class
ArraysSort {
@SuppressWarnings
(
"unchecked"
)
public
static
void
mergeSort(Comparable[] src, Comparable[] dest,
int
low,
int
high) {
if
(high - low == 1) {
return
;
}
if
(high - low == 2) {
if
(dest[low].compareTo(dest[high - 1]) > 0) {
swap(dest, low, high - 1);
}
return
;
}
int
mid = (
low
+
high
) >> 1;
mergeSort(dest, src, low, mid);
mergeSort(dest, src, mid, high);
for
(
int
i = low, p = low, q = mid; i < high; i++) {
if
(q == high|| (p != mid && src[p].compareTo(src[q]) < 0)) {
dest[i] = src[p++];
}
else
{
dest[i] = src[q++];
}
}
}
@SuppressWarnings
({
"unused"
,
"unchecked"
})
private
static
void
InsertionSort(Comparable[] src, Comparable[] dest,
int
low,
int
high) {
for
(
int
i = low; i < high; i++) {
for
(
int
j = i; j > low; j--) {
if
(dest[j].compareTo(dest[j - 1]) < 0) {
swap(dest, j, j - 1);
}
}
}
}
private
static
String[] getStrings(
int
length) {
Random random =
new
Random();
String[] strs =
new
String[length];
for
(
int
i = 0; i < length; i++) {
int
temp = random.nextInt(999);
strs[i] = Integer. toString(temp);
}
return
strs;
}
private
static
void
swap (Object[] x,
int
a,
int
b) {
Object t = x[a];
x[a] = x[b];
x[b] = t;
}
public
static
void
main(String[] args) {
String[] strs = getStrings(10);
String[] aux = strs.clone();
long
begin = System.currentTimeMillis();
mergeSort(strs, aux, 0, strs.
length
);
System.
out
.println(System.currentTimeMillis() - begin);
System.
out
.println(Arrays.asList(strs));
System.
out
.println(Arrays.asList(aux));
}
}
|
将插入排序放入并归排序中:
public
class
ArraysSort {
/**
*
排序的代码
*
@param
src
*
@param
dest
*
@param
low
*
@param
high
*/
@SuppressWarnings
(
"unchecked"
)
public
static
void
mergeSort(Comparable[] src, Comparable[] dest,
int
low,
int
high) {
if
(high - low < 7) {
for
(
int
i = low; i < high; i++) {
for
(
int
j = i; j > low; j--) {
if
(dest[j].compareTo(dest[j - 1]) < 0) {
swap(dest, j, j - 1);
}
}
}
return
;
}
int
mid = (
low
+
high
) >> 1;
mergeSort(dest, src, low, mid);
mergeSort(dest, src, mid, high);
for
(
int
i = low, p = low, q = mid; i < high; i++) {
if
(q == high|| (p != mid && src[p].compareTo(src[q]) < 0)) {
dest[i] = src[p++];
}
else
{
dest[i] = src[q++];
}
}
}
/**
*
数组置换位置
*
@param
x
*
@param
a
*
@param
b
*/
private
static
void
swap(Object[] x,
int
a,
int
b) {
Object t = x[a];
x[a] = x[b];
x[b] = t;
}
/**
*
测试的方法
*
@param
length
*
@return
*/
private
static
String[] getStrings(
int
length) {
Random random =
new
Random();
String[] strs =
new
String[length];
for
(
int
i = 0; i < length; i++) {
int
temp = random.nextInt(999);
strs[i] = Integer. toString(temp);
}
return
strs;
}
public
static
void
main(String[] args) {
String[] strs = getStrings(10);
String[] aux = strs.clone();
long
begin = System.currentTimeMillis();
mergeSort(strs, aux, 0, strs.
length
);
System.
out
.println(System.currentTimeMillis() - begin);
System.
out
.println(Arrays.asList(strs));
System.
out
.println(Arrays.asList(aux));
}
}
|
Arrays的sort方法如下:
private
static
void
mergeSort(Object[] src,
Object[] dest,
int
low,
int
high,
int
off) {
int
length = high - low;
// Insertion sort on smallest arrays
if
(length <
INSERTIONSORT_THRESHOLD
) {
for
(
int
i=low; i<high; i++)
for
(
int
j=i; j>low &&
(( Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
swap(dest, j, j-1);
return
;
}
// Recursively sort halves of dest into src
int
destLow = low;
int
destHigh = high;
low += off;
high += off;
int
mid = (low + high) >> 1;
mergeSort(dest, src, low, mid, -off) ;
mergeSort(dest, src, mid, high, -off) ;
// If list is already sorted, just copy from src to dest. This is an
// optimization that results in faster sorts for nearly ordered lists.
if
(((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
System. arraycopy(src, low, dest, destLow, length);
return
;
}
// Merge sorted halves (now in src) into dest
for
(
int
i = destLow, p = low, q = mid; i < destHigh; i++) {
if
(q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
dest[i] = src[p++];
else
dest[i] = src[q++];
}
}
/**
*
Swaps
x[a]
with
x[b].
*/
private
static
void
swap(Object[] x,
int
a,
int
b) {
Object t = x[a];
x[a] = x[b];
x[b] = t;
}
|