码迷,mamicode.com
首页 > 编程语言 > 详细

简单排序和输出

时间:2018-05-26 15:30:49      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:最大值   static   swa   sele   选择   print   冒泡排序   排序   get   

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]+" ]");
}
}

}

简单排序和输出

标签:最大值   static   swa   sele   选择   print   冒泡排序   排序   get   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!