forked from littlecurl/AppProjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5810c58
commit 1e5340b
Showing
4 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cn.edu.heuet.I.basic; | ||
|
||
import java.util.ArrayList; | ||
import java.util.BitSet; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class BitSetTest { | ||
public static void main(String[] args) { | ||
// 准备一百万个范围在[0-1百万)的随机数 | ||
final int ONE_MILLION = 100 * 10000; | ||
Random random = new Random(); | ||
List<Integer> list = new ArrayList<>(); | ||
for (int i = 0; i < ONE_MILLION; i++) { | ||
int randomResult = random.nextInt(ONE_MILLION); | ||
list.add(randomResult); | ||
} | ||
// 进行位图排序 | ||
BitSet bitSet = new BitSet(ONE_MILLION); | ||
for (int i = 0; i < ONE_MILLION; i++) { | ||
// 将索引对应位设为true | ||
bitSet.set(list.get(i)); | ||
} | ||
// 测试 | ||
int[] test = new int[] {123,456,789,1023,4056,7089}; | ||
for(int i=0; i<test.length; i++) { | ||
if(!bitSet.get(test[i])) { | ||
System.out.println(test[i]+"已随机生成"); | ||
} else { | ||
System.out.println(test[i]+"未生成"); | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package cn.edu.heuet.I.basic; | ||
|
||
/** | ||
* 归并排序Java语言 对<a href= "https://blog.csdn.net/morewindows/article/details/6678165">《白话经典算法系列之五归并排序的实现》</a> | ||
* C 代码的改编版 | ||
* @author 刘同学 | ||
* | ||
*/ | ||
public class MergeSort { | ||
// 因为要在main方法中进行测试, | ||
// 所以merge()、recursion()和mergeSort()方法都加了static | ||
|
||
// merge()方法作用: | ||
// 合并两个<b>有序</b>的数列 | ||
// 归并排序是在“并”的时候进行排序的 | ||
private static void merge(int[] a, int left, int mid, int right, int[] temp) { | ||
int i = left; | ||
int m = mid; | ||
int j = mid + 1; | ||
int n = right; | ||
int k = 0; | ||
|
||
while (i <= m && j <= n) { | ||
// 依次找最小,将其放入temp数组 | ||
if (a[i] <= a[j]) { | ||
temp[k++] = a[i++]; | ||
} else { | ||
temp[k++] = a[j++]; | ||
} | ||
} | ||
// 如果左右不对称,比较之后还剩余 | ||
// 那么把剩余的依次放入temp数组 | ||
while (i <= m) { | ||
temp[k++] = a[i++]; | ||
} | ||
|
||
while (j <= n) { | ||
temp[k++] = a[j++]; | ||
} | ||
// 将temp数组中的数据拷贝回去a数组 | ||
for (i = 0; i < k; i++) { | ||
a[left + i] = temp[i]; | ||
} | ||
|
||
} | ||
|
||
// 递归recursion,合并merge | ||
private static void recursion(int[] a, int left, int right, int[] temp) { | ||
if (left < right) { | ||
int mid = (left + right) / 2; | ||
|
||
recursion(a, left, mid, temp); | ||
/** | ||
* 上面的递归最深层是 | ||
* recursion(a, 0, 0, temp); | ||
* recursion(a, 1, 1, temp); | ||
* merge(a, 0, 0, 1, temp); | ||
* | ||
* 将a[0]和a[1]作比较后按大小放入temp数组 | ||
*/ | ||
recursion(a, mid + 1, right, temp); | ||
/** | ||
* 上面的递归最深层是 | ||
* recursion(a, mid+1, mid+1, temp); | ||
* recursion(a, mid+2, mid+2, temp); | ||
* merge(a, mid+1, mid+1, mid+2, temp); | ||
* | ||
* 将a[mid+1]和a[mid+2]作比较后按大小放入temp数组 | ||
*/ | ||
merge(a, left, mid, right, temp); | ||
} | ||
} | ||
|
||
public static boolean mergeSort(int[] a, int length) { | ||
int[] temp = new int[length]; | ||
if (temp == null) | ||
return false; | ||
// 因为left传入0,所以right需要传入length-1 | ||
// 又因为merge()方法需要temp数组,所以需要加上temp | ||
recursion(a, 0, length - 1, temp); | ||
return true; | ||
} | ||
|
||
// 测试 | ||
public static void main(String[] args) { | ||
int[] a = new int[] { 4, 3, 6, 1, 2, 5 }; | ||
mergeSort(a, a.length); | ||
for (int i = 0; i < a.length; ++i) { | ||
System.out.print(a[i] + " "); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cn.edu.heuet.I.basic; | ||
|
||
public class QuickSort { | ||
|
||
public static void qSort(int[] arr, int left, int right) { | ||
if (left >= right || arr == null || arr.length <= 1) { | ||
return; | ||
} | ||
int i = left, j = right, mid = arr[(left + right) / 2]; | ||
while (i <= j) { | ||
while (arr[i] < mid) { | ||
++i; | ||
} | ||
while (arr[j] > mid) { | ||
--j; | ||
} | ||
if (i < j) { | ||
int t = arr[i]; | ||
arr[i] = arr[j]; | ||
arr[j] = t; | ||
++i; | ||
--j; | ||
} else if (i == j) { | ||
++i; | ||
} | ||
} | ||
qSort(arr, left, j); | ||
qSort(arr, i, right); | ||
} | ||
// 测试 | ||
public static void main(String[] args) { | ||
int[] arr = new int[] { 1, 4, 8, 2, 3, 3 }; | ||
qSort(arr, 0, arr.length - 1); | ||
String out = ""; | ||
for (int digit : arr) { | ||
out += (digit + ","); | ||
} | ||
System.out.println(out); | ||
} | ||
} |