冒泡排序程序java_冒泡排序Java程序

  • Post author:
  • Post category:java


冒泡排序程序java


Bubble sort

in java is the simplest sorting technique. In bubble sort algorithm each element is compared to next adjacent element, if the next element is smaller, then it will be swapped by previous element. Although this technique is simple but it is too slow as compared to other

冒泡排序

是最简单的排序技术。 在冒泡排序算法中,将每个元素与下一个相邻元素进行比较,如果下一个元素较小,则将被前一个元素交换。 尽管此技术很简单,但与其他

sorting

techniques. Below I have shared the bubble sort java program. If you find anything missing or incorrect then please mention it in comment section. You can also ask your queries.

排序

技术相比太慢了。 下面我分享了冒泡排序java程序。 如果您发现任何缺失或不正确的内容,请在评论部分中提及。 您也可以询问您的问题。

冒泡排序Java程序

(

Bubble Sort Java Program

)

import java.util.Scanner;
 
class BubbleSort
{
 public static void main(String...s)
 {
  int a[]=new int[20],n,i,j,temp;
 
  Scanner sc=new Scanner(System.in);
  System.out.println("Enter how many elements:");
  n=sc.nextInt();
 
  System.out.println("nEnter elements of array:");
  
  for(i=0;i<n;++i)
   a[i]=sc.nextInt();
 
  for(i=1;i<n;++i)
   for(j=0;j<n-i;++j)
   {
    if(a[j]>a[j+1])
    {
     temp=a[j];
     a[j]=a[j+1];
     a[j+1]=temp;
    }
   }
 
  System.out.println("nArray after bubble sort:");
 
  for(i=0;i<n;++i)
   System.out.print(a[i]+" "); 
 }
}
Bubble Sort Java Program

翻译自:

https://www.thecrazyprogrammer.com/2015/04/bubble-sort-java-program.html

冒泡排序程序java