9.编写一个程序,用 选择法 对数组 a[]={5,4,8,9,7,2,6,5}进行从大到小的排序,和从小到大的排序。

时间:2020-12-13 22:08:46
9.编写一个程序,用 选择法 对数组 a[]={5,4,8,9,7,2,6,5}进行从大到小的排序,和从小到大的排序。

18 个解决方案

#1


http://qqwyy.javaeye.com/blog/150163

#2


http://blog.****.net/yzs_china/

#3


不知道是不是选择法!希望知道的人多说说排序的思想,谢谢!


public class SortTest {
public static void main(String[] args) {
int a[] = { 5, 4, 8, 9, 7, 2, 6, 5, 10 };
int[] b = selectMaxToMinSort(a);
for (int value : b) {
System.out.print(value + " ");
}
}

public static int[] selectMinToMaxSort(int[] input) {
for (int i = 0; i < input.length; i++) {
int temp = 9999;
int index = 0;
for (int j = i; j < input.length; j++) {
if (temp > input[j]) {
temp = input[j];
index = j;
}
}
if (index != 0) {
int t = input[index];
input[index] = input[i];
input[i] = t;
}
}
return input;
}

public static int[] selectMaxToMinSort(int[] input) {
for (int i = 0; i < input.length; i++) {
int temp = -9999;
int index = 0;
for (int j = i; j < input.length; j++) {
if (temp < input[j]) {
temp = input[j];
index = j;
}
}
if (index != 0) {
int t = input[index];
input[index] = input[i];
input[i] = t;
}
}
return input;
}
}

#4


作业?

#5


public class Permission{
    public Permission(){}
    public static void main(String[] args[]){

    }
    public static void main(String[] args){
     String[] ar[] = {{"1","2"}};
     System.out.println(ar[0][1]);
    }
}

class SSort{
    static public  void main(String args[]){
         Integer[] arrayInt;
         if(args.length>0){
           arrayInt = new Integer[args.length];
           for(int i=0;i<args.length;i++){
           arrayInt[i] = Integer.parseInt(args[i]);
           }
          
           SSort util = new SSort();
           Integer[] rtn = util.AscSort(arrayInt);
           for(int i=0;i<rtn.length;i++){
           System.out.println(rtn[i]);
           }
           
           rtn = util.DscSort(arrayInt);
           for(int i=0;i<rtn.length;i++){
           System.out.println(rtn[i]);
           }
         }
    }
    
    private Integer[] AscSort(Integer[] tar){
Integer[] target = tar;
for(int i=0;i<target.length;i++){
  int pos = getMinPos(i,target);
  int temp = target[pos];
  target[pos]=target[i];
  target[i]=temp;   
}
return target;
    }
    
    private Integer[] DscSort(Integer[] tar){
     Integer[] target = tar;
for(int i=0;i<target.length;i++){
  int pos = getMaxPos(i,target);
  int temp = target[pos];
  target[pos]=target[i];
  target[i]=temp;   
}
return target;
    }
    
    private int getMaxPos(int start,Integer[] target){
     int pos=start;
      int temp=target[start];
     for(int i=start;i<target.length;i++){
     if(temp<target[i]){
     temp=target[i];
     pos=i;
     }
     }
      return pos;
    }
    
    private int getMinPos(int start,Integer[] target){
     int pos=start;
      int temp=target[start];
     for(int i=start;i<target.length;i++){
     if(temp>target[i]){
     temp=target[i];
     pos=i;
     }
     }
      return pos;
    }
    
}

#6


雨夜写的应该是快速排序吧。

选择排序的思想就是:
排列:a1,a2,a3,a4.....an
从a中找一个最小ai的放到a1的位置,然后从甚下的n-1个a中找一个最小的,放到a2的位置,。。。

#7


楼主你自己提供的两个链接里面不是都有吗?

#8


