낑깡의 게임 프로그래밍 도전기

Sort 하기 본문

카테고리 없음

Sort 하기

낑깡겜플밍 2023. 11. 24. 21:46
반응형
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