逆转数组
public static void main(String[] args) {
int[] arrays = {1,2,3,4,5};
int[] newArrays = reverse(arrays);
for (int i = 0; i < newArrays.length; i++) {
System.out.print(newArrays[i]+" ");//5 4 3 2 1
}
}
public static int[] reverse(int[] arrayOld)
{
int[] res = new int[arrayOld.length];
for (int i = 0,j=res.length-1; i < res.length; i++,j--) {
res[i]=arrayOld[j];
}
return res;
}
冒泡排序
public static void main(String[] args) {
int[] array = {10,3,4,634,5,333335,623,46,346,6,7,8,45,5435,6,7754,3565,4355,234,66634};
sort2(array);
System.out.println(Arrays.toString(array));//[3, 4, 5, 6, 6, 7, 8, 10, 45, 46, 234, 346, 623, 634, 3565, 4355, 5435, 7754, 66634, 333335]
}
public static void sort(int[] array)
{
int temp;
//
for (int i = 0; i < array.length; i++) {
temp = array[i];
for (int j = i; j < array.length; j++) {
if(array[i]>array[j])
{
array[i]=array[j];
array[j]=temp;
temp =array[i];
}
}
}
}
static void sort2(int[] array)
{
int temp=0;
for (int i = 0; i < array.length-1; i++) {
for (int j = 0; j < array.length-i-1; j++) {
if(array[j]>array[j+1])
{
temp = array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
}