java输入行数打印菱形_JAVA题,输入行数,输入列数,输出一个菱形

这是一个关于JAVA排序算法的博客,详细介绍了冒泡排序、选择排序和插入排序的实现代码,包括排序前后的数组展示以及排序过程的输出。
摘要由CSDN通过智能技术生成

展开全部

1,冒泡排序

1. /**

2. * JAVA排序算法实现代码-冒泡(Bubble Sort)排序。

3. *

4. *

5. *

6. */

7. public class Test {

8. public static void main(String[] args) {

9. int[] a = ;

10.

11. System.out.print("排序前: ");

12.

13. for (int i = 0; i < a.length; i++)

14. System.out.printf("%3s", a[i]);

15.

16. System.out.println();

17.

18. Test test = new Test();

19. test.bubbleSort(a);

20.

21. System.out.print("排序后: ");

22.

23. for (int i = 0; i < a.length; i++)

24. System.out.printf("%3s", a[i]);

25.

26. System.out.println();

27. }

28.

29. public void bubbleSort(int[] a) {

30. int len = a.length;

31.

32. System.out.println("数组大e68a84e8a2ad3231313335323631343130323136353331333264623232小是:" + len);

33.

34. boolean change = false;

35. int temp;

36. int count = 0;

37.

38. for (int i = len; i > 1; i--) {

39. for (int j = 0; j < i - 1; j++) {

40. if (a[j + 1] < a[j]) {

41. temp = a[j + 1];

42. a[j + 1] = a[j];

43. a[j] = temp;

44. change = true;

45. count++;

46. }

47.

48. }

49. if (change) {

50. System.out.print("第" + count + "趟交换: ");

51. for (int k = 0; k < len; k++)

52. System.out.print(a[k] + " ");

53.

54. System.out.println();

55. }

56. }

57. }

58. }

2,选择排序

1. /**

2. * JAVA排序算法实现代码-选择(Select)式排序。

3. *

4. *

5. *

6. */

7. public class Test {

8. public static int[] a = ; // 预设数据数组

9.

10. public static void main(String args[]) {

11. int i; // 循环计数变量

12. int Index = a.length;// 数据索引变量

13.

14. System.out.print("排序前: ");

15. for (i = 0; i < Index - 1; i++)

16. System.out.printf("%3s", a[i]);

17. System.out.println("");

18.

19. SelectSort(Index - 1); // 选择排序

20. // 排序后结果

21. System.out.print("排序后: ");

22. for (i = 0; i < Index - 1; i++)

23. System.out.printf("%3s", a[i]);

24. System.out.println("");

25. }

26.

27. public static void SelectSort(int Index) {

28. int i, j, k; // 循环计数变量

29. int MinValue; // 最小值变量

30. int IndexMin; // 最小值索引变量

31. int Temp; // 暂存变量

32.

33. for (i = 0; i < Index - 1; i++) {

34. MinValue = 32767; // 目前最小数值

35. IndexMin = 0; // 储存最小数值的索引值

36. for (j = i; j < Index; j++) {

37. if (a[j] < MinValue) // 找到最小值

38. {

39. MinValue = a[j]; // 储存最小值

40. IndexMin = j;

41. }

42. Temp = a[i]; // 交换两数值

43. a[i] = a[IndexMin];

44. a[IndexMin] = Temp;

45. }

46.

47. System.out.print("排序中: ");

48. for (k = 0; k < Index; k++)

49. System.out.printf("%3s", a[k]);

50. System.out.println("");

51. }

52. }

53. }

3,交换排序

冒泡排序就是一种交换排序啊

这里给你一个插入排序吧!

1. /**

2. * JAVA排序算法实现代码-插入排序。

3. *

4. *

5. *

6. */

7. public class Test {

8. public static int[] a = ; // 预设数据数组

9.

10. public static void main(String args[]) {

11. int i; // 循环计数变量

12. int Index = a.length;// 数据索引变量

13.

14. System.out.print("排序前: ");

15. for (i = 0; i < Index - 1; i++)

16. System.out.print(" " + a[i] + " ");

17. System.out.println("");

18.

19. InsertSort(Index - 1); // 选择排序

20. // 排序后结果

21. System.out.print("排序后: ");

22. for (i = 0; i < Index - 1; i++)

23. System.out.print(" " + a[i] + " ");

24. System.out.println("");

25. }

26.

27. public static void InsertSort(int Index) {

28. int i, j, k; // 循环计数变量

29. int InsertNode; // 欲插入数据变量

30.

31. for (i = 1; i < Index; i++) // 依序插入数值

32. {

33. InsertNode = a[i]; // 设定欲插入的数值

34. j = i - 1; // 欲插入数组的开始位置

35. // 找适当的插入位置

36. while (j >= 0 && InsertNode < a[j]) {

37. a[j + 1] = a[j];

38. j--;

39. }

40. a[j + 1] = InsertNode; // 将数值插入

41. // 打印目前排序结果

42. System.out.print("排序中: ");

43. for (k = 0; k < Index; k++)

44. System.out.print(" " + a[k] + " ");

45. System.out.println("");

46. }

47. }

48. }

4,打印九九表:

public class Jiujiu {

public static void main(String args[]) {

Jiujiu jj = new Jiujiu();

jj.test();

}

public void test() {

for (int i = 1; i <= 9; i++) {

System.out.println("");

for (int j = 1; j <= i; j++) {

System.out.print(i + "*" + j + "=" + i * j + "; ");

}

}

}

}

5,打印金字塔

public class Pyramid {

public static void main(String[] args){

for(int i=0;i<10;i++){

for(int j=0;j<2*i+1;j++){

if(j<=i){

System.out.print(" "+(int)Math.pow(2, j));

}else{

System.out.print(" "+(int)Math.pow(2, 2*i-j));

}

}

System.out.println();

}

}

}

6,打印杨辉三角

public class YangHui {

public static void main(String args[]) {

final int ROW = 10;

int a[][] = new int[ROW + 1][];

for (int i = 0; i <= ROW; i++) {

a[i] = new int[i + 1]; // 指定每行的列数

}

yanghui(a, ROW);

}

static void yanghui(int a[][], int ROW) {

for (int i = 0; i <= ROW; i++)

for (int j = 0; j <= a[i].length - 1; j++) {

if (i == 0 || j == 0 || j == a[i].length - 1)

a[i][j] = 1;

else

a[i][j] = a[i - 1][j - 1] + a[i - 1][j];

}

for (int i = 0; i <= ROW; i++) {

for (int j = 0; j <= a[i].length - 1; j++)

System.out.print(a[i][j] + "\t");

System.out.println();

}

}

}

大题不明白你啥意思啊

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值