"거품정렬 구현"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(다른 사용자 한 명의 중간 판 하나는 보이지 않습니다)
11번째 줄: 11번째 줄:
{{참고|C언어 거품정렬 구현}}
{{참고|C언어 거품정렬 구현}}
{{소스헤더|기본형}}
{{소스헤더|기본형}}
<source lang="C">
<syntaxhighlight lang="C">
#include <stdio.h>
#include <stdio.h>
void bubble_sort(int a[], int size) {
void bubble_sort(int a[], int size) {
30번째 줄: 30번째 줄:
// -1 0 1 1 4 9 10 22 22 100  
// -1 0 1 1 4 9 10 22 22 100  
}
}
</source>
</syntaxhighlight>
{{소스헤더|개선형 (swapped 플래그 적용)}}
{{소스헤더|개선형 (swapped 플래그 적용)}}
<source lang='c'>
<syntaxhighlight lang='c'>
#include <stdio.h>
#include <stdio.h>
void bubble_sort(int a[], int size) {
void bubble_sort(int a[], int size) {
54번째 줄: 54번째 줄:
// -1 0 1 1 4 9 10 22 22 100  
// -1 0 1 1 4 9 10 22 22 100  
}
}
</source>
</syntaxhighlight>


== C++ ==
== C++ ==
60번째 줄: 60번째 줄:
{{참고|C++ 거품정렬 구현}}
{{참고|C++ 거품정렬 구현}}
{{소스헤더|기본형}}
{{소스헤더|기본형}}
<source lang="cpp">
<syntaxhighlight lang="cpp">
#include <iostream>
#include <iostream>
void bubble_sort(int a[], int size) {
void bubble_sort(int a[], int size) {
77번째 줄: 77번째 줄:
     // -1 0 1 1 4 9 10 22 22 100  
     // -1 0 1 1 4 9 10 22 22 100  
}
}
</source>
</syntaxhighlight>
{{소스헤더|개선형 (swapped 플래그 적용)}}
{{소스헤더|개선형 (swapped 플래그 적용)}}
<source lang='cpp'>
<syntaxhighlight lang='cpp'>
#include <iostream>
#include <iostream>
void bubble_sort(int a[], int size) {
void bubble_sort(int a[], int size) {
102번째 줄: 102번째 줄:
     // -1 0 1 1 4 9 10 22 22 100  
     // -1 0 1 1 4 9 10 22 22 100  
}
}
</source>
</syntaxhighlight>


==C#==
==C#==
[[분류: Csharp]]
[[분류: Csharp]]
{{참고|C샵 거품정렬 구현}}
{{참고|C샵 거품정렬 구현}}
<source lang='csharp'>
<syntaxhighlight lang='csharp'>
using System;
using System;
class Program {
class Program {
127번째 줄: 127번째 줄:
     }
     }
}
}
</source>
</syntaxhighlight>


== Java ==
== Java ==
133번째 줄: 133번째 줄:
{{참고|Java 거품정렬 구현}}
{{참고|Java 거품정렬 구현}}
{{소스헤더|기본형}}
{{소스헤더|기본형}}
<source lang="java">
<syntaxhighlight lang="java">
public class MyClass {
public class MyClass {
     static void bubble_sort(int[] a) {
     static void bubble_sort(int[] a) {
152번째 줄: 152번째 줄:
     }
     }
}
}
</source>
</syntaxhighlight>
{{소스헤더|개선형 (swapped 플래그 적용)}}
{{소스헤더|개선형 (swapped 플래그 적용)}}
<source lang='java'>
<syntaxhighlight lang='java'>
public class MyClass {
public class MyClass {
     static void bubble_sort(int[] a) {
     static void bubble_sort(int[] a) {
177번째 줄: 177번째 줄:
     }
     }
}
}
</source>
</syntaxhighlight>


