冒泡排序---java,
分享于 点击 44664 次 点评:194
冒泡排序---java,
import java.util.*;
public class BubbleSort
{
public static int[] bubbleSort(int[] x)
{
int N = x.length;
int[] a = new int[N+1];
System.out.println(N*(N-1)/2);
for (int i = 0; i < N; i++)
a[i] = x[i];
int t = 0;
int c = 0;
for (int i = 0; i < N-1; i++)
for (int j = 0; j < N-1-i; j++)
{
c ++;
if (a[j] > a[j+1])
{
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
a[N] = c; // save enter loop counter
return a;
}
public static void main(String[] args)
{
int[] a = {1,2,4,3,5,8,6,7,7,9,11,22,44,33,55,66,99,88,0};
int[] r = bubbleSort(a);
System.out.println(Arrays.toString(r));
}
}
/*
171
[0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 11, 22, 33, 44, 55, 66, 88, 99, 171]
*/
相关文章
- 暂无相关文章
用户点评