用Java语言实现的各种排序,包括插入排序、冒泡排序、选择排序、Shell排序、快速排序、归并排序、堆排序、SortUtil等.
请参考我的 blog :
http://hbohuan.blog.163.com/blog/static/2084898200812525758781/


#9


楼上的,我留言了。

#10


你自己结贴了。

#11


healer_kx , 留言收到,谢谢!

#12


将数组中的数据封装成对象 加入到TreeSet中,省时省力!哈哈!

#13



class Test{
static int a[]={5,4,8,9,7,2,6,5};
    public static void main( String[] args ){
     System.out.println("=====升序======");
     for(int i=1;i<a.length;i++){
     int temp=a[i];
     int j=i;
     for(;(j>0)&&(temp<a[j-1]);j--){
     a[j]=a[j-1];
     }
     a[j]=temp;
     }
     for(int n:a){
     System.out.println(n);
     }
     System.out.println("=====降序======");
     for(int i=1;i<a.length;i++){
     int temp=a[i];
     int j=i;
     for(;(j>0)&&(temp>a[j-1]);j--){
     a[j]=a[j-1];
     }
     a[j]=temp;
     }
     for(int n:a){
     System.out.println(n);
     }
    }
}

#14


大家帮我分析一下, 我写的排序结果有问题, 不知道是哪里有错?
一下程序的排序结果是:

选择排序法对数组按从小到大进行排序:
 a[0]:5
 a[1]:4
 a[2]:8
 a[3]:9
 a[4]:7
 a[5]:2
 a[6]:6
 a[7]:5
选择排序后:
 2 4 5 8 6 5 7 9

----------------------------------------------------------------------------------------------

class  SelectSort
{
//选择排序方法   由小到大
public int[] selSorting(int[] a){
int tmp = 0 ;
int index= 0;
for(int i=0;i<a.length-1;i++){
index=i;
tmp=a[i];
for(int j=i+1; j<a.length; j++){
if(a[j]<a[i]){
index=j;
}
}
if(i!=index){
tmp=a[i];
a[i]=a[index];
a[index]=tmp;
};
}
return a;
}

//MAIN 方法调用
public static void main(String[] args) 
{
int a[] = {5,4,8,9,7,2,6,5};
System.out.println("选择排序法对数组按从小到大进行排序:");
for(int i=0; i<a.length; i++){
System.out.println(" a["+i+"]:"+a[i]);
}

SelectSort ss = new SelectSort();
int sortedA[] = ss.selSorting(a);
System.out.println("选择排序后:");
for(int i=0; i<sortedA.length; i++){
System.out.print(" "+sortedA[i]);
}
}
}

#15


上面的代码格式变了, 不利阅读, 所以我重新贴一下代码:

class  SelectSort 

{
//排序方法
public int[] selSorting(int[] a){
int tmp = 0 ;
int index= 0;
for(int i=0;i<a.length-1;i++){
index=i;
tmp=a[i];
for(int j=i+1; j<a.length; j++){
if(a[j]<a[i]){
index=j;
}
}
if(i!=index){
tmp=a[i];
a[i]=a[index];
a[index]=tmp;
};
}
return a;
}

//MAIN 方法调用
public static void main(String[] args) 
{
int a[] = {5,4,8,9,7,2,6,5};
System.out.println("选择排序法对数组按从小到大进行排序:");
for(int i=0; i<a.length; i++){
System.out.println(" a["+i+"]:"+a[i]);
}

SelectSort ss = new SelectSort();
int sortedA[] = ss.selSorting(a);
System.out.println("选择排序后:");
for(int i=0; i<sortedA.length; i++){
System.out.print(" "+sortedA[i]);
}
}
}

#16



 public static void main(String args[]) {

    int vec[] = new int[] { 37, 47, 23, -5, 19, 56 };

    int temp;

      for (int i = 0; i < vec.length; i++) {

        for (int j = i; j < vec.length; j++) {

          if (vec[j] > vec[i]) {

            temp = vec[i];

            vec[i] = vec[j];

            vec[j] = temp;

          }

        }
    }

#17


上面打掉了一个“}”

