Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1607094239 金浩 #19

Open
shencang opened this issue Nov 22, 2017 · 0 comments
Open

1607094239 金浩 #19

shencang opened this issue Nov 22, 2017 · 0 comments

Comments

@shencang
Copy link

# 1607094239 金浩
(杂七杂八写了一堆······)

package no.one;
public class BaseSort{
	public void sort(int []a){
		System.out.println("排序算法");
		} 
} 
package no.one;

public class Factory {
	private BaseSort sort;
	//依赖注入
	public void setSort(BaseSort sort){
		this.sort = sort;
		}
	public void doSort(int []a){
		sort.sort(a);
		}
	}//策略类 

package no.one;

public class InsertSort extends BaseSort{//插入排序
	 public void sort(int array[]){
		
		int i,j,longth=0;
		int temp;
		for(int s:array){
			longth++;
		}
		  int first=0;
		  int last =longth-1;
		  
       
        for (i = first+1; i<=last;i++)  
        {  
                temp = array[i];  
                j=i-1;  
   
                //与已排序的数逐一比较,大于temp时,该数后移  
                while((j>=first) && (array[j] > temp))  //当first=0,j循环到-1时,由于[[短路求值]],不会运算array[-1]  
                {  
                        array[j+1] = array[j];  
                        j--;  
                }  
                array[j+1] = temp;      //被排序数放到正确的位置  
   
         
        }
        Print dis = new Print();
        dis.Display(array, longth);
    }
	
}

package no.one;

public class QuickSort extends BaseSort{
   //快速排序
	 public void sort(int array[]){
			
			int i,j,longth=0;
			int temp;
			for(int s:array){
				longth++;
			}
			  int first=0;
			  int last =longth-1;
	 
			  if(first<last)  
			  {  
			    i=first;j=last;  
			    array[0]=array[i]; //准备以本次最左边的元素值为标准进行划分,先保存其值  
			    do  
			    {  
			      while(array[j]>array[0] && i<j)   
			        j--;        //从右向左找第1个小于标准值的位置j  
			      if(i<j)                               //找到了,位置为j  
			      {   
			        array[i] = array[j];  
			        i++;  
			      }           //将第j个元素置于左端并重置i  
			      while(array[i]<array[0] && i<j)  
			        i++;      //从左向右找第1个大于标准值的位置i  
			      if(i<j)                       //找到了,位置为i  
			      {   
			        array[j] = array[i];  
			        j--;  
			      }           //将第i个元素置于右端并重置j  
			    }while(i!=j);  
			    array[i] = array[0];         //将标准值放入它的最终位置,本次划分结束  
			    this.sort(array, first, i-1);     //对标准值左半部递归调用本函数  
			    this.sort(array, i+1, last);    //对标准值右半部递归调用本函数  
			  }  
			  
			  
	        Print dis = new Print();
	        dis.Display(array, longth);
	    }
	 
	 
	 public void sort(int array[],int l,int r){//为了递归= =
			
			int i,j,longth=0;
			int temp;
			for(int s:array){
				longth++;
			}
			  int first=0;
			  int last =longth-1;
	 
			  if(first<last)  
			  {  
			    i=first;j=last;  
			    array[0]=array[i]; //准备以本次最左边的元素值为标准进行划分,先保存其值  
			    do  
			    {  
			      while(array[j]>array[0] && i<j)   
			        j--;        //从右向左找第1个小于标准值的位置j  
			      if(i<j)                               //找到了,位置为j  
			      {   
			        array[i] = array[j];  
			        i++;  
			      }           //将第j个元素置于左端并重置i  
			      while(array[i]<array[0] && i<j)  
			        i++;      //从左向右找第1个大于标准值的位置i  
			      if(i<j)                       //找到了,位置为i  
			      {   
			        array[j] = array[i];  
			        j--;  
			      }           //将第i个元素置于右端并重置j  
			    }while(i!=j);  
			    array[i] = array[0];         //将标准值放入它的最终位置,本次划分结束  
			    this.sort(array, first, i-1);     //对标准值左半部递归调用本函数  
			    this.sort(array, i+1, last);    //对标准值右半部递归调用本函数  
			  }  
			  }
		
}

package no.one;

public class SelectSort extends BaseSort{
	//选择排序
	
	 public void sort(int array[]){
			
			int i,j,longth=0;
			int temp,min;
			for(int s:array){
				longth++;
			}
		 
			    for(i = 0; i < longth - 1; i ++)  
			    {  
			        min = i;  
			        //查找最小值  
			        for(j = i + 1; j < longth; j ++)  
			            if(array[min] > array[j])  
			                min = j;  
			        //交换  
			        if(min != i)  
			        {  
			            temp = array[min];  
			            array[min] = array[i];  
			            array[i] = temp;  
			        }  
			    }  
			    
		        Print dis = new Print();
		        dis.Display(array, longth); 
	    
	    }
		
	}

package no.one;

import java.util.Arrays;
import java.util.Scanner;

public class Inport {
	
	
	public int[]  jin(int n){
		
//		System.out.println("请输入你要排序的数的个数:");
//		Scanner jin= new Scanner(System.in);
//		int n= jin.nextInt();
		int a[] = new int[n] ;
		Scanner s= new Scanner(System.in);
		
		for(int i= 0;i<n;i++){
	          a[i]=s.nextInt();		
		}
		System.out.println("您输入的数组为"+Arrays.toString(a));
		
		return a;
	}

}
package no.one;

public class Print {
	
	void Display(int array[],int longth){
		
		 System.out.println(longth+"个数字的排序为:");  
	        for(int j1= 0; j1<longth; j1++){  
	        System.out.print(array[j1]+" ");  
         }  
	        System.out.println("\n");
		
	}

}
package no.one;

//import java.util.Arrays;
import java.util.Scanner;

public class Test {

//自己用键盘输入 
	


	
	public static void main(String[] args) {
		// TODO 自动生成的方法存根

		
		System.out.println("请输入你要排序的数的个数:");
		Scanner jin= new Scanner(System.in);
		int n= jin.nextInt();
		System.out.println("请输入你要排序的数(用空格分开):");
		Inport inarray = new Inport();
		int a[] =inarray.jin(n);
//		 
		
		
		Factory factory=new Factory();
		BaseSort select_sort = new SelectSort();
		factory.setSort(select_sort);factory.doSort(a);
		BaseSort insert_sort = new InsertSort();
		factory.setSort(insert_sort);factory.doSort(a);
		BaseSort quick_sort = new InsertSort();
		factory.setSort(quick_sort);factory.doSort(a);

	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant