https://www.jb51.net/article/130936.htm
希尔排序:https://segmentfault.com/a/1190000013967025
冒泡思想:
它的基本思想是迭代地对输入序列的第一个元素到最后一个元素进行俩俩比较,当满足条件时交换这俩个元素的位置,只要比较n-1趟,每趟两两比较,会有1个最大或最新沉淀到尾部,每趟两两比较都会减少一次两两比较
《1》9排
/*
* 9种排序
交换
* (1)冒泡
* (2)快排
插入
* (3)直接插入排序
* (4)希尔排序
* (5)二分插入排序
选择
* (6)直接选择排序
* (7)堆排序
分法治
* (8)归并排序
* (9) 基数排序
*/
public class Sorts {
}
/*(1)冒泡
* public class bubbleSort {
public bubbleSort(){
int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
int temp=0;
for(int i=0;i<a.length-1;i++){
for(int j=0;j<a.length-1-i;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}
*
*
*
*(2)快排
*public class QuickSort {
/**
* 交换排序
* 快速排序
* 时间复杂度o(nlog2n) 空间复杂度o(nlog2n) 不稳定
*
public static void main(String[] args) {
int[] A=new int[]{745663,22, 93, 43, 55555, 14, 2888, 65, 39, 811};
quickSort(A,0, A.length-1);
for(int num:A)
{
System.out.println(num);
}
}
public static void quickSort(int[] arr,int low, int high){
if(low<high){
int mid=getMiddle(arr, low, high);
quickSort(arr, low, mid-1);
quickSort(arr, mid+1, high);
}
}
public static int getMiddle(int[] arr,int low, int high){
int temp=arr[low];
while(low<high){
while(low<high && arr[high]>temp){//注意,要先从高到低扫描
high--;
}
arr[low]=arr[high];
while(low<high && arr[low]<temp){
low++;
}
arr[high]=arr[low];
}
arr[low]=temp;
return low;
}
}
*
*
*
*(3)直接插入排序
*
*public class DirectInsertSort {
/**
* 插入排序一
*直接插入排序
* 描述:每次将一个待排序的元素与已排序的元素进行逐一比较,直到找到合适的位置按大小插入。
*时间复杂度 o(n*2) 空间复杂度O(1) 稳定
*
*
public static void main(String[] args) {
int[] A=new int[]{745663,22, 93, 43, 55555, 14, 2888, 65, 39, 811};
directInsertSort(A);
for(int num:A)
{
System.out.println(num);
}
}
public static void directInsertSort(int[] arr){
for(int i=1;i<arr.length;i++){
if(arr[i]<arr[i-1]){
int temp=arr[i];
int j=i-1;
while(j>=0 && arr[j]>temp){//---写错了,前面的应该大于后面的
arr[j+1]=arr[j];
j--;
}
// // 直到要插入的元素不小于第j个元素,将insertNote插入到数组中
arr[j+1]=temp;
}
}
}
}
*
*(4)希尔排序
*public class ShellSort {
/**
*插入排序二
*希尔排序
*把整个序列分成若干个子序列,分别进行直接插入排序。这算是“一趟希尔排序”
*时间复杂度 o(n*(1-3)) 复杂度o(1) 稳定
*
public static void main(String[] args) {
int[] A=new int[]{745663,22, 93, 43, 55555, 14, 2888, 65, 39, 811};
shellSort(A,2);
for(int num:A)
{
System.out.println(num);
}
}
public static void shellSort(int[] arr, int num){
for(int d=num; d>0; d/=2){//---此处是大于0,而不是大于等于0
for(int i=d; i<arr.length; i++){
if(arr[i]<arr[i-d]){
int temp=arr[i];
int j=i-d;
while(j>=0 && arr[j]>temp){
arr[j+d]=arr[j];
j=j-d;//----记住此处是减d,从下往上
}
arr[j+d]=temp;
}
}
}
}
}
*
*
*
*(5)二分插入排序
*public class MiddleInsertSort {
/**
* 插入排序
* 二分插入排序
* 时间复杂度 o(n*2) 空间复杂度 o(1) 不稳定
*
public static void main(String[] args) {
int[] A=new int[]{745663,22, 93, 43, 55555, 14, 2888, 65, 39, 811};
middleInsertSort(A);
for(int num:A)
{
System.out.println(num);
}
}
public static void middleInsertSort(int[] arr){
for(int i=1; i<arr.length; i++){
if(arr[i]<arr[i-1]){
int left=0;
int mid=0;
int right=i-1;//------注意右边是i-1, 始终想到0-i-1是有序的
int temp=arr[i];
while(left<=right){// ----此处要添加等于号
mid=(left+right)/2;
if(arr[mid]==temp){
left=mid;
break;
}else if(arr[mid]<temp){
left=mid+1;
}else{
right=mid-1;
}
}
// 待插入位置left,后移
for(int j=i-1;j>=left;j--){
arr[j+1]=arr[j];
}
arr[left]=temp;
}
}
}
}
(6)直接选择排序
public class DirectSelectSort {
/**
* 选择排序二
* 直接选择排序
* 时间复杂度o(n*2) 空间复杂度o(1) 稳定
*
*
public static void main(String[] args) {
int[] A=new int[]{745663,22, 93, 43, 55555, 14, 2888, 65, 39, 811};
direcInsertSort(A);
for(int num:A)
{
System.out.println(num);
}
}
public static void direcInsertSort(int[] arr){
for(int i=0; i<arr.length-1; i++){
int k=i;
for(int j=i+1; j<arr.length;j++){//-----把j写成i了,真想乎死自己
if(arr[j]<arr[k]){
k=j;
}
}
if(k!=i){
int temp=arr[k];
arr[k]=arr[i];
arr[i]=temp;
}
}
}
}
(7)堆排序
public class HeapSort{
/**选择排序之一
* 堆排序
* 时间复杂度 o(nlog2n) 空间复杂度o(1) 不稳定
*
public static void main(String[] args) {
int[] A=new int[]{745663,22, 93, 43, 55555, 14, 2888, 65, 39, 811};
heapSort(A);
for(int num:A)
{
System.out.println(num);
}
}
// 2、循环建堆
public static void heapSort(int[] arr){
for(int i=0; i<arr.length-1; i++){//-------忘记减1
buildHeap(arr, arr.length-1-i);
swap(arr, 0, arr.length-1-i);
}
}
// 1、建堆
public static void buildHeap(int[] arr, int lastIndex){
for(int i=(lastIndex-1)/2; i>=0; i--){
int k=i;
while(2*k+1<=lastIndex){
int big=2*k+1;
if(big<lastIndex){
if(arr[big]<arr[big+1]){//------敲成big++
big++;
}
}
// 父类与较大子节点比较
if(arr[k]<arr[big]){
swap(arr,k, big);
k=big;
}else{
break;
}
}
}
}
// 2、交换
public static void swap(int[] arr, int a, int b){
if(a<arr.length && b<arr.length){
int temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
}
}
(8)归并排序
public class MergeSort {
/**
* 归并排序
* 时间复杂度 o(nlog2n) 空间复杂度 o(n) 稳定
*
public static void main(String[] args) {
int[] A=new int[]{745663,22, 93, 43, 55555, 14, 2888, 65, 39, 811};
mergeSort(A, 0, A.length-1);//----忘记A.length减1,报错
for(int num:A)
{
System.out.println(num);
}
}
// 2
public static void mergeSort(int[] arr,int left, int right){
if(left<right){
int mid=(left+right)/2;
mergeSort(arr, left, mid);
mergeSort(arr,mid+1,right);
merge(arr, left, mid, right);
}
}
// 1、两两归并
public static void merge(int[] arr, int left, int mid, int right){
int r_left=mid+1;
int index=left;
int index1=left;
int[] temp=new int[arr.length];
while(left<=mid && r_left<=right){//-------写错了范围,写成left<r_left报错
if(arr[left]<arr[r_left]){
temp[index++]=arr[left++];
}else{
temp[index++]=arr[r_left++];
}
}//------注意,此处已跳出while循环
// 剩余部分处理
while(left<=mid){
temp[index++]=arr[left++];
}
while(r_left<=right){//---------- 忘了等号,报错
temp[index++]=arr[r_left++];
}
// 复制
while(index1<=right){
arr[index1]=temp[index1++];
}
}
}
*(9) 基数排序
*public class RadixSort {
/**
* 2017.7.5
* 基数排序
* 时间复杂度 o(dr+dn) 空间复杂度 o(dr+n) 稳定
* d为关键的长度 n为关键字的排序的个数 r为关键字的基数
*
public static void main(String[] args) {
int[] A=new int[]{745663,22, 93, 43, 55555, 14, 2888, 65, 39, 811};
radixSort(A);
for(int num:A)
{
System.out.println(num);
}
}
// 4
public static void radixSort(int arr[]){
int bits=findMaxBits(arr);
for(int i=0; i<bits; i++){
oneBitSort(arr, i+1);
}
}
// 3/
public static void oneBitSort(int[] arr, int bit){
int index=0;
int index1=0;
int[] temp=new int[arr.length];
for(int i=0; i<10; i++){
for(int j=0; j<arr.length; j++){
if(findOneBit(arr[j],bit)==i){
temp[index++]=arr[j];
}
}
}
while(index1<arr.length){
arr[index1]=temp[index1++];
}
}
//2/如(1234,3)=2
public static int findOneBit(int num, int bit){
int key1=(int)Math.pow(10, bit);
int key2=(int)Math.pow(10, bit-1);
num %=key1;
num /=key2;
return num;
}
// 1/
public static int findMaxBits(int[] arr){
if(arr == null || arr.length == 0){
return 0;
}
int max=arr[0];
for(int i=1; i<arr.length; i++){
if(arr[i]>max){
max=arr[i];
}
}
int bits=0;
while(max>0){
max/=10;
bits++;
}
return bits;
}
}
*
*
*
*/
《2》单例
http://www.runoob.com/design-pattern/singleton-pattern.html
单例模式
这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。
注意:
- 1、单例类只能有一个实例。
- 2、单例类必须自己创建自己的唯一实例。
- 3、单例类必须给所有其他对象提供这一实例。
介绍
意图:保证一个类仅有一个实例,并提供一个访问它的全局访问点。
主要解决:一个全局使用的类频繁地创建与销毁。
何时使用:当您想控制实例数目,节省系统资源的时候。
如何解决:判断系统是否已经有这个单例,如果有则返回,如果没有则创建。
关键代码:构造函数是私有的。
应用实例: 1、一个党只能有一个主席。 2、Windows 是多进程多线程的,在操作一个文件的时候,就不可避免地出现多个进程或线程同时操作一个文件的现象,所以所有文件的处理必须通过唯一的实例来进行。 3、一些设备管理器常常设计为单例模式,比如一个电脑有两台打印机,在输出的时候就要处理不能两台打印机打印同一个文件。
优点: 1、在内存里只有一个实例,减少了内存的开销,尤其是频繁的创建和销毁实例(比如管理学院首页页面缓存)。 2、避免对资源的多重占用(比如写文件操作)。
缺点:没有接口,不能继承,与单一职责原则冲突,一个类应该只关心内部逻辑,而不关心外面怎么样来实例化。
使用场景: 1、要求生产唯一序列号。 2、WEB 中的计数器,不用每次刷新都在数据库里加一次,用单例先缓存起来。 3、创建的一个对象需要消耗的资源过多,比如 I/O 与数据库的连接等。
注意事项:getInstance() 方法中需要使用同步锁 synchronized (Singleton.class) 防止多线程同时进入造成 instance 被多次实例化。
单例模式的几种实现方式
单例模式的实现有多种方式,如下所示:
1、懒汉式,线程不安全
是否 Lazy 初始化:是
是否多线程安全:否
实现难度:易
描述:这种方式是最基本的实现方式,这种实现最大的问题就是不支持多线程。因为没有加锁 synchronized,所以严格意义上它并不算单例模式。
这种方式 lazy loading 很明显,不要求线程安全,在多线程不能正常工作。
代码实例:
public class Singleton {
private static Singleton instance;
private Singleton (){}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
class Singleton {
private static Singleton instance;
private Singleton (){}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
接下来介绍的几种实现方式都支持多线程,但是在性能上有所差异。
2、懒汉式,线程安全
是否 Lazy 初始化:是
是否多线程安全:是
实现难度:易
描述:这种方式具备很好的 lazy loading,能够在多线程中很好的工作,但是,效率很低,99% 情况下不需要同步。
优点:第一次调用才初始化,避免内存浪费。
缺点:必须加锁 synchronized 才能保证单例,但加锁会影响效率。
getInstance() 的性能对应用程序不是很关键(该方法使用不太频繁)。
代码实例:
public class Singleton {
private static Singleton instance;
private Singleton (){}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
class Singleton {
private static Singleton instance;
private Singleton (){}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
3、饿汉式
是否 Lazy 初始化:否
是否多线程安全:是
实现难度:易
描述:这种方式比较常用,但容易产生垃圾对象。
优点:没有加锁,执行效率会提高。
缺点:类加载时就初始化,浪费内存。
它基于 classloder 机制避免了多线程的同步问题,不过,instance 在类装载时就实例化,虽然导致类装载的原因有很多种,在单例模式中大多数都是调用 getInstance 方法, 但是也不能确定有其他的方式(或者其他的静态方法)导致类装载,这时候初始化 instance 显然没有达到 lazy loading 的效果。
代码实例:
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton (){}
public static Singleton getInstance() {
return instance;
}
}
class Singleton {
private static Singleton instance = new Singleton();
private Singleton (){}
public static Singleton getInstance() {
return instance;
}
}
4、双检锁/双重校验锁(DCL,即 double-checked locking)
JDK 版本:JDK1.5 起
是否 Lazy 初始化:是
是否多线程安全:是
实现难度:较复杂
描述:这种方式采用双锁机制,安全且在多线程情况下能保持高性能。
getInstance() 的性能对应用程序很关键。
代码实例:
public class Singleton {
private volatile static Singleton singleton;
private Singleton (){}
public static Singleton getSingleton() {
if (singleton == null) {
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
}
class Singleton {
private volatile static Singleton singleton;
private Singleton (){}
public static Singleton getSingleton() {
if (singleton == null) {
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
}
5、登记式/静态内部类
是否 Lazy 初始化:是
是否多线程安全:是
实现难度:一般
描述:这种方式能达到双检锁方式一样的功效,但实现更简单。对静态域使用延迟初始化,应使用这种方式而不是双检锁方式。这种方式只适用于静态域的情况,双检锁方式可在实例域需要延迟初始化时使用。
这种方式同样利用了 classloder 机制来保证初始化 instance 时只有一个线程,它跟第 3 种方式不同的是:第 3 种方式只要 Singleton 类被装载了,那么 instance 就会被实例化(没有达到 lazy loading 效果),而这种方式是 Singleton 类被装载了,instance 不一定被初始化。因为 SingletonHolder 类没有被主动使用,只有显示通过调用 getInstance 方法时,才会显示装载 SingletonHolder 类,从而实例化 instance。想象一下,如果实例化 instance 很消耗资源,所以想让它延迟加载,另外一方面,又不希望在 Singleton 类加载时就实例化,因为不能确保 Singleton 类还可能在其他的地方被主动使用从而被加载,那么这个时候实例化 instance 显然是不合适的。这个时候,这种方式相比第 3 种方式就显得很合理。
代码实例:
public class Singleton {
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
private Singleton (){}
public static final Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}
class Singleton {
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
private Singleton (){}
public static final Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}
6、枚举
JDK 版本:JDK1.5 起
是否 Lazy 初始化:否
是否多线程安全:是
实现难度:易
描述:这种实现方式还没有被广泛采用,但这是实现单例模式的最佳方法。它更简洁,自动支持序列化机制,绝对防止多次实例化。
这种方式是 Effective Java 作者 Josh Bloch 提倡的方式,它不仅能避免多线程同步问题,而且还自动支持序列化机制,防止反序列化重新创建新的对象,绝对防止多次实例化。不过,由于 JDK1.5 之后才加入 enum 特性,用这种方式写不免让人感觉生疏,在实际工作中,也很少用。
不能通过 reflection attack 来调用私有构造方法。
代码实例:
public enum Singleton {
INSTANCE;
public void whateverMethod() {
}
}
enum Singleton {
INSTANCE;
public void whateverMethod() {
}
}
经验之谈:一般情况下,不建议使用第 1 种和第 2 种懒汉方式,建议使用第 3 种饿汉方式。只有在要明确实现 lazy loading 效果时,才会使用第 5 种登记方式。如果涉及到反序列化创建对象时,可以尝试使用第 6 种枚举方式。如果有其他特殊的需求,可以考虑使用第 4 种双检锁方式。
《3》输入输出
//《1》输入一个数组,再输入一个数字k
//Scanner scanner=new Scanner(System.in);
//String str=scanner.nextLine();
//String[] strArr=str.split("\\s");
//int[] intArr=new int[strArr.length];
//for(int i=0; i<strArr.length; i++){
// intArr[i]=Integer.valueOf(strArr[i]);
//}
//
//int k=scanner.nextInt();
//Arrays.sort(intArr);
//System.out.println(intArr[intArr.length-k]);
//<2>输入N,再每行输入n个数字
//Scanner sc=new Scanner(System.in);
//int a, b, n=sc.nextInt();
//while(n>0){
// a=sc.nextInt();
// b=sc.nextInt();
// System.out.println(a+b);
//}