首页 > 编程语言 > 详细

简单排序和输出

时间:2018-05-26 15:09:52      阅读:191      评论:0      收藏:0      [点我收藏+]

class ArrayTool
{
//最大值
public static int getMax(int[] arr)
{
int max=0;
for (int x=0;x<arr.length ;x++ )
{
if (arr[x]>arr[max])
{
max =x;
}
}
return arr[max];
}
//最小值
public static int getMin(int[] arr)
{
int min=0;
for (int x=0;x<arr.length ;x++ )
{
if (arr[x]<arr[min])
{
min =x;
}
}
return arr[min];
}


//选择排序
public void selectSort(int[] arr)
{
for (int x =0;x<arr.length ;x++ )
{
for (int y=x+1;y<arr.length ;y++ )
{
if (arr[x]>arr[y])
{
swap(arr,x,y);
}
}
}
}
//冒泡排序
public void bubbleSort(int[] arr)
{
for (int x=0;x<arr.length-1 ;x++ )
{
for (int y=0;y<arr.length-x-1 ;y++ )
{
if (arr[y]>arr[y+1])
{
swap(arr,y,y+1);
}
}
}
}
///交换.
public void swap(int[] arr,int a,int b )
{
int temp =arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
//输出.
public void printArray(int[] arr)
{
System.out.print("[");
for (int x=0;x<arr.length ;x++ )
{
if (x!=arr.length-1)
System.out.print(arr[x]+" , ");
else
System.out.println(arr[x]+" ]");
}
}

}

简单排序和输出

原文:https://www.cnblogs.com/agzno1hb/p/9092905.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!