퀵정렬 구현

Jmnote (토론 | 기여)님의 2021년 9월 24일 (금) 23:33 판 (→‎Ruby)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 C[ | ]

#include <stdio.h>
void quick_sort(int a[], int L, int R) {
    int left, right, pivot=a[L];
    for(left=L,right=R; left<right; right--) {
        while( a[right]>=pivot && left<right ) right--;
        if( left<right ) a[left] = a[right];
        while( a[left]<=pivot && left<right ) left++;
        if( left>=right ) break;
        a[right] = a[left];
    }
    a[left]=pivot;
    if(left>L) quick_sort(a, L, left-1);
    if(left<R) quick_sort(a, left+1, R);
}
int main() {
	int arr[] = {9,1,22,4,0,-1,1,22,100,10};
	int size = sizeof(arr)/sizeof(int);
	quick_sort(arr, 0, size-1);
	for(int i=0; i<size; i++) printf("%d ", arr[i]); // -1 0 1 1 4 9 10 22 22 100
}

2 C++[ | ]

#include <iostream>
void quick_sort(int a[], int L, int R) {
    int left, right, pivot=a[L];
    for(left=L,right=R; left<right; right--) {
        while( a[right]>=pivot && left<right ) right--;
        if( left<right ) a[left] = a[right];
        while( a[left]<=pivot && left<right ) left++;
        if( left>=right ) break;
        a[right] = a[left];
    }
    a[left]=pivot;
    if(left>L) quick_sort(a, L, left-1);
    if(left<R) quick_sort(a, left+1, R);
}
int main() {
    int arr[] = {9,1,22,4,0,-1,1,22,100,10};
    int size = sizeof(arr)/sizeof(int);
    quick_sort(arr, 0, size-1);
    for(int x: arr) std::cout << x << " "; // -1 0 1 1 4 9 10 22 22 100 
}

3 C#[ | ]

using System;
class Program {
    static void quickSort(int[] a) {
        quickSort(a, 0, a.Length-1);
    }
    static void quickSort(int[] a, int L, int R) {
        int left, right, pivot=a[L];
        for(left=L,right=R; left<right; right--) {
            while( a[right]>=pivot && left<right ) right--;
            if( left<right ) a[left] = a[right];
            while( a[left]<=pivot && left<right ) left++;
            if( left>=right ) break;
            a[right] = a[left];
        }
        a[left]=pivot;
        if( left>L ) quickSort(a, L, left-1);
        if( left<R ) quickSort(a, left+1, R);
    }
    static void Main() {
        int[] arr = {9,1,22,4,0,-1,1,22,100,10};
        quickSort(arr);
        Console.Write(string.Join(",",arr)); // -1,0,1,1,4,9,10,22,22,100
    }
}

4 Java[ | ]

public class MyClass {
    static void quickSort(int[] a) {
        quickSort(a, 0, a.length-1);
    }
    static void quickSort(int[] a, int L, int R) {
        int left, right, pivot=a[L];
        for(left=L,right=R; left<right; right--) {
            while( a[right]>=pivot && left<right ) right--;
            if( left<right ) a[left] = a[right];
            while( a[left]<=pivot && left<right ) left++;
            if( left>=right ) break;
            a[right] = a[left];
        }
        a[left]=pivot;
        if( left>L ) quickSort(a, L, left-1);
        if( left<R ) quickSort(a, left+1, R);
    }
    public static void main(String[] args) {
        int[] arr = {9,1,22,4,0,-1,1,22,100,10};
        quickSort(arr);
        for(int x:arr) System.out.format( "%d ", x ); // -1 0 1 1 4 9 10 22 22 100 
    }
}

5 PHP[ | ]

function quick_sort(&$a, $L=0, $R=-1) {
    if($R==-1) $R=count($a)-1;
    $pivot = $a[$L];
    for($left=$L,$right=$R; $left<$right; $right--) {
        while( $a[$right]>=$pivot && $left<$right ) $right--;
        if( $left<$right ) $a[$left] = $a[$right];
        while( $a[$left]<=$pivot && $left<$right ) $left++;
        if( $left>=$right ) break;
        $a[$right] = $a[$left];
    }
    $a[$left]=$pivot;
    if($left>$L) quick_sort($a, $L, $left-1);
    if($left<$R) quick_sort($a, $left+1, $R);
}
$arr = [9,1,22,4,0,-1,1,22,100,10];
quick_sort( $arr );
echo implode(',',$arr); # -1,0,1,1,4,9,10,22,22,100

6 Python[ | ]

def quick_sort(a, L=0, R=-1):
    if R==-1: R=len(a)-1
    pivot=a[L]
    left=L
    right=R
    while left<right:
        while a[right]>=pivot and left<right: right-=1
        if left<right: a[left]=a[right]
        while a[left]<=pivot and left<right: left+=1
        if left>=right: break
        a[right]=a[left]
        right-=1
    a[left]=pivot
    if left>L: quick_sort(a, L, left-1)
    if left<R: quick_sort(a, left+1, R)
arr = [9,1,22,4,0,-1,1,22,100,10]
quick_sort(arr)
print( arr ) # [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]

7 Ruby[ | ]

def quick_sort(a, l=0, r=-1)
    if r==-1; r=a.size-1; end
    pivot=a[l]
    left=l
    right=r
    while left<right
        while a[right]>=pivot and left<right; right-=1; end
        if left<right; a[left]=a[right]; end
        while a[left]<=pivot and left<right; left+=1; end
        if left>=right; break; end
        a[right]=a[left]
        right-=1
    end
    a[left]=pivot
    if left>l; quick_sort(a, l, left-1); end
    if left<r; quick_sort(a, left+1, r); end
end
arr = [9,1,22,4,0,-1,1,22,100,10]
quick_sort(arr)
print arr # [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]

8 같이 보기[ | ]

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