数组排序相关题目以及排序方式

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

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; } }

题目:用同一个函数名对n个数据进行从小到大排序 用函数模板

                        
                
#include <iostream>
using namespace std;
template <class T>
void sort(T* a, int n)
{
 int t;
 T temp; 
 for (int i=0; i<n-1; i++) 
 { 
  t=i;
  for (int j=i+1; j<n; j++) 
  { 
   if (*(a+t)>*(a+j))
    t=j; 
  } 
  temp=*(a+i);
  *(a+i)=*(a+t); 
  *(a+t)=temp; 
 }
}
static float arr0[6]={2.0,65.0,9.0,78.0,88.0,-2.0};
static double arr1[6]={558.0,999.0,123.0,222.0,55.0,456.0};
static int arr2[6]={123,456,789,654,321,5};
void main()
{
 cout<<"float exp."<<endl;
 for (int i=0; i<6; i++)
 {
  cout<<arr0[i]<<" ";
 }
 cout<<endl;
 sort(arr0, 6);
 for (i=0; i<6; i++)
 {
  cout<<arr0[i]<<" ";
 }
 cout<<endl;
 cout<<"double exp."<<endl;
 for (i=0; i<6; i++)
 {
  cout<<arr1[i]<<" ";
 }
 cout<<endl;
 sort(arr1, 6);
 for (i=0; i<6; i++)
 {
  cout<<arr1[i]<<" ";
 }
 cout<<endl;
 cout<<"int exp."<<endl;
 for (i=0; i<6; i++)
 {
  cout<<arr2[i]<<" ";
 }
 cout<<endl;
 sort(arr2, 6);
 for (i=0; i<6; i++)
 {
  cout<<arr2[i]<<" ";
 }
 cout<<endl;
}

题目:用Array.Sort()函数直接对数组中的数字进行排序,按升序取出前五个

            int[] nums = new int[] { 2, 652, 32, 1, 6, 65 };

            Array.Sort(nums);

            //Array.Reverse(nums);//方法,反转,通过翻转把升序变成降序排列

          

            for (int i = 0; i < nums.Length; i++)

            {

                Console.WriteLine(nums[i] + "\t");

            }

                Console.ReadKey();

//结果:1,2,6,32,65

 

 

原文:https://bbs.csdn.net/topics/200075670

 https://bbs.csdn.net/topics/392035366?page=1

 java数组排序的几种方法

题目:给数组{5,2,66,3,7}排序

(1)用sort排序(从小到大)

public class Test{
         public static void main(String[] args){
            int[] arr = {5,2,66,3,7};
            Arrays.sort(arr);//Arrays是util包
            for(int i : arr){
            System.out.println(i);
            }
        } 
}            

(2)冒泡排序

从小到大

public class Test{
    public static void main(String[] args){
     int[] arr = {5,2,66,3,7};
     int temp;
     for(int i=0;i<arr.length;i++){   
      for(int j=0;j<arr.length-i-1;j++){ 
           if(arr[j]>arr[j+1]){ 
            temp = arr[j];           
            arr[j] = arr[j+1];           
            arr[j+1] = temp;    }     }      }     for(int i:arr){ 
      System.out.println(i);
    }

        
  }
}

从大到小,在主方法内

 public class Test{
    public static void main(String[] args){
    int[] arr = {5,2,66,3,7};
    int temp;
    for(int i=0;i<arr.length;i++){   
      for(int j=0;j<arr.length-i-1;j++){       
        if(arr[j]<arr[j+1]){           
          temp = arr[j];           
           arr[j] = arr[j+1];           
            arr[j+1] = temp;       
        }   
      }
    }
  for(int i:arr){   
      System.out.println(i);
  }
  

}

https://blog.csdn.net/lam521_125/article/details/80768825

java数组排序

题目:声明一个类people 数据成员包括姓名 性别 身高和体重 在测试类中创建10个pople对象放在一维数组中并按身高进行排序
方式1:
import java.util.Arrays;
import java.util.Scanner;

public class test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Object[] obj=null;
int[] l=null;
int m=0;
for(int i=0;i<10;i++){
People people = new People();
people.setHeight(i);
obj[i]=people;
}
for(Object k:obj){

l[m] =(People)k.getHeight;
m++;
}
Arrays.sort(l);

}

}

方式2:Java API

Java API针对集合类型排序提供了两种支持:
java.util.Collections.sort(java.util.List)
java.util.Collections.sort(java.util.List, java.util.Comparator)

