Java中的冒泡排序示例| Java Bubble Sort Tutorial


Java中的冒泡排序示例| Java Bubble Sort Tutorial是今天的主题。冒泡排序是对数组或整数列表进行排序的最简单方法。这种排序方法也称为下沉排序。冒泡排序是最直接的排序算法,如果它们的顺序错误,可以反复交易所相邻的元素。我已经介绍了像MergeSort,QuickSort,InsertionSort和Selection Sort这样的Java排序。

内容概述

  • 1冒泡用Java排序
  • 2#如何运作:
  • 3#算法
  • 4#示例说明
    • 4.1 #First Pass
    • 4.2 #Second Pass
    • 4.3 #Tird通行证
  • 5 #Complexity分析:
  • 6 #Optimized泡泡排序解决方案

冒泡用Java排序

我们可以创建java程序来使用冒泡排序对数组元素进行排序。冒泡排序算法被称为最直接的排序算法。

在冒泡排序算法中,数组从第一个元素遍历到最后一个元素。这里,将当前项与阵列的下一项进行比较。如果当前元素大于下一个元素,则交易所它。

#这个怎么运作:

在冒泡排序中,我们首先采用两个循环,一个循环用于逐个数组生成值,另一个循环用于遍历数组。第二个循环是第一个循环的嵌套循环。

该嵌套循环完成所有工作,它将其当前值与下一个邻居值进行比较,如果找到较小的一个,则将其与当前值交易所。

我们将通过一个例子来理解这一点。

#算法

步骤1:输入N个数字作为数组arr

步骤2:重复步骤2到5 For(i = 0; i

Step3:For(j = 0; j

Step4:if(arr(j)> arr(j + 1)

Step5:swap(arr(j),arr(j + 1))

第6步:结束

#Example with explanation

我们来看一个数组:

{} 45,24,64,2,10

我们将在其中应用冒泡排序……我们只会看到交易所的执行位置。

#First Pass

{45,24,64,2,10} – > {24,45,64,2,10}

{24,45,64,2,10} – > {24,45,2,64,10}

{24,45,2,64,10} – > {24,45,2,10,64}

#Second Pass

{24,45,2,10,64} – > {24,2,45,10,64}

{24,2,45,10,64} – > {24,2,10,45,64}

#Third Pass

{24,2,10,45,64} – > {2,24,10,45,64}

{2,24,10,45,64} – > {2,10,24,45,64}

像这样,第四遍也将被执行,但不会进行交易所作为我们在第三遍中获得的最终排序数组。

最终排序的数组是:{2,10,24,45,64}

现在我们将看到冒泡排序程序。

import java.util.*;  class Bubble {   public static void main(String args()) {     Scanner s = new Scanner(System.in);     System.out.println("nEnter the length of the array:");     int len = s.nextInt();     int() arr = new int(len);     System.out.println("Enter the elements of the array:n");     for (int i = 0; i < len; i++) {       arr(i) = s.nextInt();     }     // Performing Bubble Sort Now      for (int i = 0; i < len - 1; i++) {       for (int j = 0; j < len - i - 1; j++) {         // checking if previous value is         // grater than next one or not         if (arr(j) > arr(j + 1)) {           // temp will temporarly store           // the value of arr(j)           // then we will swap the values           int temp = arr(j);           arr(j) = arr(j + 1);           arr(j + 1) = temp;         }       }     }      System.out.println("Array after sorting is:n");     for (int i = 0; i < len; i++)       System.out.print(arr(i) + " ");     System.out.println("n");   } } 

请参阅以下输出。

在Java示例中冒泡排序

#Complexity分析:

一般来说,因为我们有N个数字,所以我们必须这样做,

(N-1)+(N-2)+(N-3)+ ... + 2 + 1 =((N-1)* N)/ 2比较。

因此,算法的复杂性为O(n ^ 2)

这是针对最坏情况和平均情况的

最佳案例的复杂性是O(n)

#Optimized泡泡排序解决方案

正如我们上面所看到的,即使在任何先前的传递中获得最终排序的数组之后,程序仍继续运行。但程序将一直运行,直到完成n-1次传递。因此,当我们得到最终排序的数组时,我们将制作一个程序将停止的程序。请参阅以下优化解决方案的程序。

请参阅以下程序。

import java.util.*;  class Bubble {   public static void main(String args()) {     Scanner s = new Scanner(System.in);     System.out.println("nEnter the length of the array:");     int len = s.nextInt();     int() arr = new int(len);     System.out.println("Enter the elements of the array:n");     for (int i = 0; i < len; i++) {       arr(i) = s.nextInt();     }     // Performing Bubble Sort Now     int flag = 1;     // using flag variable to check if there is any swap inside the nested loop     // if not, then the outer loop will stop executing     // and that time we will get our final sorted value too     for (int i = 0; i < len - 1 && flag == 1; i++) {       flag = 0;       for (int j = 0; j < len - i - 1; j++) {         // checking if previous value is         // grater than next one or not         if (arr(j) > arr(j + 1)) {           // temp will temporarly store           // the value of arr(j)           // then we will swap the values           int temp = arr(j);           arr(j) = arr(j + 1);           arr(j + 1) = temp;           // changing flag variable to 1           flag = 1;         }       }     }      System.out.println("Array after sorting is:n");     for (int i = 0; i < len; i++)       System.out.print(arr(i) + " ");     System.out.println("n");   } } 

请参阅以下输出。

Java Bubble Sort Tutorial

最后,冒泡排序在Java示例中| Java Bubble Sort Tutorial结束了。

资讯来源:由0x资讯编译自APPDIVIDEND,版权归作者Ankit Lathiya所有,未经许可,不得转载
你可能还喜欢