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>

이 함수에는 두 개의 루프가 있습니다. 외부 루프는 배열의 첫 번째 요소부터 두 번째 마지막 요소까지 실행됩니다. 내부 루프는 배열의 정렬되지 않은 부분의 첫 번째 요소부터 두 번째 마지막 요소까지 실행됩니다. 내부 루프의 조건은 n - i - 1입니다. 배열의 마지막 i 요소가 이미 정렬되어 있기 때문입니다.

내부 루프의 각 반복에서 인접한 요소를 비교합니다. 왼쪽 요소가 오른쪽 요소보다 크면 서로 바꿉니다. 내부 루프가 완료된 후 가장 큰 요소는 배열의 정렬되지 않은 부분 끝에 있게 됩니다.

이제 버블 정렬 구현을 테스트하기 위해 기본 함수를 작성할 수 있습니다. 이전 부분과 함께 주요 기능은 다음과 같습니다.

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(n)의 시간복잡도를 갖습니다.2) 최악의 경우, 여기서 n은 배열의 크기입니다.

용법:

  • 버블 정렬은 교육 목적과 소규모 데이터 세트에 유용합니다.
  • 시간 복잡도로 인해 대규모 데이터 세트에는 적합하지 않습니다.

장점:

  • 버블 정렬은 이해하고 구현하기 쉽습니다.
  • 정렬을 수행하려면 최소한의 추가 메모리 공간이 필요합니다.

단점:

  • 시간 복잡성으로 인해 대규모 데이터 세트에는 효율적이지 않습니다.
  • 퀵정렬, 병합정렬 등 다른 정렬 알고리즘에 비해 성능이 좋지 않습니다.

결론:

버블 정렬은 교육 목적과 소규모 데이터 세트에 유용한 간단하고 직관적인 정렬 알고리즘입니다. 그러나 시간 복잡성으로 인해 대규모 데이터 세트에는 비효율적입니다. 따라서 실제 응용 프로그램에서는 일반적으로 사용되지 않습니다. Quicksort 및 mergesort와 같은 다른 정렬 알고리즘은 대규모 데이터 세트에 더 효율적입니다.