logo

C のバブルソートプログラム

バブルソート シンプルで直感的な並べ替えアルゴリズムです。配列がソートされるまで、隣接する要素の順序が間違っている場合は繰り返し交換します。このアルゴリズムでは、反復ごとに最大の要素が配列の最後まで「バブルアップ」します。バブル ソートは大規模なデータ セットでは非効率的ですが、教育目的や小規模なデータ セットには役立ちます。この記事では、C プログラミング言語でバブル ソート アルゴリズムを実装します。

最初のステップは、バブル ソート関数を定義することです。この関数は、整数配列とその配列のサイズをパラメーターとして受け取ります。この関数は元の配列を変更するため、何も返しません。ここにあります 関数定義:

多重化
 void bubble_sort(int arr[], int n) { int i, j; for (i = 0; i <n - 1; i++) { for (j="0;" j <n i j++) if (arr[j]> arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } </n>

この関数には 2 つのループがあります。外側のループは、配列の最初の要素から最後から 2 番目の要素まで実行されます。内部ループは、配列の未ソート部分の最初の要素から最後から 2 番目の要素まで実行されます。配列の最後の i 要素はすでにソートされているため、内側のループの条件は n - i - 1 になります。

内側のループの各反復で、隣接する要素を比較します。左側の要素が右側の要素より大きい場合、それらを交換します。内側のループが完了すると、最大の要素が配列の未ソート部分の末尾にあることが保証されます。

これで、バブル ソートの実装をテストする main 関数を作成できます。前の部分と合わせて main 関数を次に示します。

C プログラム:

 #include void bubble_sort(int arr[], int n) { int i, j; for (i = 0; i <n - 1; i++) { for (j="0;" j <n i j++) if (arr[j]> arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } int main() { int arr[] = {64, 34, 25, 12, 22, 11, 90}; int n = sizeof(arr) / sizeof(arr[0]); bubble_sort(arr, n); printf(&apos;Sorted array: &apos;); for (int i = 0; i <n; i++) { printf('%d ', arr[i]); } return 0; < pre> <p>The main function creates an integer array arr of size 7 and initializes it with random numbers. We then calculate the size of the array by dividing the size of the array by the size of an integer element. Next, we call the bubble_sort function to sort the array. Finally, we print the sorted array using a for loop.</p> <p> <strong>When we run the program, we should see the following output:</strong> </p> <pre> Sorted array: 11 12 22 25 34 64 90 </pre> <p>This output shows that our bubble sort implementation correctly sorted the array in ascending order.</p> <p>To run the program, we need to compile it using a C compiler. Here is an example <strong>compilation command for GCC:</strong> </p> <pre> gcc -o bubble_sort bubble_sort.c </pre> <p>This command compiles the bubble_sort.c file and produces an executable file named bubble_sort.</p> <p>In summary, the bubble sort algorithm repeatedly swaps adjacent elements until the array is sorted. The algorithm has a time complexity of O(n<sup>2</sup>), which makes it inefficient for large data sets. However, it is useful for educational purposes and small data sets. We implemented the bubble sort algorithm in C programming language and tested it using a simple example.</p> <h3>Characteristics:</h3> <ul> <li>Bubble sort is a simple sorting algorithm.</li> <li>It works by repeatedly swapping adjacent elements if they are in the wrong order.</li> <li>The algorithm sorts the array in ascending or descending order.</li> <li>It has a time complexity of O(n<sup>2</sup>) in the worst case, where n is the size of the array.</li> </ul> <h3>Usage:</h3> <ul> <li>Bubble sort is useful for educational purposes and small data sets.</li> <li>It is not suitable for large data sets because of its time complexity.</li> </ul> <h3>Advantages:</h3> <ul> <li>Bubble sort is easy to understand and implement.</li> <li>It requires minimal additional memory space to perform the sorting.</li> </ul> <h3>Disadvantages:</h3> <ul> <li>It is not efficient for large data sets because of its time complexity.</li> <li>It has poor performance compared to other sorting algorithms, such as quicksort and mergesort.</li> </ul> <h2>Conclusion:</h2> <p>Bubble sort is a simple and intuitive sorting algorithm that is useful for educational purposes and small data sets. However, its time complexity makes it inefficient for large data sets. Therefore, it is not commonly used in real-world applications. Other sorting algorithms, such as quicksort and mergesort, are more efficient for large data sets.</p> <hr></n;></n>

この出力は、バブル ソート実装が配列を昇順に正しくソートしたことを示しています。

プログラムを実行するには、C コンパイラを使用してコンパイルする必要があります。ここに例があります GCC のコンパイル コマンド:

 gcc -o bubble_sort bubble_sort.c 

このコマンドは bubble_sort.c ファイルをコンパイルし、bubble_sort という名前の実行可能ファイルを生成します。

要約すると、バブル ソート アルゴリズムは、配列がソートされるまで隣接する要素を繰り返し交換します。このアルゴリズムの時間計算量は O(n2)、大規模なデータセットの場合は非効率的になります。ただし、教育目的や小規模なデータ セットには役立ちます。バブル ソート アルゴリズムを C プログラミング言語で実装し、簡単な例を使用してテストしました。

ページダウンキーボード

特徴:

  • バブル ソートは単純な並べ替えアルゴリズムです。
  • これは、隣接する要素の順序が間違っている場合に繰り返し交換することで機能します。
  • このアルゴリズムは配列を昇順または降順に並べ替えます。
  • 時間計算量は O(n2) 最悪の場合。n は配列のサイズです。

使用法:

  • バブル ソートは、教育目的や小規模なデータ セットに役立ちます。
  • 時間がかかるため、大規模なデータセットには適していません。

利点:

  • バブルソートは理解しやすく、実装も簡単です。
  • ソートを実行するには最小限の追加メモリスペースが必要です。

短所:

  • 時間がかかるため、大規模なデータ セットの場合は効率的ではありません。
  • クイックソートやマージソートなどの他の並べ替えアルゴリズムと比較すると、パフォーマンスが劣ります。

結論:

バブル ソートは、教育目的や小規模なデータ セットに役立つ、シンプルで直感的な並べ替えアルゴリズムです。ただし、時間の複雑さにより、大規模なデータセットの場合は非効率になります。したがって、実際のアプリケーションでは一般的に使用されません。クイックソートやマージソートなどの他の並べ替えアルゴリズムは、大規模なデータ セットの場合により効率的です。