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 |
Tags
- Simulation
- 드롭다운
- 행동트리
- articulation body
- dfs
- 너비탐색
- 디지털트윈
- 유니티 sparkmain(clone)
- navisworks api
- removeAll
- 최소신장트리 mst
- C#
- 깊이탐색
- 트리구조
- unity korea
- 오블완
- readonly
- BFS
- 크루스칼
- 티스토리챌린지
- dropdown
- raycast
- unity sparkmain(clone)
- sparkmain(clone)
- GetComponent
- 유니티
- Unity
- list clear
- 최단거리 알고리즘
- sparkmain(clone) 무한생성
Archives
- Today
- Total
낑깡의 게임 프로그래밍 도전기
Sort 하기 본문
반응형
SMALL
https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
Comparison Sorting Visualization
www.cs.usfca.edu
using JetBrains.Annotations;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
public class SortTest
{
public List<int> array;//배열선언
protected const int rootindex = 1;//1로 시작해서 0번째 배열은 안쓴다
public SortTest()
{
array = new List<int>();//새공간할당
array.Add(-1);//첫 0번째는 -1로 임의의 값 암거나 줌
}
public virtual void Add(int value)//일단 Add만
{
array.Add(value);
int intputCurlndex = array.Count - 1;
}
public virtual void Sort()
{
}
}
public class InsertionSort : SortTest
{
public override void Add(int value)
{
base.Add(value);
}
public override void Sort()
{
bool sortAble = true;
while (sortAble)
{
int count = 0;
int lastIndex = array.Count - 1;
int frontIndex = rootindex;
while (frontIndex < lastIndex)
{
int nextIndex = frontIndex + 1;
if (nextIndex <= lastIndex)
{
if (array[frontIndex] > array[nextIndex])
{
Swap(frontIndex, nextIndex);
count++;
}
}
frontIndex = frontIndex + 1;
nextIndex = nextIndex + 1;
}
if (count == 0)
{
sortAble = false;
}
}
}
public void Swap(int indexA, int indexB)
{
int temp = array[indexA];
array[indexA] = array[indexB];
array[indexB] = temp;
}
}
public class SelectionSort : SortTest
{
public override void Add(int value)
{
base.Add(value);
}
public override void Sort()
{
int startIndex = rootindex;
int lastIndex = array.Count - 1;
while (startIndex <= lastIndex)
{
int count = 0;
int pickIndex = startIndex;
for (int chack = startIndex + 1; chack <= lastIndex; chack++)
{
if (array[pickIndex] > array[chack])
{
pickIndex = chack;
count++;
}
}
Swap(startIndex, pickIndex);
startIndex++;
}
}
public void Swap(int indexA, int indexB)
{
int temp = array[indexA];
array[indexA] = array[indexB];
array[indexB] = temp;
}
}
public class BubbleSort : SortTest
{
public override void Add(int value)
{
base.Add(value);
}
public override void Sort()
{
int countIndex = rootindex;
bool sortAble = true;
while (countIndex <= array.Count - 1)
{
int frontIndex = rootindex;
int nextIndex = rootindex + 1;
int count = 0;
while (nextIndex <= array.Count - 1)
{
if (array[frontIndex] > array[nextIndex])
{
Swap(frontIndex, nextIndex);
count++;
}
frontIndex++;
nextIndex++;
}
countIndex++;
}
}
public void Swap(int indexA, int indexB)
{
int temp = array[indexA];
array[indexA] = array[indexB];
array[indexB] = temp;
}
}
public class Test : MonoBehaviour
{
InsertionSort insertionSort = new InsertionSort();
SelectionSort selectionSort = new SelectionSort();
BubbleSort bubbleSort = new BubbleSort();
void Start()
{
insertionSort.array.Add(3);
insertionSort.array.Add(2);
insertionSort.array.Add(6);
insertionSort.array.Add(9);
insertionSort.array.Add(4);
insertionSort.array.Add(8);
insertionSort.array.Add(1);
insertionSort.array.Add(5);
insertionSort.array.Add(7);
insertionSort.Sort();
for (int i = 1; i <= insertionSort.array.Count - 1; i++)
{
// Debug.Log(insertionSort.array[i]);
}
selectionSort.array.Add(3);
selectionSort.array.Add(2);
selectionSort.array.Add(6);
selectionSort.array.Add(9);
selectionSort.array.Add(4);
selectionSort.array.Add(8);
selectionSort.array.Add(1);
selectionSort.array.Add(5);
selectionSort.array.Add(7);
selectionSort.Sort();
for (int i = 1; i <= selectionSort.array.Count - 1; i++)
{
//Debug.Log(selectionSort.array[i]);
}
bubbleSort.array.Add(3);
bubbleSort.array.Add(2);
bubbleSort.array.Add(6);
bubbleSort.array.Add(9);
bubbleSort.array.Add(4);
bubbleSort.array.Add(8);
bubbleSort.array.Add(1);
bubbleSort.array.Add(5);
bubbleSort.array.Add(7);
bubbleSort.Sort();
for (int i = 1; i <= bubbleSort.array.Count - 1; i++)
{
Debug.Log(bubbleSort.array[i]);
}
}
void Update()
{
}
}
반응형
LIST