#18


if(a[j] <a[i]){
index=j;


======>
if(a[j] <a[index]){
index=j;

#1


http://qqwyy.javaeye.com/blog/150163

#2


http://blog.****.net/yzs_china/

#3


不知道是不是选择法!希望知道的人多说说排序的思想,谢谢!


public class SortTest {
public static void main(String[] args) {
int a[] = { 5, 4, 8, 9, 7, 2, 6, 5, 10 };
int[] b = selectMaxToMinSort(a);
for (int value : b) {
System.out.print(value + " ");
}
}

public static int[] selectMinToMaxSort(int[] input) {
for (int i = 0; i < input.length; i++) {
int temp = 9999;
int index = 0;
for (int j = i; j < input.length; j++) {
if (temp > input[j]) {
temp = input[j];
index = j;
}
}
if (index != 0) {
int t = input[index];
input[index] = input[i];
input[i] = t;
}
}
return input;
}

public static int[] selectMaxToMinSort(int[] input) {
for (int i = 0; i < input.length; i++) {
int temp = -9999;
int index = 0;
for (int j = i; j < input.length; j++) {
if (temp < input[j]) {
temp = input[j];
index = j;
}
}
if (index != 0) {
int t = input[index];
input[index] = input[i];
input[i] = t;
}
}
return input;
}
}

#4


作业?

#5


public class Permission{
    public Permission(){}
    public static void main(String[] args[]){

    }
    public static void main(String[] args){
     String[] ar[] = {{"1","2"}};
     System.out.println(ar[0][1]);
    }
}

class SSort{
    static public  void main(String args[]){
         Integer[] arrayInt;
         if(args.length>0){
           arrayInt = new Integer[args.length];
           for(int i=0;i<args.length;i++){
           arrayInt[i] = Integer.parseInt(args[i]);
           }
          
           SSort util = new SSort();
           Integer[] rtn = util.AscSort(arrayInt);
           for(int i=0;i<rtn.length;i++){
           System.out.println(rtn[i]);
           }
           
           rtn = util.DscSort(arrayInt);
           for(int i=0;i<rtn.length;i++){
           System.out.println(rtn[i]);
           }
         }
    }
    
    private Integer[] AscSort(Integer[] tar){
Integer[] target = tar;
for(int i=0;i<target.length;i++){
  int pos = getMinPos(i,target);
  int temp = target[pos];
  target[pos]=target[i];
  target[i]=temp;   
}
return target;
    }
    
    private Integer[] DscSort(Integer[] tar){
     Integer[] target = tar;
for(int i=0;i<target.length;i++){
  int pos = getMaxPos(i,target);
  int temp = target[pos];
  target[pos]=target[i];
  target[i]=temp;   
}
return target;
    }
    
    private int getMaxPos(int start,Integer[] target){
     int pos=start;
      int temp=target[start];
     for(int i=start;i<target.length;i++){
     if(temp<target[i]){
     temp=target[i];
     pos=i;
     }
     }
      return pos;
    }
    
    private int getMinPos(int start,Integer[] target){
     int pos=start;
      int temp=target[start];
     for(int i=start;i<target.length;i++){
     if(temp>target[i]){
     temp=target[i];
     pos=i;
     }
     }
      return pos;
    }
    
}

#6


雨夜写的应该是快速排序吧。

选择排序的思想就是:
排列:a1,a2,a3,a4.....an
从a中找一个最小ai的放到a1的位置,然后从甚下的n-1个a中找一个最小的,放到a2的位置,。。。

#7


楼主你自己提供的两个链接里面不是都有吗?

#8


用Java语言实现的各种排序,包括插入排序、冒泡排序、选择排序、Shell排序、快速排序、归并排序、堆排序、SortUtil等.
请参考我的 blog :
http://hbohuan.blog.163.com/blog/static/2084898200812525758781/


#9


楼上的,我留言了。

#10


你自己结贴了。

#11


healer_kx , 留言收到,谢谢!

#12


将数组中的数据封装成对象 加入到TreeSet中,省时省力!哈哈!

#13



class Test{
static int a[]={5,4,8,9,7,2,6,5};
    public static void main( String[] args ){
     System.out.println("=====升序======");
     for(int i=1;i<a.length;i++){
     int temp=a[i];
     int j=i;
     for(;(j>0)&&(temp<a[j-1]);j--){
     a[j]=a[j-1];
     }
     a[j]=temp;
     }
     for(int n:a){
     System.out.println(n);
     }
     System.out.println("=====降序======");
     for(int i=1;i<a.length;i++){
     int temp=a[i];
     int j=i;
     for(;(j>0)&&(temp>a[j-1]);j--){
     a[j]=a[j-1];
     }
     a[j]=temp;
     }
     for(int n:a){
     System.out.println(n);
     }
    }
}

#14


大家帮我分析一下, 我写的排序结果有问题, 不知道是哪里有错?
一下程序的排序结果是:

选择排序法对数组按从小到大进行排序:
 a[0]:5
 a[1]:4
 a[2]:8
 a[3]:9
 a[4]:7
 a[5]:2
 a[6]:6
 a[7]:5
选择排序后:
 2 4 5 8 6 5 7 9

----------------------------------------------------------------------------------------------

class  SelectSort
{
//选择排序方法   由小到大
public int[] selSorting(int[] a){
int tmp = 0 ;
int index= 0;
for(int i=0;i<a.length-1;i++){
index=i;
tmp=a[i];
for(int j=i+1; j<a.length; j++){
if(a[j]<a[i]){
index=j;
}
}
if(i!=index){
tmp=a[i];
a[i]=a[index];
a[index]=tmp;
};
}
return a;
}

//MAIN 方法调用
public static void main(String[] args) 
{
int a[] = {5,4,8,9,7,2,6,5};
System.out.println("选择排序法对数组按从小到大进行排序:");
for(int i=0; i<a.length; i++){
System.out.println(" a["+i+"]:"+a[i]);
}

SelectSort ss = new SelectSort();
int sortedA[] = ss.selSorting(a);
System.out.println("选择排序后:");
for(int i=0; i<sortedA.length; i++){
System.out.print(" "+sortedA[i]);
}
}
}

#15


上面的代码格式变了, 不利阅读, 所以我重新贴一下代码:

class  SelectSort 

{
//排序方法
public int[] selSorting(int[] a){
int tmp = 0 ;
int index= 0;
for(int i=0;i<a.length-1;i++){
index=i;
tmp=a[i];
for(int j=i+1; j<a.length; j++){
if(a[j]<a[i]){
index=j;
}
}
if(i!=index){
tmp=a[i];
a[i]=a[index];
a[index]=tmp;
};
}
return a;
}

//MAIN 方法调用
public static void main(String[] args) 
{
int a[] = {5,4,8,9,7,2,6,5};
System.out.println("选择排序法对数组按从小到大进行排序:");
for(int i=0; i<a.length; i++){
System.out.println(" a["+i+"]:"+a[i]);
}

SelectSort ss = new SelectSort();
int sortedA[] = ss.selSorting(a);
System.out.println("选择排序后:");
for(int i=0; i<sortedA.length; i++){
System.out.print(" "+sortedA[i]);
}
}
}

#16



 public static void main(String args[]) {

    int vec[] = new int[] { 37, 47, 23, -5, 19, 56 };

    int temp;

      for (int i = 0; i < vec.length; i++) {

        for (int j = i; j < vec.length; j++) {

          if (vec[j] > vec[i]) {

            temp = vec[i];

            vec[i] = vec[j];

            vec[j] = temp;

          }

        }
    }

#17


上面打掉了一个“}”

#18


if(a[j] <a[i]){
index=j;


======>
if(a[j] <a[index]){
index=j;