冒泡排序(JAVA和C語言)

1、C語言ide

#include <stdio.h> spa

#include <stdlib.h>

int main()
{
int i,j,t,a[10];
printf("Please input 10 integers:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<9;i++) /* 冒泡法排序 */
for(j=0;j<10-i-1;j++)
if(a[j]>a[j+1])
{t=a[j];/* 交換a[i]和a[j] */
a[j]=a[j+1];
a[j+1]=t;
}
printf("The sequence after sort is:\n");
for(i=0;i<10;i++)
printf("%-5d",a[i]);
printf("\n");
system("pause");
return 0;

} 排序


2、JAVA:input

1 public class BubbleSort{it

2      public static void main(String[] args){io

3          int score[] = {67, 69, 75, 87, 89, 90, 99, 100};class

4          for (int i = 0; i < score.length -1; i++){    //最多作n-1趟排序sort

5              for(int j = 0 ;j < score.length - i - 1; j++){    //對當前無序區間score[0......length-i-1]進行排序(j的範圍很關鍵,這個範圍是在逐步縮小的)margin

6                  if(score[j] >score[j + 1]){    //把大的值交換到後面static

7                      int temp = score[j];

8                      score[j] = score[j + 1];

9                      score[j + 1] = temp;

10                  }

11              }            

17          }

18              System.out.print("最終排序結果:");

19              for(int a = 0; a < score.length; a++){

20                  System.out.print(score[a] + "\t");

21         }

22      }

23  }

相關文章
相關標籤/搜索