バブルソートアルゴリズムを理解する

    このチュートリアルでは、バブルソートがどのように機能するかを学びます。さらに、C言語でのバブルソートの例もあります。
    バブルソートは、隣接する要素を比較するアルゴリズムであり、それらが所定の順序に準拠していない場合は、それらの位置を交換します。順序は昇順または降順です。

バブルソートはどのように機能しますか?
  1. 最初のインデックスから始めて、最初の要素と2番目の要素が比較され、最初の要素が2番目の要素より大きい場合、それらは交換されます。
    次に、2番目と3番目の要素を比較します。順序が正しくない場合は、交換してください。
    最後の要素まで上記のプロセスを続けます。
    ここに画像の説明を挿入します
  2. 残りの反復では、同じプロセスが続行されます。各反復の後、ソートされていない要素の中で最大の要素が最後に配置されます。(たとえば、上の図の最初の反復を完了した後、ソートされた要素は45で、ソートされていない要素は-2、0、11、-9です)
    各反復で、比較は最後のソートされていない要素まで続行されます。
    並べ替えられていないすべての要素が正しい位置に配置されると、配列が並べ替えられます。
    ここに画像の説明を挿入します
    ここに画像の説明を挿入します
    ここに画像の説明を挿入します
バブルソートアルゴリズム
bubbleSort(array)
  for i <- 1 to indexOfLastUnsortedElement-1
    if leftElement > rightElement
      swap leftElement and rightElement
end bubbleSort
Cの例
// Bubble sort in C

#include <stdio.h>

void bubbleSort(int array[], int size) {
    
    

  // run loops two times: one for walking throught the array
  // and the other for comparison
  for (int step = 0; step < size - 1; ++step) {
    
    
    for (int i = 0; i < size - step - 1; ++i) {
    
    
      
      // To sort in descending order, change">" to "<".
      if (array[i] > array[i + 1]) {
    
    
        
        // swap if greater is at the rear position
        int temp = array[i];
        array[i] = array[i + 1];
        array[i + 1] = temp;
      }
    }
  }
}

// function to print the array
void printArray(int array[], int size) {
    
    
  for (int i = 0; i < size; ++i) {
    
    
    printf("%d  ", array[i]);
  }
  printf("\n");
}

// driver code
int main() {
    
    
  int data[] = {
    
    -2, 45, 0, 11, -9};
  int size = sizeof(data) / sizeof(data[0]);
  bubbleSort(data, size);
  printf("Sorted Array in Ascending Order:\n");
  printArray(data, size);
}
バブルソートを最適化する

    上記のコードでは、配列が並べ替えられている場合でも、可能なすべての比較が実行されます。実行時間が長くなります。
    スワップされた追加の変数を導入することにより、コードを最適化できます。各反復の後、交換が発生しない場合、それ以上のループを実行する必要はありません。
    この場合、スワップされた変数はfalseに設定されます。したがって、それ以上の反復を防ぐことができます。
    バブルソートを最適化するためのアルゴリズムは次のとおりです。

bubbleSort(array)
  swapped <- false
  for i <- 1 to indexOfLastUnsortedElement-1
    if leftElement > rightElement
      swap leftElement and rightElement
      swapped <- true
end bubbleSort
Cの例
// Optimized bubble sort in C

#include <stdio.h>

void bubbleSort(int arrayay[], int size) {
    
    
  for (int step = 0; step < size - 1; ++step) {
    
    

    // Swapped keeps track of swapping
    int swapped = 0;

    // Run loops two times: one for walking throught the array
    // and the other for comparison
    for (int i = 0; i < size - step - 1; ++i) {
    
    

      // To sort in descending order, change > to < in this line.
      if (arrayay[i] > arrayay[i + 1]) {
    
    
        
        // Swap if greater is at the rear position
        int temp = arrayay[i];
        arrayay[i] = arrayay[i + 1];
        arrayay[i + 1] = temp;
        swapped = 1;
      }
    }

    // If there is not swapping in the last swap, then the array is already sorted.
    if (swapped == 0)
      break;
  }
}

// Function to print an array
void printarrayay(int arrayay[], int size) {
    
    
  for (int i = 0; i < size; ++i) {
    
    
    printf("%d  ", arrayay[i]);
  }
  printf("\n");
}

// Driver code
int main() {
    
    
  int data[] = {
    
    -2, 45, 0, 11, -9};
  int size = sizeof(data) / sizeof(data[0]);
  bubbleSort(data, size);
  printf("Sorted Array in Ascending Order:\n");
  printarrayay(data, size);
}
複雑さ

    バブルソートは、最も単純なソートアルゴリズムの1つです。アルゴリズムは2つのサイクルを実装します。

サイクル 比較の数
初めて (n-1)
2回目 (n-2)
3回目 (n-3)
…… ……
前回 1

    比較の数(n-1)+(n-2)+(n-3)+…+ 1 = n(n-1)/ 2ほぼn2 n ^ 2に等しいn2
    複雑さ:O(n 2 n ^ 2n2
    さらに、サイクル数を観察するだけで複雑さを分析できます。2つのループがあるため、複雑さはn * n =n 2 n ^ 2です。n2
    時間の複雑さ

  • 最悪の場合の複雑さ:O(n 2 n ^ 2n2
    昇順で並べ替え、配列を降順で並べ替えると、最悪のケースが発生します。
  • 最良の複雑さ:O(n)
    配列がすでに配置されている場合は、ソートする必要はありません。
  • 平均的なケースの複雑さ:O(n 2 n ^ 2n2
    これは、配列の要素の順序が正しくない(昇順でも降順でもない)場合に発生します。

    スペースの複雑さ:
    スワッピング時に追加の可変温度が使用されるため、スペースの複雑さはO(1)です。
    最適化アルゴリズムでは、スワップされた変数によってスペースの複雑さが増し、O(2)になります。

バブルソートアプリケーション

    バブルソートは、次の状況で使用されます。

  1. コードの複雑さは重要ではありません。
  2. ショートコードが推奨されます。
参照文書

[1]パレワラボPvt。Ltd.Bubble Sort Algorithm [EB / OL] .https://www.programiz.com/dsa/bubble-sort,2020-01-01。

おすすめ

転載: blog.csdn.net/zsx0728/article/details/114693658