自己写的工具类,没办法,谁让咱是个懒人呢不过也算是为了练练静态导入吧
package com.tcgbp.tools;
import java.lang.reflect.Method;
import java.util.Random;
public class ToolMethods {
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
public static <E> void println(E e) {
System.out.println(e);
}
public static <E> void print(E e) {
System.out.print(e);
}
public static <E> void printn(E e,int n){
for(int i=0;i<n;i++)
System.out.print(e);
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/*
* 打印一维int数组,
*/
public static void printArray(int[] a) {
for (int i : a)
print(i + "\t");
println("");
}
/*
* 打印输出二维int数组
*/
public static void print2DArray(int[][] da) {
for (int[] a : da){
printArray(a);
}
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/*
* 插入排序,T不能为原生态类型
*/
public static <T extends Comparable<T>> void insertSort(T[] a) {
int in, out;
for (out = 1; out < a.length; out++) {
T temp = a[out];
in = out;
while (in > 0 && a[in - 1].compareTo(temp) >= 0) {
a[in] = a[in - 1];
--in;
}
a[in] = temp;
}
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/*
* 生成数组并随机初始化之
*/
public static int[] randomArray(int n) {
Random r = new Random();
int[] a = new int[n];
for (int i = 0; i < a.length; i++)
a[i] = r.nextInt(101);
return a;
}
/*
* 生成二维数组并随机初始化之
*/
public static int[][] random2DArray(int m, int n) {
int[][] a = new int[m][n];
for (int i = 0; i < m; i++) {
a[i] = randomArray(n);
}
return a;
}
/*
* 二维数组转一维
*/
public static int[] to1DArray(int[][] a){
int[] n = new int[a.length * a[0].length];
for(int i=0;i<a.length;i++){
for(int j=0;j<a[0].length;j++){
n[i * a[0].length +j] = a[i][j];
}
}
return n;
}
/*
* 一维数组转二维
* m 行数
* n 列数
*/
public static int[][] to2DArray(int[] a,int n){
int m = (a.length % n == 0)?(a.length / n) : (a.length / n + 1);
int[][] b = new int[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(i*n+j < a.length){
b[i][j] = a[i*n+j];
}
}
}
return b;
}
/*
* 使用另一个二维数组为二维数组赋值
* 要求两个数组有同样的维度
*/
public static void apply2DArray(int[][] t, int[][] f){
for(int i=0;i<f.length;i++){
for(int j=0;j<f[0].length;j++){
t[i][j] = f[i][j];
}
}
}
/*
* 使用另一个数组为数组赋值
*/
public static int[] applyArray(int[] t, int[] f, int...se) {
int[] mr = new int[2];
int j = 0;
for(int r:se){
mr[j] = r;
j++;
}
if(mr[1]==0)
mr[1] = Math.min(f.length-mr[0], t.length); //mr[1]为将复制元素的个数
int start = (se == null) ? 1 : mr[0];
int end = (se == null) ? f.length : mr[1] + start;
for (int i = 0; start < end; start++) {
t[i] = f[start];
i++;
}
return t;
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
public static void swap(int[] array, int index1, int index2) {
int temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
}
简单排序算法,其中insertNoDupsSort较复杂吧算是,是凌晨2点躺在床上了才想出来的
package com.tcgbp.Sort;
import static com.tcgbp.tools.ToolMethods.*;
import java.util.Arrays;
import java.util.Random;
public class ArraySort {
public static void main(String[] args) {
int arrayNum = 10;
println("冒泡排序");
int[] a = randomArray(arrayNum);
printArray(a);
println("");
bilateralBubbleSort(a);
printArray(a);
// println("转成二维数组");
// int[][] a2 = to2DArray(a,4);
// print2DArray(a2);
// println("");
//
println("选择排序");
int[] b = randomArray(arrayNum);
printArray(b);
println("");
selectSort(b);
printArray(b);
println("");
// println("插入排序");
// int[] c = randomArray(arrayNum);
// printArray(c);
// println("");
// // insertSort(c);
// println(Arrays.toString(c));
// println(Arrays.toString(insertNoDupsSort(c)));
// println("");
// println("二维数组");
// int[][] d = random2DArray(3,4);
// print2DArray(d);
// println("用newBubble2DArray()排序");
// print2DArray(newBubble2DArray(d));//it works
// println("用bubble2DArray()排序");
// bubble2DArray(d);
// print2DArray(d);
// to1DArray(d);
// println("");
// printArray(to1DArray(d));
}
/*
* 插入排序
*/
public static void insertSort(int[] a) {
int in, out;
for (out = 1; out < a.length; out++) {
int temp = a[out];
in = out;
while (in > 0 && a[in - 1] >= temp) {
a[in] = a[in - 1];
--in;
}
a[in] = temp;
}
}
/*
* 插入排序,并除去重复项
*/
static int[] insertNoDupsSort(int[] a) {
int in, out,rn= -1;
for (out = 1; out < a.length; out++) {
int temp = a[out];
int p = 0;
for (in = out; in > 0; in--) {
if (a[in - 1] == -1) {
break;
} else if (a[in - 1] == temp) {
a[in - 1] = -1;
rn++;
for (int i = in - 1; i > rn; i--) {
a[i] = a[i - 1];
}
a[rn] = -1;
break;
} else if (a[in - 1] > temp) {
a[in] = a[in - 1];
++p;
}
}
if (p > 0)
a[out - p] = temp;
}
int[] b = new int[a.length - rn -1];
return applyArray(b,a,rn+1);
}
/*
* 选择排序
*/
public static void selectSort(int[] a) {
int out, in, min;
for (out = 0; out < a.length - 1; out++) { // outer loop (forward)
min = out;
for (in = out + 1; in < a.length; in++) { // inner loop (forward)
if (a[in] < a[min]) {
min = in;
}
}
swap(a,min,out);
}
}
public static int[] newSelectSortArray(int[] a) {
int out, in, min;
int[] b = a;
for (out = 0; out < a.length - 1; out++) { // outer loop (forward)
min = out;
for (in = out + 1; in < a.length; in++) { // inner loop (forward)
if (b[in] < b[min]) {
min = in;
}
}
swap(a,min,out);
}
return b;
}
/*
* 冒泡排序
*/
public static void bubbleSort(int[] a) {
int out, in;
// outer loop (backward)
for (out = a.length - 1; out > 0; out--)
// inner loop (forward)
for (in = 0; in < out; in++)
if (a[in] > a[in + 1]) {
swap(a,in,in+1);
}
}
public static int[] newBubbleSortArray(int[] a) {
int out, in;
int[] b = a;
// outer loop (backward)
for (out = a.length - 1; out > 0; out--)
// inner loop (forward)
for (in = 0; in < out; in++)
if (b[in] > b[in + 1]) {
swap(b,in,in+1);
}
return b;
}
/*
* 双向冒泡排序
*/
public static void bilateralBubbleSort(int[] a){
int out, in, f=0;
for(out = a.length - 1;out>0 && f<out;out--){
for(in=f;in<out;in++){
if (a[in] > a[in + 1]) {
swap(a,in,in+1);
}
}
for(;in>f;in--){
if(a[in]<a[in-1]){
swap(a,in,in-1);
}
}
f++;
}
}
/*
* 二维数组排序
*/
static void bubble2DArray(int[][] a) {
apply2DArray(a,to2DArray(newBubbleSortArray(to1DArray(a)), a[0].length));
}
static int[][] newBubble2DArray(int[][] a) {
int[][] b = to2DArray(newBubbleSortArray(to1DArray(a)), a[0].length);
return b;
}
}
分享到:
相关推荐
数据结构-栈与队列,链表,递归,简单排序到高级排序的算法的详细笔记,本人根据视频学习进行的数据结构记录。适合入门算法学习初级篇
本书的目的是从抽象思维和问题求解的观点提供对数据结构的实用介绍,试图包含有关数据结构、算法分析及其Java实现的所有重要的细节。作者采用了独特的方法将数据结构分成说明和实现两部分,并充分利用了已有的数据...
学习建议:算法和数据结构的内容,用最简单的C语言描述会比较清楚,没有必要使用C++和Java的面向对象描述。面向对象编程在这里没啥用处,反而冲淡了学习主题。初学者,先学习Weiss的《数据结构与算法分析 C语言描述...
此书的算法部分也很精到 比算法导论更容易学习和入门 Sartaj Sahni《数据结构算法与应用 C++语言描述》全集 包含中英文图书 代码 习题答案 演示动画 都是我亲自从此书的官方网站下载并汇总的 绝对权威 算法和数据...
自己整理的一些资料,不需要积分,希望对大家有帮助。 里面包有如下的一些java资料 Ant使用指南.pdf ...Java数据结构和算法中文第二版.pdf Java网络高级编程.pdf jsp高级运用 spring2中文开发参考手册.chm a.txt
资源名称:Java学习进阶资源目录:【】cf1a0ddfaa1c0da11a9b01f1e5e35bbe【】java数据结构【】java笔记2【】Java算法大全(近100种算法打包)【】ThinkinJava【】张孝祥正在整理Java就业面试题大全【】新建文件夹...
也许对于数据结构的学习涉及的内容比较少,没有动态规划,图论也只是讲了很基础的东西,字符串中KMP弄的过于复杂(对比于acm)。但是瑕不掩瑜,对于绝大部分内容真的讲的超级清楚,完美的图解,就像单步调试一样,也许...
DataStucture-Algorithms My learning process of DataStructure ...身边现在有4本书《数据结构 (C语言版)》、《数据结构与算法分析 Java语言描述第二版》、《Algorithms 4th edition》、 《算法设计与分析基础》。
数据结构 稀疏数组 二维数组 转 稀疏数组的思路 遍历 原始的二维数组,得到有效数据的个数 sum 根据sum 就可以创建 稀疏数组 sparseArr int[sum + 1] [3] 将二维数组的有效数据数据存入到 稀疏数组 稀疏数组转原始的...
FrameWork [java] 结构,框架 ['freimwә:k] Generic [java] 泛型 [dʒi'nerik] goto (保留字) 跳转 heap n.堆 [hi:p] implements (关键字) 实现 ['implimәnt] import (关键字) 引入(进口,输入) Info n.信息 ...
Structure /src/main/java ... ├ effective_java Effective Java 中文第二版 ├ jvm 深入理解Java虚拟机:JVM高级特性与最佳实践 ├ lambda JAVA 8实战 ├ netty Netty权威指南 ├ oop ├ recursion
本代码仓库是我学习“算法与数据结构”的过程中,对于一些经典算法和数据结构的实现,同时我还写了 Java 版本。 文章标题 文章链接 Python 代码链接 Java 代码链接 【算法日积月累】0-写在前面的话 第 1 部分:排序...
第二章 用户和权限 一、 用户介绍 ORACLE用户是学习ORACLE数据库中的基础知识,下面就介绍下类系统常用的默认ORACLE用户: 1. sys用户:超级用户,完全是个SYSDBA(管理数据库的人)。拥有dba,sysdba,sysoper等角色或...
VS2005 ASP.NET本地化学习笔记&感受 在自定义Server Control中捆绑JS文件 Step by Step 深度解析Asp.Net2.0中的Callback机制 使用 Web 标准生成 ASP.NET 2.0 Web 站点 ASP.NET 2.0基于SQLSERVER 2005的aspnetdb.mdf...
算法学习 LeetCode LeetCode标签: 提前 特里树 结论: BIT /段树 结论: 结论: 单层堆叠 ... 结论:我们可以使用索引加快递归速度,如果我们希望所有元素都使用一... 《算法(第4算法)》一书的读书笔记,ISBN:97871152