9.编写一个程序,用 选择法 对数组 a[]={5,4,8,9,7,2,6,5}进行从大到小的排序,和从小到大的排序。 9.编写一个程序,用 选择法 对数组 a[]={5,4,8,9,7,2,6,5}进行从大到小的排序,和从小到大的排序。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不知道是不是选择法!希望知道的人多说说排序的思想,谢谢!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; }} 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; } } 雨夜写的应该是快速排序吧。选择排序的思想就是:排列:a1,a2,a3,a4.....an从a中找一个最小ai的放到a1的位置,然后从甚下的n-1个a中找一个最小的,放到a2的位置, 用Java语言实现的各种排序,包括插入排序、冒泡排序、选择排序、Shell排序、快速排序、归并排序、堆排序、SortUtil等.请参考我的 blog :http://hbohuan.blog.163.com/blog/static/2084898200812525758781/ healer_kx , 留言收到,谢谢! 将数组中的数据封装成对象 加入到TreeSet中,省时省力!哈哈! 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); } }} 大家帮我分析一下, 我写的排序结果有问题, 不知道是哪里有错?一下程序的排序结果是:选择排序法对数组按从小到大进行排序: 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]); } }} 上面的代码格式变了, 不利阅读, 所以我重新贴一下代码: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]); } }} 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; } } } if(a[j] <a[i]){index=j;} ======>if(a[j] <a[index]){index=j;} 这种插入图片方式是否正确?? 利用jni如何在C中将String数组转化为char** 下班回家! JAVA做界面开发,数据方面一般用什么技术 这道题如何做? [求助]访问tomcat服务器上的https页面 出现的问题 关于HashMap中value的问题 求救,sos!!! 开发前奏,学习java的旅程连载(2) 请教了多次,问题依然存在,请大家再指教一下,非常感谢!!!!!! java初学者的问题 老师会不会带学生做项目啊
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;
}
}
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;
}
}
排列:a1,a2,a3,a4.....an
从a中找一个最小ai的放到a1的位置,然后从甚下的n-1个a中找一个最小的,放到a2的位置,
请参考我的 blog :
http://hbohuan.blog.163.com/blog/static/2084898200812525758781/
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);
}
}
}
一下程序的排序结果是:选择排序法对数组按从小到大进行排序:
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]);
}
}
}
{
//排序方法
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]);
}
}
}
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; } }
}
index=j;
} ======>
if(a[j] <a[index]){
index=j;
}