Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- simulation
- greedy
- Number Theory
- Math
- Tree
- 코딩테스트
- geometry
- two pointers
- Data Structure
- Stack
- hash table
- 구현
- implement
- java
- bit manipulation
- string
- array
- 자바
- 코테
- dynamic programming
- sorting
- Counting
- Matrix
- Binary Search
- SQL
- 파이썬
- Method
- database
- Class
- Binary Tree
Archives
- Today
- Total
코린이의 소소한 공부노트
Exchange Sort 본문
1. Problem
- n개의 요소를 오름차순으로 정렬해보자.
2. Input
1) 양수 n
2) 배열 S indexed from 1 to n
3. Output
1) 오름차순으로 정렬된 S
4. PseudoCode
void exchangesort(int n, keytype[] S){
index i, j;
for(i=1 ; i<=n ; i++)
for(j=i+1 ; j<=n ; j++)
if(S[j]<S[i])
exchange S[i] and S[j]
}
5. Example
class Test {
public static void main(String[] args){
int[] arr = {3, 6, 2, 1, 0};
exchangesort(arr.length, arr);
for(int i : arr)
System.out.print(i); // 01236
}
static void exchangesort(int n, int[] S){
for(int i=0 ; i<n ; i++) {
for(int j=i+1 ; j<n ; j++) {
if(S[j]<S[i]) {
int tmp = S[j];
S[j] = S[i];
S[i] = tmp;
}
} // j
} // i
}
}
- T(n) = n*(n-1)/2
'Back-End > Algorithm' 카테고리의 다른 글
n-th Fibonacci Term (Recursive) (0) | 2023.02.22 |
---|---|
Binary Search (iterative) (0) | 2023.02.22 |
Matrix Multiplication (0) | 2023.02.22 |
Add Array Members (0) | 2023.02.22 |
Sequential Search (0) | 2023.02.22 |