第一个方法要求所排序的元素类必须实现java.lang.Comparable接口。
第二个方法要求实现一个java.util.Comparator接口。

java.lang.Comparable接口和  java.util.Comparator接口是Java对排序最提供最基本支持。这两个接口不但可以用于集合元素排序,还可以用于数组排序。

如果数组或集合元素是String类型,则可以利用Java API实现的Comparator<String>对象String.CASE_INSENSITIVE_ORDER为容器元素排序。

下面给出两个里测试,涵盖集合和数组的排序,并且还演示了数组和集合的相互转换,附件里面是完整的排序演示代码。
方法一:实现Comparable接口排序package collsort.comparable; 

package com.cvicse.sort.comparable;

public class Cat implements Comparable<Cat> {
private int age;
private String name;

public Cat(int age, String name) {
this.age = age;
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
......
public int compareTo(Cat o) {
return this.getAge() - o.getAge();
}
......
} 
通过实现Comparable接口实现个性化排序测试。
排序测试,Collection.sort(list)升序排列。
Collections.sort(list, Collections.reverseOrder());降序排列;
Collections.reverse(list);反转排序,先输出列表最后一个元素
public class TestComparable { public static void main(String args[]) { test(); test2(); } public static void test() { ...... List<Cat> listCat1 = new ArrayList<Cat>(); Cat cat1 = new Cat(34, "hehe"); Cat cat2 = new Cat(12, "haha"); Cat cat3 = new Cat(23, "leizhimin"); Cat cat4 = new Cat(13, "lavasoft"); listCat1.add(cat1); listCat1.add(cat2); listCat1.add(cat3); ...... System.out.println("调用Collections.sort(List<T> list)listCat2升序排序:"); Collections.sort(listCat1); System.out.println("降序排列元素:"); Collections.sort(listCat1, Collections.reverseOrder()); System.out.println("Collections.reverse 从列表中最后一个元素开始输出:"); Collections.reverse(listCat1); ...... } /** * 针对数组的排序 */ public static void test2() { String[] strArray = new String[] { "z", "a", "C" }; System.out.println("数组转换为列表"); List<String> list = Arrays.asList(strArray); System.out.println("顺序排序列表"); Collections.sort(list); System.out.println("按String实现的Comparator对象String.CASE_INSENSITIVE_ORDER排序----"); Collections.sort(list, String.CASE_INSENSITIVE_ORDER); System.out.println("倒序排序列表"); Collections.sort(list, Collections.reverseOrder()); ...... } }

方法二:实现Comparator接口排序

public class Person {
private int age;
private String name;
......
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
......
} 

实现了Comparator接口,重写了compare方法

import java.util.Comparator;
public class PersonComparator implements Comparator<Person> {

public int compare(Person o1, Person o2) {
return o1.getAge() - o2.getAge();
}
} 测试方法

public class TestComparator {
public static void main(String args[]) {
test1();
}
public static void test1() {
System.out.println("升序排序测试:");
List<Person> listPerson = new ArrayList<Person>();
Person person1 = new Person(34, "lavasoft");
Person person2 = new Person(12, "lavasoft");
Person person3 = new Person(23, "leizhimin");
Person person4 = new Person(13, "sdg");
listPerson.add(person1);
listPerson.add(person2);
listPerson.add(person3);
Comparator<Person> ascComparator = new PersonComparator();
System.out.println("排序后集合为:");
// 利用Collections类静态工具方法对集合List进行排序
Collections.sort(listPerson, ascComparator);
System.out.println("\n降序排序测试:");
// 从升序排序对象产生一个反转(降序)的排序对象
Comparator<Person> descComparator = Collections.reverseOrder(ascComparator);
System.out.println("利用反转后的排序接口对象对集合List排序并输出:");
Collections.sort(listPerson, descComparator);
outCollection(listPerson);
}
} 
以上的例子中使用是对int类型的属性排序,对String属性排序可以用以下的方法
public int compareTo(Cat o) {
return this.getName().compareTo(o.getName());
}
compareTo()函数的使用说明:
如果   结果
<0       a<b
==0    a==b
>=0         a>b
 
Java如何通过所实现接口的方法进行排序是API内部的事情,Java这样处理排序目的就是对容器元素排序有一个统一的方式,以简化编程。

 

posted @ 2019-03-14 20:59  浅嫣  阅读(451)  评论(0编辑  收藏  举报