Share
Sign In
📄

Bubble Sort

Best Case : \Omega(n)
Average Case : \Theta(n^2)
Worst Case : O(n^2)
우리말로 버블 정렬이라고 하는 Bubble Sort 알고리즘은 앞에서부터 2개씩 서로를 비교해가는 것이다.
[출처] : ESSLAB "Algorithm" Lecture Note - Elementary Sorting
1.
1번째 원소와 2번째 원소를 비교한다.
2.
이때 앞의 원소가 더 크면 서로를 swap한다.
3.
1~2의 과정을 원소의 개수만큼 반복한다.
4.
1~3의 과정을 원소의 개수만큼 반복한다.
N개의 원소를 갖는 배열에서 1~2의 과정을 N번 반복하게 되면 맨 마지막 원소만 정렬되게 된다. 이를 다시 원소의 개수만큼 정렬시키면 모든 원소가 정렬되게 된다.
Pseudo Code
BUBBLE-SORT(A) for i=0 to n-1 for j=0 to n-1-i if leftElement > rightElement do swap(leftElement, rightElement) end
C Code
void bubbleSort(int *A, int n){ int im j,temp; for(i=n-1;i>0;i--){ for(j=0;j<i;j++){ /*compare neighboring elements*/ if(A[j]>A[j+1]){ /*Swap(A[j],A[j+1])*/ temp= A[j]; A[j] = A[j+1]; A[j+1] = temp; } } }
Time Complexity
Bubble의 모든 경우에서의 시간 복잡도는 다음과 같다.
Best Case : \Omega(n)
Average Case : \Theta(n^2)
Worst Case : O(n^2)
Proof.
Worst Case 에서의 시간 복잡도를 증명하는 방법은 이전 내용에서 진행한 Selection Sort의 시간복잡도 증명 방법과 동일하다.
Selection Sort
만개의 숫자를 input.txt 입력받아 정렬하는 C code
#include <stdio.h> #include <stdlib.h> #define _CRT_SECURE_NO_WARNINGS #pragma warning(disable :4996) #define max_len 10000 void main (){ int dataBox[max_len]; int max,temp,index,i,n,E; E=0; //file input FILE* fin; fin = fopen("input.txt","r"); if (fin == NULL) { printf("Fail to read file. \n\nCheck if the \"input.txt\" file exists in the directory.\n\n"); E=1; } for(i=0; i<max_len ;i++){ fscanf(fin,"%d",&dataBox[i]); } fclose(fin); //bubble Sort n = max_len; for(i=n-1;i>0;i--){ for(index=0;index<i;index++){ if(dataBox[index]>dataBox[index+1]){ temp= dataBox[index]; dataBox[index] = dataBox[index+1]; dataBox[index+1]=temp; } } } //Check if there is a valid value inside the array and display it in the console. // for(i=0; i<max_len; i++){ // printf("%d ",dataBox[i]); // } // if (dataBox[0] < dataBox[max_len-1]&&E==0) { printf("Sorting is complete.\n\nOpen the output file to check the results. \n\n"); } //file output FILE* fout; fout = fopen("output.txt", "w+"); for(i=0; i<max_len; i++){ fprintf(fout,"%d ",dataBox[i]); } fclose(fout); }
Best Case : \Omega(n)
Average Case : \Theta(n^2)
Worst Case : O(n^2)
우리말로 버블 정렬이라고 하는 Bubble Sort 알고리즘은 앞에서부터 2개씩 서로를 비교해가는 것이다.
[출처] : ESSLAB "Algorithm" Lecture Note - Elementary Sorting
1.
1번째 원소와 2번째 원소를 비교한다.
2.
이때 앞의 원소가 더 크면 서로를 swap한다.
3.
1~2의 과정을 원소의 개수만큼 반복한다.
4.
1~3의 과정을 원소의 개수만큼 반복한다.
N개의 원소를 갖는 배열에서 1~2의 과정을 N번 반복하게 되면 맨 마지막 원소만 정렬되게 된다. 이를 다시 원소의 개수만큼 정렬시키면 모든 원소가 정렬되게 된다.
Pseudo Code
BUBBLE-SORT(A) for i=0 to n-1 for j=0 to n-1-i if leftElement > rightElement do swap(leftElement, rightElement) end
C Code
void bubbleSort(int *A, int n){ int im j,temp; for(i=n-1;i>0;i--){ for(j=0;j<i;j++){ /*compare neighboring elements*/ if(A[j]>A[j+1]){ /*Swap(A[j],A[j+1])*/ temp= A[j]; A[j] = A[j+1]; A[j+1] = temp; } } }
Time Complexity
Bubble의 모든 경우에서의 시간 복잡도는 다음과 같다.
Best Case : \Omega(n)
Average Case : \Theta(n^2)
Worst Case : O(n^2)
Proof.
Worst Case 에서의 시간 복잡도를 증명하는 방법은 이전 내용에서 진행한 Selection Sort의 시간복잡도 증명 방법과 동일하다.
Selection Sort
만개의 숫자를 input.txt 입력받아 정렬하는 C code
#include <stdio.h> #include <stdlib.h> #define _CRT_SECURE_NO_WARNINGS #pragma warning(disable :4996) #define max_len 10000 void main (){ int dataBox[max_len]; int max,temp,index,i,n,E; E=0; //file input FILE* fin; fin = fopen("input.txt","r"); if (fin == NULL) { printf("Fail to read file. \n\nCheck if the \"input.txt\" file exists in the directory.\n\n"); E=1; } for(i=0; i<max_len ;i++){ fscanf(fin,"%d",&dataBox[i]); } fclose(fin); //bubble Sort n = max_len; for(i=n-1;i>0;i--){ for(index=0;index<i;index++){ if(dataBox[index]>dataBox[index+1]){ temp= dataBox[index]; dataBox[index] = dataBox[index+1]; dataBox[index+1]=temp; } } } //Check if there is a valid value inside the array and display it in the console. // for(i=0; i<max_len; i++){ // printf("%d ",dataBox[i]); // } // if (dataBox[0] < dataBox[max_len-1]&&E==0) { printf("Sorting is complete.\n\nOpen the output file to check the results. \n\n"); } //file output FILE* fout; fout = fopen("output.txt", "w+"); for(i=0; i<max_len; i++){ fprintf(fout,"%d ",dataBox[i]); } fclose(fout); }
Best Case : \Omega(n)
Average Case : \Theta(n^2)
Worst Case : O(n^2)
우리말로 버블 정렬이라고 하는 Bubble Sort 알고리즘은 앞에서부터 2개씩 서로를 비교해가는 것이다.
[출처] : ESSLAB "Algorithm" Lecture Note - Elementary Sorting
1.
1번째 원소와 2번째 원소를 비교한다.
2.
이때 앞의 원소가 더 크면 서로를 swap한다.
3.
1~2의 과정을 원소의 개수만큼 반복한다.
4.
1~3의 과정을 원소의 개수만큼 반복한다.
N개의 원소를 갖는 배열에서 1~2의 과정을 N번 반복하게 되면 맨 마지막 원소만 정렬되게 된다. 이를 다시 원소의 개수만큼 정렬시키면 모든 원소가 정렬되게 된다.
Pseudo Code
BUBBLE-SORT(A) for i=0 to n-1 for j=0 to n-1-i if leftElement > rightElement do swap(leftElement, rightElement) end
C Code
void bubbleSort(int *A, int n){ int im j,temp; for(i=n-1;i>0;i--){ for(j=0;j<i;j++){ /*compare neighboring elements*/ if(A[j]>A[j+1]){ /*Swap(A[j],A[j+1])*/ temp= A[j]; A[j] = A[j+1]; A[j+1] = temp; } } }
Time Complexity
Bubble의 모든 경우에서의 시간 복잡도는 다음과 같다.
Best Case : \Omega(n)
Average Case : \Theta(n^2)
Worst Case : O(n^2)
Proof.
Worst Case 에서의 시간 복잡도를 증명하는 방법은 이전 내용에서 진행한 Selection Sort의 시간복잡도 증명 방법과 동일하다.
Selection Sort
만개의 숫자를 input.txt 입력받아 정렬하는 C code
#include <stdio.h> #include <stdlib.h> #define _CRT_SECURE_NO_WARNINGS #pragma warning(disable :4996) #define max_len 10000 void main (){ int dataBox[max_len]; int max,temp,index,i,n,E; E=0; //file input FILE* fin; fin = fopen("input.txt","r"); if (fin == NULL) { printf("Fail to read file. \n\nCheck if the \"input.txt\" file exists in the directory.\n\n"); E=1; } for(i=0; i<max_len ;i++){ fscanf(fin,"%d",&dataBox[i]); } fclose(fin); //bubble Sort n = max_len; for(i=n-1;i>0;i--){ for(index=0;index<i;index++){ if(dataBox[index]>dataBox[index+1]){ temp= dataBox[index]; dataBox[index] = dataBox[index+1]; dataBox[index+1]=temp; } } } //Check if there is a valid value inside the array and display it in the console. // for(i=0; i<max_len; i++){ // printf("%d ",dataBox[i]); // } // if (dataBox[0] < dataBox[max_len-1]&&E==0) { printf("Sorting is complete.\n\nOpen the output file to check the results. \n\n"); } //file output FILE* fout; fout = fopen("output.txt", "w+"); for(i=0; i<max_len; i++){ fprintf(fout,"%d ",dataBox[i]); } fclose(fout); }
Best Case : \Omega(n)
Average Case : \Theta(n^2)
Worst Case : O(n^2)
우리말로 버블 정렬이라고 하는 Bubble Sort 알고리즘은 앞에서부터 2개씩 서로를 비교해가는 것이다.
[출처] : ESSLAB "Algorithm" Lecture Note - Elementary Sorting
1.
1번째 원소와 2번째 원소를 비교한다.
2.
이때 앞의 원소가 더 크면 서로를 swap한다.
3.
1~2의 과정을 원소의 개수만큼 반복한다.
4.
1~3의 과정을 원소의 개수만큼 반복한다.
N개의 원소를 갖는 배열에서 1~2의 과정을 N번 반복하게 되면 맨 마지막 원소만 정렬되게 된다. 이를 다시 원소의 개수만큼 정렬시키면 모든 원소가 정렬되게 된다.
Pseudo Code
BUBBLE-SORT(A) for i=0 to n-1 for j=0 to n-1-i if leftElement > rightElement do swap(leftElement, rightElement) end
C Code
void bubbleSort(int *A, int n){ int im j,temp; for(i=n-1;i>0;i--){ for(j=0;j<i;j++){ /*compare neighboring elements*/ if(A[j]>A[j+1]){ /*Swap(A[j],A[j+1])*/ temp= A[j]; A[j] = A[j+1]; A[j+1] = temp; } } }
Time Complexity
Bubble의 모든 경우에서의 시간 복잡도는 다음과 같다.
Best Case : \Omega(n)
Average Case : \Theta(n^2)
Worst Case : O(n^2)
Proof.
Worst Case 에서의 시간 복잡도를 증명하는 방법은 이전 내용에서 진행한 Selection Sort의 시간복잡도 증명 방법과 동일하다.
Selection Sort
만개의 숫자를 input.txt 입력받아 정렬하는 C code
#include <stdio.h> #include <stdlib.h> #define _CRT_SECURE_NO_WARNINGS #pragma warning(disable :4996) #define max_len 10000 void main (){ int dataBox[max_len]; int max,temp,index,i,n,E; E=0; //file input FILE* fin; fin = fopen("input.txt","r"); if (fin == NULL) { printf("Fail to read file. \n\nCheck if the \"input.txt\" file exists in the directory.\n\n"); E=1; } for(i=0; i<max_len ;i++){ fscanf(fin,"%d",&dataBox[i]); } fclose(fin); //bubble Sort n = max_len; for(i=n-1;i>0;i--){ for(index=0;index<i;index++){ if(dataBox[index]>dataBox[index+1]){ temp= dataBox[index]; dataBox[index] = dataBox[index+1]; dataBox[index+1]=temp; } } } //Check if there is a valid value inside the array and display it in the console. // for(i=0; i<max_len; i++){ // printf("%d ",dataBox[i]); // } // if (dataBox[0] < dataBox[max_len-1]&&E==0) { printf("Sorting is complete.\n\nOpen the output file to check the results. \n\n"); } //file output FILE* fout; fout = fopen("output.txt", "w+"); for(i=0; i<max_len; i++){ fprintf(fout,"%d ",dataBox[i]); } fclose(fout); }
Best Case : \Omega(n)
Average Case : \Theta(n^2)
Worst Case : O(n^2)
우리말로 버블 정렬이라고 하는 Bubble Sort 알고리즘은 앞에서부터 2개씩 서로를 비교해가는 것이다.
[출처] : ESSLAB "Algorithm" Lecture Note - Elementary Sorting
1.
1번째 원소와 2번째 원소를 비교한다.
2.
이때 앞의 원소가 더 크면 서로를 swap한다.
3.
1~2의 과정을 원소의 개수만큼 반복한다.
4.
1~3의 과정을 원소의 개수만큼 반복한다.
N개의 원소를 갖는 배열에서 1~2의 과정을 N번 반복하게 되면 맨 마지막 원소만 정렬되게 된다. 이를 다시 원소의 개수만큼 정렬시키면 모든 원소가 정렬되게 된다.
Pseudo Code
BUBBLE-SORT(A) for i=0 to n-1 for j=0 to n-1-i if leftElement > rightElement do swap(leftElement, rightElement) end
C Code
void bubbleSort(int *A, int n){ int im j,temp; for(i=n-1;i>0;i--){ for(j=0;j<i;j++){ /*compare neighboring elements*/ if(A[j]>A[j+1]){ /*Swap(A[j],A[j+1])*/ temp= A[j]; A[j] = A[j+1]; A[j+1] = temp; } } }
Time Complexity
Bubble의 모든 경우에서의 시간 복잡도는 다음과 같다.
Best Case : \Omega(n)
Average Case : \Theta(n^2)
Worst Case : O(n^2)
Proof.
Worst Case 에서의 시간 복잡도를 증명하는 방법은 이전 내용에서 진행한 Selection Sort의 시간복잡도 증명 방법과 동일하다.
Selection Sort
만개의 숫자를 input.txt 입력받아 정렬하는 C code
#include <stdio.h> #include <stdlib.h> #define _CRT_SECURE_NO_WARNINGS #pragma warning(disable :4996) #define max_len 10000 void main (){ int dataBox[max_len]; int max,temp,index,i,n,E; E=0; //file input FILE* fin; fin = fopen("input.txt","r"); if (fin == NULL) { printf("Fail to read file. \n\nCheck if the \"input.txt\" file exists in the directory.\n\n"); E=1; } for(i=0; i<max_len ;i++){ fscanf(fin,"%d",&dataBox[i]); } fclose(fin); //bubble Sort n = max_len; for(i=n-1;i>0;i--){ for(index=0;index<i;index++){ if(dataBox[index]>dataBox[index+1]){ temp= dataBox[index]; dataBox[index] = dataBox[index+1]; dataBox[index+1]=temp; } } } //Check if there is a valid value inside the array and display it in the console. // for(i=0; i<max_len; i++){ // printf("%d ",dataBox[i]); // } // if (dataBox[0] < dataBox[max_len-1]&&E==0) { printf("Sorting is complete.\n\nOpen the output file to check the results. \n\n"); } //file output FILE* fout; fout = fopen("output.txt", "w+"); for(i=0; i<max_len; i++){ fprintf(fout,"%d ",dataBox[i]); } fclose(fout); }
Best Case : \Omega(n)
Average Case : \Theta(n^2)
Worst Case : O(n^2)
우리말로 버블 정렬이라고 하는 Bubble Sort 알고리즘은 앞에서부터 2개씩 서로를 비교해가는 것이다.
[출처] : ESSLAB "Algorithm" Lecture Note - Elementary Sorting
1.
1번째 원소와 2번째 원소를 비교한다.
2.
이때 앞의 원소가 더 크면 서로를 swap한다.
3.
1~2의 과정을 원소의 개수만큼 반복한다.
4.
1~3의 과정을 원소의 개수만큼 반복한다.
N개의 원소를 갖는 배열에서 1~2의 과정을 N번 반복하게 되면 맨 마지막 원소만 정렬되게 된다. 이를 다시 원소의 개수만큼 정렬시키면 모든 원소가 정렬되게 된다.
Pseudo Code
BUBBLE-SORT(A) for i=0 to n-1 for j=0 to n-1-i if leftElement > rightElement do swap(leftElement, rightElement) end
C Code
void bubbleSort(int *A, int n){ int im j,temp; for(i=n-1;i>0;i--){ for(j=0;j<i;j++){ /*compare neighboring elements*/ if(A[j]>A[j+1]){ /*Swap(A[j],A[j+1])*/ temp= A[j]; A[j] = A[j+1]; A[j+1] = temp; } } }
Time Complexity
Bubble의 모든 경우에서의 시간 복잡도는 다음과 같다.
Best Case : \Omega(n)
Average Case : \Theta(n^2)
Worst Case : O(n^2)
Proof.
Worst Case 에서의 시간 복잡도를 증명하는 방법은 이전 내용에서 진행한 Selection Sort의 시간복잡도 증명 방법과 동일하다.
Selection Sort
만개의 숫자를 input.txt 입력받아 정렬하는 C code
#include <stdio.h> #include <stdlib.h> #define _CRT_SECURE_NO_WARNINGS #pragma warning(disable :4996) #define max_len 10000 void main (){ int dataBox[max_len]; int max,temp,index,i,n,E; E=0; //file input FILE* fin; fin = fopen("input.txt","r"); if (fin == NULL) { printf("Fail to read file. \n\nCheck if the \"input.txt\" file exists in the directory.\n\n"); E=1; } for(i=0; i<max_len ;i++){ fscanf(fin,"%d",&dataBox[i]); } fclose(fin); //bubble Sort n = max_len; for(i=n-1;i>0;i--){ for(index=0;index<i;index++){ if(dataBox[index]>dataBox[index+1]){ temp= dataBox[index]; dataBox[index] = dataBox[index+1]; dataBox[index+1]=temp; } } } //Check if there is a valid value inside the array and display it in the console. // for(i=0; i<max_len; i++){ // printf("%d ",dataBox[i]); // } // if (dataBox[0] < dataBox[max_len-1]&&E==0) { printf("Sorting is complete.\n\nOpen the output file to check the results. \n\n"); } //file output FILE* fout; fout = fopen("output.txt", "w+"); for(i=0; i<max_len; i++){ fprintf(fout,"%d ",dataBox[i]); } fclose(fout); }
Best Case : \Omega(n)
Average Case : \Theta(n^2)
Worst Case : O(n^2)
우리말로 버블 정렬이라고 하는 Bubble Sort 알고리즘은 앞에서부터 2개씩 서로를 비교해가는 것이다.
[출처] : ESSLAB "Algorithm" Lecture Note - Elementary Sorting
1.
1번째 원소와 2번째 원소를 비교한다.
2.
이때 앞의 원소가 더 크면 서로를 swap한다.
3.
1~2의 과정을 원소의 개수만큼 반복한다.
4.
1~3의 과정을 원소의 개수만큼 반복한다.
N개의 원소를 갖는 배열에서 1~2의 과정을 N번 반복하게 되면 맨 마지막 원소만 정렬되게 된다. 이를 다시 원소의 개수만큼 정렬시키면 모든 원소가 정렬되게 된다.
Pseudo Code
BUBBLE-SORT(A) for i=0 to n-1 for j=0 to n-1-i if leftElement > rightElement do swap(leftElement, rightElement) end
C Code
void bubbleSort(int *A, int n){ int im j,temp; for(i=n-1;i>0;i--){ for(j=0;j<i;j++){ /*compare neighboring elements*/ if(A[j]>A[j+1]){ /*Swap(A[j],A[j+1])*/ temp= A[j]; A[j] = A[j+1]; A[j+1] = temp; } } }
Time Complexity
Bubble의 모든 경우에서의 시간 복잡도는 다음과 같다.
Best Case : \Omega(n)
Average Case : \Theta(n^2)
Worst Case : O(n^2)
Proof.
Worst Case 에서의 시간 복잡도를 증명하는 방법은 이전 내용에서 진행한 Selection Sort의 시간복잡도 증명 방법과 동일하다.
Selection Sort
만개의 숫자를 input.txt 입력받아 정렬하는 C code
#include <stdio.h> #include <stdlib.h> #define _CRT_SECURE_NO_WARNINGS #pragma warning(disable :4996) #define max_len 10000 void main (){ int dataBox[max_len]; int max,temp,index,i,n,E; E=0; //file input FILE* fin; fin = fopen("input.txt","r"); if (fin == NULL) { printf("Fail to read file. \n\nCheck if the \"input.txt\" file exists in the directory.\n\n"); E=1; } for(i=0; i<max_len ;i++){ fscanf(fin,"%d",&dataBox[i]); } fclose(fin); //bubble Sort n = max_len; for(i=n-1;i>0;i--){ for(index=0;index<i;index++){ if(dataBox[index]>dataBox[index+1]){ temp= dataBox[index]; dataBox[index] = dataBox[index+1]; dataBox[index+1]=temp; } } } //Check if there is a valid value inside the array and display it in the console. // for(i=0; i<max_len; i++){ // printf("%d ",dataBox[i]); // } // if (dataBox[0] < dataBox[max_len-1]&&E==0) { printf("Sorting is complete.\n\nOpen the output file to check the results. \n\n"); } //file output FILE* fout; fout = fopen("output.txt", "w+"); for(i=0; i<max_len; i++){ fprintf(fout,"%d ",dataBox[i]); } fclose(fout); }
Best Case : \Omega(n)
Average Case : \Theta(n^2)
Worst Case : O(n^2)
우리말로 버블 정렬이라고 하는 Bubble Sort 알고리즘은 앞에서부터 2개씩 서로를 비교해가는 것이다.
[출처] : ESSLAB "Algorithm" Lecture Note - Elementary Sorting
1.
1번째 원소와 2번째 원소를 비교한다.
2.
이때 앞의 원소가 더 크면 서로를 swap한다.
3.
1~2의 과정을 원소의 개수만큼 반복한다.
4.
1~3의 과정을 원소의 개수만큼 반복한다.
N개의 원소를 갖는 배열에서 1~2의 과정을 N번 반복하게 되면 맨 마지막 원소만 정렬되게 된다. 이를 다시 원소의 개수만큼 정렬시키면 모든 원소가 정렬되게 된다.
Pseudo Code
BUBBLE-SORT(A) for i=0 to n-1 for j=0 to n-1-i if leftElement > rightElement do swap(leftElement, rightElement) end
C Code
void bubbleSort(int *A, int n){ int im j,temp; for(i=n-1;i>0;i--){ for(j=0;j<i;j++){ /*compare neighboring elements*/ if(A[j]>A[j+1]){ /*Swap(A[j],A[j+1])*/ temp= A[j]; A[j] = A[j+1]; A[j+1] = temp; } } }
Time Complexity
Bubble의 모든 경우에서의 시간 복잡도는 다음과 같다.
Best Case : \Omega(n)
Average Case : \Theta(n^2)
Worst Case : O(n^2)
Proof.
Worst Case 에서의 시간 복잡도를 증명하는 방법은 이전 내용에서 진행한 Selection Sort의 시간복잡도 증명 방법과 동일하다.
Selection Sort
만개의 숫자를 input.txt 입력받아 정렬하는 C code
#include <stdio.h> #include <stdlib.h> #define _CRT_SECURE_NO_WARNINGS #pragma warning(disable :4996) #define max_len 10000 void main (){ int dataBox[max_len]; int max,temp,index,i,n,E; E=0; //file input FILE* fin; fin = fopen("input.txt","r"); if (fin == NULL) { printf("Fail to read file. \n\nCheck if the \"input.txt\" file exists in the directory.\n\n"); E=1; } for(i=0; i<max_len ;i++){ fscanf(fin,"%d",&dataBox[i]); } fclose(fin); //bubble Sort n = max_len; for(i=n-1;i>0;i--){ for(index=0;index<i;index++){ if(dataBox[index]>dataBox[index+1]){ temp= dataBox[index]; dataBox[index] = dataBox[index+1]; dataBox[index+1]=temp; } } } //Check if there is a valid value inside the array and display it in the console. // for(i=0; i<max_len; i++){ // printf("%d ",dataBox[i]); // } // if (dataBox[0] < dataBox[max_len-1]&&E==0) { printf("Sorting is complete.\n\nOpen the output file to check the results. \n\n"); } //file output FILE* fout; fout = fopen("output.txt", "w+"); for(i=0; i<max_len; i++){ fprintf(fout,"%d ",dataBox[i]); } fclose(fout); }
Best Case : \Omega(n)
Average Case : \Theta(n^2)
Worst Case : O(n^2)
우리말로 버블 정렬이라고 하는 Bubble Sort 알고리즘은 앞에서부터 2개씩 서로를 비교해가는 것이다.
[출처] : ESSLAB "Algorithm" Lecture Note - Elementary Sorting
1.
1번째 원소와 2번째 원소를 비교한다.
2.
이때 앞의 원소가 더 크면 서로를 swap한다.
3.
1~2의 과정을 원소의 개수만큼 반복한다.
4.
1~3의 과정을 원소의 개수만큼 반복한다.
N개의 원소를 갖는 배열에서 1~2의 과정을 N번 반복하게 되면 맨 마지막 원소만 정렬되게 된다. 이를 다시 원소의 개수만큼 정렬시키면 모든 원소가 정렬되게 된다.
Pseudo Code
BUBBLE-SORT(A) for i=0 to n-1 for j=0 to n-1-i if leftElement > rightElement do swap(leftElement, rightElement) end
C Code
void bubbleSort(int *A, int n){ int im j,temp; for(i=n-1;i>0;i--){ for(j=0;j<i;j++){ /*compare neighboring elements*/ if(A[j]>A[j+1]){ /*Swap(A[j],A[j+1])*/ temp= A[j]; A[j] = A[j+1]; A[j+1] = temp; } } }