==Perl==
==Perl==
[[분류: Perl]]
[[분류: Perl]]
{{참고|Perl 거품정렬 구현}}
{{참고|Perl 거품정렬 구현}}
<source lang='perl'>
<syntaxhighlight lang='perl'>
sub bubble_sort {
sub bubble_sort {
     my $a=shift;
     my $a=shift;
196번째 줄: 196번째 줄:
print join(',',@arr);
print join(',',@arr);
# -1,0,1,1,4,9,10,22,22,100
# -1,0,1,1,4,9,10,22,22,100
</source>
</syntaxhighlight>


==PHP==
==PHP==
202번째 줄: 202번째 줄:
{{참고|PHP 거품정렬 구현}}
{{참고|PHP 거품정렬 구현}}
{{소스헤더|기본형}}
{{소스헤더|기본형}}
<source lang='php'>
<syntaxhighlight lang='php'>
<?php
<?php
function bubble_sort(&$a) {
function bubble_sort(&$a) {
218번째 줄: 218번째 줄:
echo implode(',', $arr);
echo implode(',', $arr);
# -1,0,1,1,4,9,10,22,22,100
# -1,0,1,1,4,9,10,22,22,100
</source>
</syntaxhighlight>
{{소스헤더|개선형 (swapped 플래그 적용)}}
{{소스헤더|개선형 (swapped 플래그 적용)}}
<source lang='php'>
<syntaxhighlight lang='php'>
<?php
<?php
function bubble_sort(&$a) {
function bubble_sort(&$a) {
239번째 줄: 239번째 줄:
echo implode(',', $arr);
echo implode(',', $arr);
# -1,0,1,1,4,9,10,22,22,100
# -1,0,1,1,4,9,10,22,22,100
</source>
</syntaxhighlight>


== Python ==
== Python ==
[[분류: Python]]
[[분류: Python]]
{{참고|Python 거품정렬 구현}}
{{참고|Python 거품정렬 구현}}
<source lang='python'>
<syntaxhighlight lang='python'>
def bubble_sort(a):
def bubble_sort(a):
size = len(a)
size = len(a)
254번째 줄: 254번째 줄:
print( arr )
print( arr )
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]
</source>
</syntaxhighlight>


==Ruby==
==Ruby==
[[분류: Ruby]]
[[분류: Ruby]]
{{참고|Ruby 거품정렬 구현}}
{{참고|Ruby 거품정렬 구현}}
<source lang='ruby'>
<syntaxhighlight lang='ruby'>
def bubble_sort(a)
def bubble_sort(a)
     for i in (0...a.size-1)
     for i in (0...a.size-1)
271번째 줄: 271번째 줄:
print arr
print arr
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]
</source>
</syntaxhighlight>


== Scala==
== Scala==
[[분류: Scala]]
[[분류: Scala]]
<source lang="scala">
<syntaxhighlight lang="scala">
object MyClass {
object MyClass {
def bubble_sort(arr: Array[Int]) =  {
def bubble_sort(arr: Array[Int]) =  {
294번째 줄: 294번째 줄:
}
}
}
}
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==
302번째 줄: 302번째 줄:


==참고==
==참고==
* https://www.interviewbit.com/tutorial/bubble-sort/
* https://www.geeksforgeeks.org/bubble-sort/
* https://www.geeksforgeeks.org/bubble-sort/

2020년 11월 2일 (월) 02:31 기준 최신판

1 개요[ | ]

거품정렬 구현 bubble_sort()
  • 기본형과 개선형이 있음
  • 기본형 정도만 알아두면 될 듯
  • swapped 플래그를 적용하면 평균적인 속도 향상이 약간 있겠으나 대세에는 큰 변화가 없음
성능이 중요하다면 다른 알고리즘 사용을 권장함

2 C[ | ]

기본형
#include <stdio.h>
void bubble_sort(int a[], int size) {
    int i, j, temp;
    for(i=0; i<size-1; i++) {
		for(j=0; j<size-i-1; j++) {
			if(a[j] > a[j+1]) {
				temp=a[j]; a[j]=a[j+1]; a[j+1]=temp;
			}
		}
	}
}
int main() {
	int arr[] = {9,1,22,4,0,-1,1,22,100,10};
	int size = sizeof(arr)/sizeof(int);
	bubble_sort(arr, size);
	for(int i=0; i<size; i++) printf("%d ", arr[i]);
	// -1 0 1 1 4 9 10 22 22 100 
}
개선형 (swapped 플래그 적용)
#include <stdio.h>
void bubble_sort(int a[], int size) {
    int i, j, temp, swapped;
    for(i=0; i<size-1; i++) {
        swapped = 0;
		for(j=0; j<size-i-1; j++) {
			if(a[j] > a[j+1]) {
				temp = a[j]; a[j] = a[j+1]; a[j+1] = temp;
				swapped = 1;
			}
		}
		if( swapped == 0 ) break;
	}
}
int main() {
	int arr[] = {9,1,22,4,0,-1,1,22,100,10};
	int size = sizeof(arr)/sizeof(int);
	bubble_sort(arr, size);
	for(int i=0; i<size; i++) printf("%d ", arr[i]);
	// -1 0 1 1 4 9 10 22 22 100 
}

3 C++[ | ]

기본형
#include <iostream>
void bubble_sort(int a[], int size) {
    int i, j;
    for(i=0; i<size-1; i++) {
    	for(j=0; j<size-i-1; j++) {
    		if(a[j] > a[j+1]) std::swap(a[j], a[j+1]);
    	}
    }
}
int main() {
    int arr[] = {9,1,22,4,0,-1,1,22,100,10};
    int size = sizeof(arr)/sizeof(int);
    bubble_sort(arr, size);
    for(int i=0; i<size; i++) std::cout << arr[i] << " ";
    // -1 0 1 1 4 9 10 22 22 100 
}
개선형 (swapped 플래그 적용)
#include <iostream>
void bubble_sort(int a[], int size) {
    int i, j;
    bool swapped;
    for(i=0; i<size-1; i++) {
        swapped = false;
    	for(j=0; j<size-i-1; j++) {
    		if(a[j] > a[j+1]) {
    			std::swap(a[j], a[j+1]);
    			swapped = true;
    		}
    	}
    	if( !swapped ) break;
    }
}
int main() {
    int arr[] = {9,1,22,4,0,-1,1,22,100,10};
    int size = sizeof(arr)/sizeof(int);
    bubble_sort(arr, size);
    for(int x: arr) std::cout << x << " ";
    // -1 0 1 1 4 9 10 22 22 100 
}

4 C#[ | ]

using System;
class Program {
    static void bubbleSort(int[] a) {
        int i, j, temp, size=a.Length;
        for(i=0; i<size-1; i++) {
            for(j=1; j<size-i; j++) {
                if (a[j-1] > a[j]) {
                    temp=a[j-1]; a[j-1]=a[j]; a[j]=temp;
                }
            }
        }
    }
    static void Main() {
        int[] arr = {9,1,22,4,0,-1,1,22,100,10};
        bubbleSort(arr);
        Console.Write(string.Join(",",arr));
        // -1,0,1,1,4,9,10,22,22,100
    }
}

5 Java[ | ]

기본형
public class MyClass {
    static void bubble_sort(int[] a) {
    	int i, j, temp, size=a.length;
    	for(i=0; i<size-1; i++) {
    		for(j=1; j<size-i; j++) {
    			if(a[j-1] > a[j]) {
    				temp=a[j-1]; a[j-1]=a[j]; a[j]=temp;
    			}
    		}
    	}
    }
    public static void main(String args[]) {
    	int[] arr = {9,1,22,4,0,-1,1,22,100,10};
    	bubble_sort(arr);
    	for(int x: arr) System.out.format( "%d ", x );
    	// -1 0 1 1 4 9 10 22 22 100 
    }
}
개선형 (swapped 플래그 적용)
public class MyClass {
    static void bubble_sort(int[] a) {
    	int i, j, temp, size=a.length;
    	boolean swapped;
    	for(i=0; i<size-1; i++) {
    	    swapped = false;
    		for(j=1; j<size-i; j++) {
    			if(a[j-1] > a[j]) {
    				temp=a[j-1]; a[j-1]=a[j]; a[j]=temp;
    				swapped = true;
    			}
    		}
    		if( !swapped ) break;
    	}
    }
    public static void main(String args[]) {
    	int[] arr = {9,1,22,4,0,-1,1,22,100,10};
    	bubble_sort(arr);
    	for(int x: arr) System.out.format( "%d ", x );
    	// -1 0 1 1 4 9 10 22 22 100 
    }
}

6 Perl[ | ]

sub bubble_sort {
    my $a=shift;
    $size=@$a;
    for $i (0..$size-2) {
        for $j (1..$size-$i-1) {
            (@$a[$j-1],@$a[$j]) = (@$a[$j],@$a[$j-1]) if @$a[$j-1]>@$a[$j];
        }
    }
}
@arr = (9,1,22,4,0,-1,1,22,100,10);
bubble_sort(\@arr);
print join(',',@arr);
# -1,0,1,1,4,9,10,22,22,100

7 PHP[ | ]

기본형
<?php
function bubble_sort(&$a) {
	$size = count($a);
	for($i=0; $i<$size-1; $i++) {
		for($j=1; $j<$size-$i; $j++) {
			if($a[$j-1] > $a[$j]) {
				$temp=$a[$j-1]; $a[$j-1]=$a[$j]; $a[$j]=$temp;
			}
		}
	}
}
$arr = [9,1,22,4,0,-1,1,22,100,10];
bubble_sort( $arr );
echo implode(',', $arr);
# -1,0,1,1,4,9,10,22,22,100
개선형 (swapped 플래그 적용)
<?php
function bubble_sort(&$a) {
	$size = count($a);
	for($i=0; $i<$size-1; $i++) {
		$swapped = false;
		for($j=1; $j<$size-$i; $j++) {
			if($a[$j-1] > $a[$j]) {
				$temp=$a[$j-1]; $a[$j-1]=$a[$j]; $a[$j]=$temp;
				$swapped = true;
			}
		}
		if( !$swapped ) break;
	}
}
$arr = [9,1,22,4,0,-1,1,22,100,10];
bubble_sort( $arr );
echo implode(',', $arr);
# -1,0,1,1,4,9,10,22,22,100

8 Python[ | ]

def bubble_sort(a):
	size = len(a)
	for i in range(size-1):
		for j in range(size-i-1):
			if a[j]>a[j+1]: a[j],a[j+1] = a[j+1],a[j]
arr = [9,1,22,4,0,-1,1,22,100,10]
bubble_sort(arr)
print( arr )
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]

9 Ruby[ | ]

def bubble_sort(a)
    for i in (0...a.size-1)
        for j in (0...a.size-i-1)
            a[j],a[j+1] = a[j+1],a[j] if a[j]>a[j+1]
        end
    end    
end
arr = [9,1,22,4,0,-1,1,22,100,10]
bubble_sort(arr)
print arr
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]

10 Scala[ | ]

object MyClass {
	def bubble_sort(arr: Array[Int]) =  {
		var temp, size = arr.length
		for(i<-0 until size; j<-1 until size-i) {
			if(arr(j-1) > arr(j)) {
				temp = arr(j-1)
				arr(j-1) = arr(j)
				arr(j) = temp
			}
		}
	}
	def main(args: Array[String]) {
	    var arr = Array(3,4,2,1,7,5,8,9,0,6,100,10)
	    bubble_sort( arr )
	    print( arr.mkString(" ") )
	    // 0 1 2 3 4 5 6 7 8 9 10 100
	}
}

11 같이 보기[ | ]

12 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}