백준 | 2750 | 수 정렬하기
카테고리 없음 2019. 8. 20. 20:16반응형
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
32
33
34
35
36
37
38
|
using System;
using System.Linq;
namespace _2750
{
class Program
{
static void Main(string[] args)
{
var p = new Problem();
p.Solution(6);
}
}
public class Problem{
private int[] arr;
public void Solution(int n){
arr = new int[n];
int count = 0;
while(true){
if(count >= n) break;
var input = Console.ReadLine();
var num = int.Parse(input);
arr[count] = num;
count++;
}
var e = this.arr.Distinct().ToArray();
foreach(var num in e){
Console.WriteLine(num);
}
}
}
}
|
버블 정렬
javascript
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
|
function bubbleSort1(arr){
bubbleSort2(arr, arr.length - 1);
}
function bubbleSort2(arr, last){
if(last>0){
for(let i = 1; i<= last; i++){
if(arr[i-1] > arr[i]){
swap(arr, i-1, i);
}
}
bubbleSort2(arr, last-1);
}
}
function swap(arr, source, target){
let temp = arr[source];
arr[source] = arr[target];
arr[target] = temp;
}
function print(arr){
console.log(arr);
}
let arr = [3,5,4,2,1];
print(arr);
bubbleSort1(arr);
print(arr);
|
삽입 정렬
반응형