import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.Random;
import static org.junit.Assert.*;
/**
* Created by corning on 2017/12/19.
*/
public class ArraySortTest {
private int[] array;
private int[] sortedArray;
// 计数排序等不支持负数排序
private int[] positiveArray;
private int[] positiveArraySorted;
@Before
public void setUp() throws Exception {
// 生成随机数组
array = randomArray(-1000, 1000, 100);
// 使用 Arrays.sort() 排序作为对比
sortedArray = Arrays.copyOf(array, array.length);
Arrays.sort(sortedArray);
positiveArray = randomArray(0, 1000, 100);
positiveArraySorted = Arrays.copyOf(positiveArray, positiveArray.length);
Arrays.sort(positiveArraySorted);
}
/**
* 随机指定范围内N个不重复的数
* 在初始化的无重复待选数组中随机产生一个数放入结果中,
* 将待选数组被随机到的数,用待选数组(len-1)下标对应的数替换
* 然后从len-2里随机产生下一个随机数,如此类推
*
* @param max 指定范围最大值
* @param min 指定范围最小值
* @param n 随机数个数
* @return int[] 随机数结果集
*/
public int[] randomArray(int min, int max, int n) {
int len = max - min + 1;
if (max < min || n > len) {
return null;
}
//初始化给定范围的待选数组
int[] source = new int[len];
for (int i = min; i < min + len; i++) {
source[i - min] = i;
}
int[] result = new int[n];
Random rd = new Random();
int index = 0;
for (int i = 0; i < result.length; i++) {
//待选数组0到(len-2)随机一个下标
index = Math.abs(rd.nextInt() % len--);
//将随机到的数放入结果集
result[i] = source[index];
//将待选数组中被随机到的数,用待选数组(len-1)下标对应的数替换
source[index] = source[len];
}
return result;
}
@After
public void tearDown() throws Exception {
array = null;
sortedArray = null;
}
@Test
public void bubbleSort() throws Exception {
assertArrayEquals(sortedArray, new BubbleSort().sort(array));
}
@Test
public void choiceSort() throws Exception {
assertArrayEquals(sortedArray, new SelectionSort().sort(array));
}
@Test
public void insertSort() throws Exception {
assertArrayEquals(sortedArray, new InsertSort().sort(array));
}
@Test
public void shellSort() throws Exception {
assertArrayEquals(sortedArray, new ShellSort().sort(array));
}
@Test
public void mergeSort() throws Exception {
assertArrayEquals(sortedArray, new MergeSort().sort(array));
}
@Test
public void mergeSort_merge() throws Exception {
assertArrayEquals(new int[]{1, 2}, new MergeSort().merge(new int[]{1, 2}, new int[]{}));
assertArrayEquals(new int[]{1, 2}, new MergeSort().merge(new int[]{1}, new int[]{2}));
assertArrayEquals(new int[]{1, 2, 3}, new MergeSort().merge(new int[]{1, 3}, new int[]{2}));
}
@Test
public void quickSort() throws Exception {
assertArrayEquals(sortedArray, new QuickSort().sort(array));
}
@Test
public void heapSort() throws Exception {
assertArrayEquals(sortedArray, new HeapSort().sort(array));
}
@Test
public void countingSort() throws Exception {
assertArrayEquals(positiveArraySorted, new CountingSort().sort(positiveArray));
}
@Test
public void bucketSort() throws Exception {
assertArrayEquals(sortedArray, new BucketSort().sort(array));
}
@Test
public void radixSort() throws Exception {
assertArrayEquals(sortedArray, new RadixSort().sort(array));
}
@Test
public void radixSort_getNumLenght() throws Exception {
assertEquals(3, new RadixSort().getNumLenght(-100));
assertEquals(1, new RadixSort().getNumLenght(1));
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
十大经典排序算法-多种编程语言
共37个文件
java:12个
md:10个
gif:8个
需积分: 3 1 下载量 2 浏览量
2022-12-14
19:32:15
上传
评论
收藏 2.98MB ZIP 举报
温馨提示
十大经典排序算法 (1)多种编程语言,JavaScript,python,go,php等语言。 (2)排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存。常见的内部排序算法有:插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序
资源推荐
资源详情
资源评论
收起资源包目录
多种语言的经典排序算法.zip (37个子文件)
多种语言的经典排序算法
8.countingSort.md 3KB
1.bubbleSort.md 4KB
6.quickSort.md 7KB
res
heapSort.gif 1.48MB
countingSort.gif 209KB
mergeSort.gif 326KB
radixSort.gif 149KB
bubbleSort.gif 343KB
quickSort.gif 270KB
insertionSort.gif 360KB
sort.png 25KB
selectionSort.gif 459KB
9.bucketSort.md 5KB
5.mergeSort.md 7KB
src
phpSortTest.php 7KB
goSortTest.go 4KB
java
pom.xml 2KB
target
sort-1.0-SNAPSHOT.jar 2KB
maven-archiver
pom.properties 107B
test
ArraySortTest.java 4KB
main
RadixSort.java 2KB
ShellSort.java 815B
CountingSort.java 1KB
MergeSort.java 1KB
QuickSort.java 1KB
IArraySort.java 278B
BubbleSort.java 910B
SelectionSort.java 893B
HeapSort.java 1KB
InsertSort.java 932B
BucketSort.java 2KB
pythonSortTest.py 5KB
4.shellSort.md 4KB
7.heapSort.md 5KB
3.insertionSort.md 3KB
10.radixSort.md 5KB
2.selectionSort.md 3KB
共 37 条
- 1
资源评论
倾城一少
- 粉丝: 667
- 资源: 62
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功